简体   繁体   English

动态删除表中的行

[英]Deleting rows in a table dynamically

I am making a mail web application .So while showing all inbox messages at top i am having a image button to delete all those items that are checked in corresponding checkboxes of rows of my mail table. 我正在制作一个邮件Web应用程序。因此,在顶部显示所有收件箱消息时,我有一个图像按钮来删除在我的邮件表行的相应复选框中选中的所有那些项目。

My image for delete is : 我要删除的图片是:

<img src="images/delete.png" /></div>

And for each row the row fetches data from database and is something like this : 对于每一行,该行都从数据库中获取数据,如下所示:

<tr bgcolor="#5D7B9D" color="#FFFFFF" onmouseover="ChangeColor(this, true,true);" onmouseout="ChangeColor(this, false,true);" onclick="DoNav('showmail.jsp?mid=<%=messageid%>');">    
<td><input type="checkbox" onclick="DoRemove(event);" width="20"></td>
<td callspan="3" width="1000px"><%=sendername%>  :   <%=messagesubject%>      <%=sendingtime%></td>
</tr>

Now how to remove the rows dynamically from my rows and at the same time from the database too.Please help 现在如何动态地从我的行以及同时从数据库中删除行。请帮助

EDIT : 编辑:

I was trying to put all checked rows checkboxes ids in an array.Whats wrong with this ? 我试图将所有选中的行复选框ID放入数组中。这有什么问题?

try {
        var checkedrows=[]
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;

        for(var i=0; i<rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null != chkbox && true == chkbox.checked) {
                //table.deleteRow(i);
                checkedrows.push(e.prop(chkbox.id));

            }


        }
        }catch(e) {
            alert(e);
        }
}

To get a list of checked checkboxes, you can use: 要获取已选中复选框的列表,可以使用:

var checkedBoxes = document.querySelectorAll('input:checked');

but IE 8 and lower don't support that, but you can use querySelectorAll with a class (see below). 但是IE 8及更低版本不支持该功能,但您可以将querySelectorAll与一个类一起使用(请参见下文)。 You can then do processing based on the returned NodeList. 然后,您可以根据返回的NodeList进行处理。

Given simple function that goes from an element up to a parent with a provided tag name, the rest is easy: 给定简单的功能(从一个元素到具有提供的标签名称的父元素),其余的操作很简单:

// Starting from root, go up to an element with tagName
// and return it. Otherwise return undefined
function upTo(tagName, root) {
  if (!root) return;
  tagName = tagName.toLowerCase();
  while ((root = root.parentNode)) {
    if (root.tagName && root.tagName.toLowerCase() == tagName) {
       return root;
    }
  }
}    

You can then do: 然后,您可以执行以下操作:

function deleteRow(el) {
    var row = upTo('tr', el);
    if (row) row.parentNode.removeChild(row);
}

Some test HTML: 一些测试HTML:

<table>
  <tr>
    <td><button onclick="deleteRow(this);">Delete row</button>
</table>

If you want to have a checkbox on each row and then use a single button to delete all the checked ones, you can do something like: 如果要在每行上都有一个复选框,然后使用一个按钮删除所有选中的复选框,则可以执行以下操作:

function deleteRows() {
    var flags = document.querySelectorAll('.deleteRowFlag');
    for (var i=0, iLen=flags.length; i<iLen; i++) {
      if (flags[i].checked) {
        deleteRow(flags[i]);
      }
    }
}

And use it like: 并像这样使用它:

<table>
  <tr>
    <td><input type="checkbox" class="deleteRowFlag">
    <td>foo
  <tr>
    <td><input type="checkbox" class="deleteRowFlag">
    <td>bar
</table>

<button onclick="deleteRows();">Delete checked rows</button>

You can perform an AJAX request to delete the row from database, and when the request succeeds delete the row in question from the page. 您可以执行AJAX请求以从数据库中删除行,当请求成功后,从页面中删除有问题的行。

I'd recommend jQuery and assigning a unique id to each row (corresponding to the ID field in your database) 我建议使用jQuery并为每行分配一个唯一的ID(对应于数据库中的ID字段)

Assuming your checkboxes have a common class: 假设您的复选框具有一个通用类:

var checkedMessages = [];
$('checkboxCls').each(function(e) {
   if(e.prop('checked')) { checkedMessages.push(e.prop('id')); }
}

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

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