使用Ajax Pager设置行类

时间:2016-01-23 16:37:14

标签: tablesorter

我正在使用ajax pager插件,我想知道是否可以根据返回的JSON数据中的值将行添加到行中。例如,如果单元格具有特定值,则将行变为红色。

ajaxProcessing: function(data){
  if (data && data.hasOwnProperty('rows')) { 
    var r, row, c, d = data.rows, 
    total = data.total_rows, 
    headers = data.headers, 
    rows = [], 
    len = d.length; 
    for ( r=0; r < len; r++ ) { 
      row = [];
      for ( c in d[r] ) { 
        if (typeof(c) === "string") { 
          row.push(d[r][c]); 
        } 
      } 
      // is there a way to do that here when it pushes the row onto the array
      // or perhaps there is another function you have implemented that will let me do that
      rows.push(row);
    } 
    return [ total, rows, headers ]; 
  } 
},

谢谢

1 个答案:

答案 0 :(得分:0)

ajaxProcessing是一个回调函数,允许您返回问题中显示的数据,或者只返回总行数(ref):

ajaxProcessing: function(data, table, xhr){
  if (data && data.hasOwnProperty('rows')) {
    var r, row, c, d = data.rows,
    // total number of rows (required)
    total = data.total_rows,
    // all rows: array of arrays; each internal array has the table cell data for that row
    rows = '',
    // len should match pager set size (c.size)
    len = d.length;
    // this will depend on how the json is set up - see City0.json
    // rows
    for ( r=0; r < len; r++ ) {
      rows += '<tr class="ajax-row">'; // new row
      // cells
      for ( c in d[r] ) {
        if (typeof(c) === "string") {
          rows += '<td>' + d[r][c] + '</td>'; // add each table cell data to row
        }
      }
      rows += '</tr>'; // end new row
    }
    // find first sortable tbody, then add new rows
    table.config.$tbodies.eq(0).html(rows);
    // no need to trigger an update method, it's done internally
    return [ total ];
  }
}

return [ total ];之前,如果在构建字符串时尚未完成,则可以使用方法查找行并添加类名。

或者,对服务器的调用可以返回已包含应用于行(ref)的类名的HTML字符串:

ajaxProcessing: function(data, table, xhr){
  if (data && data.hasOwnProperty('rows')) {
    // data.rows would look something like this
    // '<tr class="fred"><td>r0c0</td><td>r0c1</td></tr><tr><td>r1c0</td><td>r1c1</td></tr>'
    return [ data.total, $(data.rows), data.headers ];
  }
}