数据表根据列值计算行数并将其附加到列 header

datatables count row count based on column value and append it to column header

正如标题所说,我正在尝试计算特定字符在特定列中出现的次数,例如,如果 m 出现 30 次,我想 return 30 并将其附加到该列 header。 这是我的一部分

   function loadData()
    {
    var counter = 0;
    dataTable = $('#example').dataTable( {
     ...
     ...
    "sAjaxSource": "ReturnData",
    "columnDefs": [
                    {          
                        "render": function ( data, type, row ) {
                          if( data == 'm') {
                            counter++;
                          }
                         return data; 
                        // not sure how to return counter to the column header
                        },
                        "targets": [1]
                  }, 
           ]
        } );
       console.log(counter);
      }

例如我的 table 看起来像

 id  char(3)
---   ----
 1     m
 2     l
 3     y
 4     m
 5     m

更新:如果我控制台日志我得到 0

您应该将 counter 附加到 initComplete 处理程序中的列 header。这是一个示例,计算 "Firefox" 的所有实例并将数字添加到列 header :

table = $('#example').DataTable({
    columnDefs: [{          
        render: function ( data, type, row ) {
                    if (data.indexOf('Firefox')>-1) counter++;
                    return data;
                },
                targets: 1        
    }],
    initComplete : function() {
        counter =' (Firefox : '+counter+')';
        var $th = $("#example thead th:nth-child(2)");
        $th.text($th.text()+counter);
    }     
});

演示 -> http://jsfiddle.net/ae7t7w4k/