0

My code is:

var table1 = $('#view-table').DataTable( {
        fixedHeader: true,
        "search": {
            "smart": false
        },

        "columnDefs": [
            {
                "targets": [ 0,16 ],
                "visible": false,
                "bSearchable": true
            },
            { 
                "orderable": false, 
                "targets": [ 1,15 ] 
            }
        ],

        "createdRow": function( row, data, dataIndex ) {

            if ( data[16] == "yes" ) {   
                $(row).addClass('warning');
            }
            if ( data[0] == "yes" ) {   
                $(row).removeClass('warning');
                $(row).addClass('success');
            }
        },
        "ajax": '/getViewData',
        "pageLength": 25
    });

I have looked at jQuery How to count the no of rows in table by distinct column value but it only brings back what's on the screen at the time.

What I need is to see how many rows have the value of 'yes' in the 16th column for all the data that is brought back. Not just the data is on the screen. Everything.

All the examples I've tried, as the one above, only work where it's not AJAX based

2
  • Uhm, I believe you just want var count = 0; on top of your callback and then when it's yes, just count += 1;. Is that it? Commented Feb 23, 2018 at 20:15
  • I could only get that to work when it was static, not ajax and not for ever row in the data returned i.e. just for data on the screen at the time Commented Feb 23, 2018 at 20:17

2 Answers 2

1

I have the same problem a few weeks ago, I'm not sure if this is the best solution but it did the work then:

var count = table1.rows(function(idx, data, node) {
  return data[16] == 'yes'
}).count();

EDIT: If you want that count after the ajax ends, you can user initComplete

initComplete: function(row, data, index) {
   var count = table1.rows(function(idx, data, node) {
      return data[16] == 'yes'
   }).count();
   console.log('count: ', count);
}, // initComplete()

EDIT 2: add jsfiddle

Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't work with ajax data - and it only works on stuff I see on the screen, unless I shouldn't be using initComplete?
I was showing the count on an user action, but yes, you can use it on initComplete callback. Check the edit.
Still only counts on what's visible rather than everything else
I add a jsfiddle, It doesn't check the 'yes' just some numbers, but you'll get the idea. Otherwise, paste your full implementation.
-1

You should probably use ajax callback. Instead of this "ajax": '/getViewData' do this:

"ajax": {
  "type": "GET",
  "url": "/getViewData",
  "dataSrc": function(json){
    //Count your rows here
    return json.data;
  }  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.