Tablesorter ajaxProcessing添加tr

时间:2016-10-06 04:58:15

标签: javascript jquery tablesorter pager

我使用tablesorter和pager插件ajaxProcessing。但我有一个问题,我想在ajax产生的每一行中添加<tr onclick = "selectProduct (row)"></tr>

从那里的演示中我看不到如何在ajax的结果中插入行tr

请帮帮我。

//ajax result
{"total_rows":"3","headers":["Gambar","SKU","Nama","Harga Beli","Harga Jual","Stok"],
  "rows":[
    {"Gambar":"",
     "SKU":"SKU0001",
     "Nama":"Lenovo Notebook",
     "HargaBeli":"Rp. 1.000.000",
     "HargaJual":"Rp. 2.000.000",
     "Stok":"10"},
    {"Gambar":"",
     "SKU":"SKU0002",
     "Nama":"Logitech Mouse",
     "HargaBeli":"Rp. 10.000",
     "HargaJual":"Rp. 20.000",
     "Stok":"20"},
    ]}

//javascript after ajaxProcessing
if (data && data.hasOwnProperty('rows')) {
   var indx, r, row, c, d = data.rows,
   total = data.total_rows,
   headers = data.headers,
   headerXref = headers.join(',').replace(/\s+/g, '').split(','),
   rows = [],
   len = d.length;
   for (r = 0; r < len; r++) {
     row = [];
       for (c in d[r]) {
         if (typeof (c) === "string") {
         indx = $.inArray(c, headerXref);
           if (indx >= 0) {
             row[indx] = d[r][c];
           }
         }
       }
       rows.push(row);
     }
     return [total, rows, headers];
   }

1 个答案:

答案 0 :(得分:0)

使用ajaxProcessing函数时,有许多方法可以将行应用于表。如果您查看返回array with only the total示例,则可以看到可以在ajaxProcessing函数中添加行。

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 ];
  }
}