【问题标题】:Tablesorter ajaxProcessing adding trTablesorter ajaxProcessing 添加 tr
【发布时间】:2016-10-06 04:58:14
【问题描述】:

我使用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];
   }

【问题讨论】:

    标签: javascript jquery tablesorter pager


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      相关资源
      最近更新 更多