2016-10-06 5 views
0

私はtablesorterとpager plugin ajaxProcessingを使用します。しかし、私は問題がある、私はそれぞれの行にajaxの結果として<tr onclick = "selectProduct (row)"></tr>を追加したい。Tablesorter ajaxProcessing trを追加する

デモから、私はどのように行trをajaxの結果に挿入するのかわかりません。

私を助けてください。 ajaxProcessing機能を使用する場合

//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

、テーブルに行を適用するための多数の方法があります。あなたが戻り値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 ]; 
    } 
} 
関連する問題