[英]data tags are working only on the first page of dataTable
I have a table made with a foreach
.For every row i give to the edit button(which show up a modal) some data tags with the product details. 我有一个用foreach
制成的表格。每行我给编辑按钮(显示一个模式),其中包含一些带有产品详细信息的数据标签。
When the edit button it's clicked i take every data tag
and i store the information in variables
.The problem is that,after i switch to another page, the javascript
variables will get stucked with the last data tags
used from the first page. 当单击编辑按钮时,我将获取每个data tag
,并将信息存储在variables
。问题是,切换到另一页后, javascript
变量将卡在第一页中使用的最后一个data tags
。
I saw questions about this but the users were using checkboxes
or links
, not data tags
. 我看到了有关此问题,但用户正在使用checkboxes
或links
,而不是data tags
。
My table: 我的桌子:
<tbody>
<?php foreach ($products as $product):?>
<tr class="active">
<td><?php echo $product['products_id']; ?></td>
<td><?php echo $product['product_name'];?></td>
<td><?php echo $product['ingredients'];?></td>
<td><?php echo $product['grams'];?></td>
<?php if($product['show'] == 0): ?>
<td><?php echo 'Nu'; ?></td>
<?php else: ?>
<td><?php echo 'Da'; ?></td>
<?php endif; ?>
<td><?php echo $product['price'];?></td>
<td>
<i class="fas fa-edit edit-btn" data-toggle="modal" data-target="#editModal" data-prod_category = "<?php echo $product['category']; ?>" data-prod_img="<?php echo $product['image']; ?>" data-prod_name="<?php echo $product['product_name']; ?>" data-prod_id="<?php echo $product['products_id']; ?>" data-prod_ingredients="<?php echo $product['ingredients'];?>" data-prod_grams="<?php echo $product['grams'];?>" data-prod_price="<?php echo $product['price'];?>" data-prod_show="<?php echo $product['show']; ?>"></i>
<button type="button" class="delete_toggle" data-toggle="modal" data-target="#deleteModal" data-prod_id="<?php echo $product['products_id']; ?>" style="border: none;"><i class="fas fa-trash"></i></button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
Jquery: jQuery:
var id = $(this).data('prod_id');
var name = $(this).data('prod_name');
var category = $(this).data('prod_category');
var ingredients = $(this).data('prod_ingredients');
var grams = $(this).data('prod_grams');
var price = $(this).data('prod_price');
var show = $(this).data('prod_show');
var img = $(this).data('prod_img');
var details = [id,name,category,ingredients,grams,price,show,img];
Your code should work, but you don't specify the most important thing: how you are catching the click event. 您的代码应该可以工作,但是您没有指定最重要的事情:如何捕获click事件。 Maybe you were using something like 也许您正在使用类似
$('.delete_toggle').click(function() {
var id = $(this).data('prod_id');
// etc
});
Which obviously cannot work on pages not yet generated by DataTable. 这显然无法在尚未由DataTable生成的页面上工作。 You need to bind the event in this manner: 您需要通过以下方式绑定事件:
$('#your_table_id').on('click', '.delete_toggle', function() {
var id = $(this).data('prod_id');
// etc
});
Other than that, there's nothing wrong with your code. 除此之外,您的代码没有任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.