2016-10-06 9 views
0

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

Из демонстрации там я не вижу, как вставить строку 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]; 
 
    }

ответ

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