Tablesorter ajax Процессинг добавления tr

Я использую TableSorter и пейджер плагин ajaxProcessing. Но у меня есть проблема, я хочу добавить <tr onclick = "selectProduct (row)"></tr> в каждом ряду в результате AJAX.

Из демки там не вижу как вставить строку tr в результатах AJAX.

Пожалуйста, помогите мне.

//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 ответ

При использовании ajaxProcessing Функция, есть множество способов применить строки к таблице. Если вы посмотрите на возвращаемый массив только с общим примером, вы увидите, что строки могут быть добавлены в 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 ];
  }
}
Другие вопросы по тегам