我正在使用啓用了響應選項的jquery數據表。我無法返回行數據時,該表是在響應模式:在響應式數據表中返回行數據
我的表:
$('#productsTable').DataTable({
responsive: true
});
表有產品代碼,品牌,類別,價格一欄,「添加到購物車'按鈕。該按鈕應該檢索行數據。但是,當表處於響應模式(又名縮小,某些列已摺疊)時,單擊我的按鈕,它不會返回任何數據。
我的HTML表:
<table id="productsTable" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>Product Code</th>
<th>Brand</th>
<th>Category</th>
<th>Price ($)</th>
<th></th>
</tr>
</thead>
<tbody>
{{#each context}}
<tr>
<td class="product_code">{{product_code}}</td>
<td class="brand">{{brand}}</td>
<td class="category">{{category}}</td>
<td class="price">{{invoice_price}}</td>
<td><button type="button" class="btn btn-primary">Add to Cart</button></td>
</tr>
{{/each}}
</tbody>
</table>
我添加到購物車事件:
$(".btn-primary").click(function(e) {
var price = $(this).parent().parent().children('td.price').text();
var context = {
product_code: $(this).parent().parent().children('td.product_code').text(),
brand: $(this).parent().parent().children('td.brand').text(),
category: $(this).parent().parent().children('td.category').text(),
price: $(this).parent().parent().children('td.price').text(),
};
console.log(context) //console.logs context
});
摺疊的表的圖片:
有人能幫忙嗎?
提前致謝!
嗨 - 我試過了,它完美的作品,如果表是在其全尺寸。但是,如果它崩潰了(我添加了上面的屏幕截圖),然後單擊我的按鈕,它不會返回任何數據... –
有道理,我認爲列被隱藏,未轉換爲子行。在這種情況下,請查看此鏈接https://datatables.net/extensions/responsive/examples/child-rows/index.html。在$('#productsTable').DataTable()。row($ tr).child()''上嘗試console.log。你將不得不解析html來找到你需要的數據。我不認爲數據表提供了方便的數據訪問子行。 –