JS deletes the corresponding table row according to the selected check box

Source: Internet
Author: User
Tags rowcount

Today, it took me half a day to complete the add and delete functions for this stuff. I have been debugging for a long time because I didn't pay attention to the details!

The "add" button of a single machine dynamically creates a table to present the selected personnel data. The Code is as follows:

// Create the selected private void createselectnodeinfo () {If (Al. count> 0) {table TB = This. selectemployetable; htmlinputcheckbox cbox; dropdownlist DDL; For (INT I = 0; I <Al. count; I ++) {tablerow TR = new tablerow (); tablecell TC1 = new tablecell (); cbox = new htmlinputcheckbox (); cbox. id = ("cbox" + I ). tostring (); tc1.controls. add (cbox); tr. cells. add (TC1); tablecell TC2 = new tablecell (); tc2.text = al [I]. tostring (); tr. cells. add (TC2); tablecell TC3 = new tablecell (); DDL = new dropdownlist (); DDL. id = ("DDL" + I ). tostring (); DDL. datasource = bindddldata (); DDL. databind (); tc3.controls. add (DDL); tr. cells. add (TC3); TB. rows. add (TR); this. pnlselect. controls. add (TB );}}}

 

The single-host "delete" button will delete the corresponding row according to the selected check box. The algorithm here is a bit Rao. We know that when several check boxes are selected at the same time, each traversal is performed, the Rows marked by a check box will be deleted from top to bottom, so that the number of rows in the table will be reduced by one row, so we can declare a variable index to dynamically note the changes in the number of rows in the table, to delete multiple rows. The Code is as follows:

function DelRow() {    var tb = document.getElementById("AddEmployee1_selectEmployeTable");    if (tb.hasChildNodes) {        var rowCount = tb.childNodes[0].childNodes.length;        var index = 0;        for (var i = 0; i < rowCount; i++) {            var child = tb.childNodes[0].childNodes[index];            index++;            var mark = child.childNodes[0].childNodes[0].checked;            if (mark) {                child.parentNode.removeChild(child);                index--;            }        }    }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.