Skipping a row from rendering in jquery DataTables

后端 未结 1 2039
深忆病人
深忆病人 2021-01-19 06:22

I want to skip row rendering if a condition is met during its initialization, however I dont know where exactly to place it.

Should I put it in fnCreatedRow

相关标签:
1条回答
  • 2021-01-19 06:42

    For fnCreatedRow it is too late, and for fnPreDrawCallback you just end up cancelling rendering of the table. You have two different ways :

    1) Cleanup JSON in the ajax.dataSrc callback :

    var table = $('#example').DataTable( {
        ajax : {
            url : 'test.json',
            dataSrc: function(json) {
                var rows = [];
                for (var i=0;i<json.data.length;i++) {
                    //skip rows "if a condition is met"
                    //here just any rows except row #1
                    if (i>0) rows.push(json.data[i]);
                }
                return rows;
            }
        }
        ....
    })    
    

    2) Cleanup JSON upon the xhr event :

    table.on('xhr.dt', function (e, settings, json, xhr) {
        //manipulate the json directly, no return needed
        //delete row #1, same as above
        json.data.splice(0,1);
    });
    

    Both examples is under assumption that you have wellformed JSON on the (simplified) form

    {
     "data": [
         {
           "id": "2423",
           "username" : "joe"
         },
         {
           "id": "4321",
           "username" : "gordon"
         }
      ]
    }
    
    0 讨论(0)
提交回复
热议问题