简体   繁体   English

如何删除(而不是删除)动态生成的表元素?

[英]how to remove(not delete) dynamically generated table elements?

Hi I have looped and echoed out this result 嗨,我已经循环播放并回显了此结果

echo '<tr><td id="'.$row['productID'].'"><img height="150px" width="130px" 
src="products/'.$row['image_url'].'"></td><td>'.$row['name'].'</td>
<td>'.$row['price'].'</td></tr>';

The above resulted in a table full of data, now How do i remove a specific row, I want to remove it not delete from the table, as in a shopping cart where you remove the item but not delete it from the table. 上面的结果是一个充满数据的表,现在我该如何删除特定的行,我想删除它而不是从表中删除,就像在购物车中删除项目但不从表中删除它一样。 How would you use javascript or any other in this case? 在这种情况下,您将如何使用javascript或其他任何语言?

Thank you very much. 非常感谢你。

Make a extra field in table. 在表格中添加一个额外的字段。

Default value for the active Row is 1.... 活动行的默认值为1 ....

deactive row is value is 0 停用行的值为0

When retrieve the data form table used the where function for the Active rows 检索数据表单表时,对活动行使用where函数

Give it a id and using jQuery do as 给它一个id并使用jQuery做为

$('yourid').hide(); // to hide

$('yourid').show(); // to show

请不需要JQuery这样简单的东西。

document.getElementById('id_off_product').style.display = 'none'; 

You don't need jQuery to do this, although you can use it if you want. 您不需要jQuery来执行此操作,尽管您可以根据需要使用它。

If you want to hide the product: 如果要隐藏产品:

document.getElementById('product_id').style.display = 'none';

With jQuery: 使用jQuery:

$('#product_id').hide();

If you want to show it again: 如果要再次显示:

document.getElementById('product_id').style.display = '';

With jQuery: 使用jQuery:

$('#product_id').show();

If you want to completely remove the product: 如果要完全删除产品:

document.getElementById('product_id').remove();

With jQuery: 使用jQuery:

$('#product_id').remove();

Also, you should probably put the id on the actual table row ( tr ) instead of on the table cell ( td ). 另外,您可能应该将id放在实际的表行( tr )上,而不是放在表格单元格( td )上。

skimberk1's answer is most comprehensive. skimberk1的答案是最全面的。 Here is an example that would allow a button on the row that would remove the row (parent/tr). 这是一个示例,该示例允许该行上的按钮删除该行(父/ tr)。

<table id="test">
    <tr>
        <td>one</td><td>two</td><td id="test" onclick="removerow(this);">remove</td>
    </tr>
</table>

function removerow(e) {
    e.parentNode.remove();
}

http://jsfiddle.net/FqbHW/19/embedded/result/ http://jsfiddle.net/FqbHW/19/embedded/result/

If completely removing is not desirable, then hiding or otherwise indicating it's inactive is possible... I would in that case also point you to Taveer's answer. 如果不希望将其完全删除,则可以隐藏或以其他方式表明它处于非活动状态……在这种情况下,我也将指出Taveer的答案。 You would need to track which rows are active/inactive. 您将需要跟踪哪些行处于活动状态/非活动状态。 If you need additional details on this please comment. 如果您需要其他详细信息,请发表评论。

如果您使用jquery,则必须知道要删除的产品ID

$('td#productId').parent().hide();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM