【原生】js實現表格的增刪改查

說在前面的,寫給小白白的,大神請繞道~javascript

今天用原生js寫一下動態表格的增刪改查,主要是熟悉一下js的DOM操做。css

首先,作一個表格,用來顯示提交的數據,如圖下:java

此處,我添加了編號、姓名、密碼、生日、地址五個屬性,另外加選中、操做兩個操做,親們能夠自行添加,佈局代碼以下:node

複製代碼
<table class="table table-hover table-bordered" id="mytable">
    <thead>
        <tr>
            <th>選中</th>
            <th>編號</th>
            <th>姓名</th>
            <th>密碼</th>
            <th>生日</th>
            <th>地址</th>
            <th>操做</th>
        </tr>
        <tr>
            <td><input type="checkbox" onclick="checkAll(this)"/></td>
            <td colspan="6"><a href="javascript:;" class="btn btn-danger" role="button" onclick="delAll(this)">所有刪除</a></td>
        </tr>
    </thead>
    <tbody id="listTable">    
        <tr>
            <td><input type="checkbox" name="item" /></td>
            <td>100806131234</td>
            <td>劈日e斬月</td>
            <td>123456</td>
            <td>1995-08-07</td>
            <td>北京市朝陽區艾歐尼亞</td>
            <td>
                <input type="button" name="" value="刪除" class="btn btn-danger" onclick="del(this)" />
                <input type="button" name="" value="修改" class="btn btn-info" onclick="modify(this)" />
            </td>
        </tr>
    </tbody>
</table>
複製代碼

css方面我運用了bootstrap的表格框架,圖個方便,以爲很差看的,你們自行修改,在此就展現了。bootstrap

而後,下面添加一個須要提交的表單,如圖下:app

生日選項用了h5新屬性date,框架

佈局代碼以下:函數

複製代碼
<h1>新增數據</h1>
<form>
    <table class="table table-hover table-bordered">
        <tr>
            <th>編號</th>
            <td><input type="text" name="" class="form-control" id="num" /></td>
        </tr>
        <tr>
            <th>姓名</th>
            <td><input type="text" name="" class="form-control" id="username" /></td>
        </tr>
        <tr>
            <th>密碼</th>
            <td><input type="password" name="" class="form-control" id="pwd" /></td>
        </tr>
        <tr>
            <th>生日</th>
            <td><input type="date" name="" class="form-control" id="birth" /></td>
        </tr>
        <tr>
            <th>地址</th>
            <td><input type="text" name="" class="form-control" id="addre" /></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="reset" value="重置" class="btn btn-primary" id="reset" />   
                <input type="button" value="添加"  class="btn btn-success" id="add"  onclick="addList()" />
                <input type="button" value="更新"  class="btn btn-info" id=""  onclick="update()" />
            </td>
        </tr>
    </table>
</form>
複製代碼

基本佈局就這樣了,而後能夠寫腳本了,佈局

1.添加數據this

思路:獲取表單每一個input的value值,而後建立節點td,添加到上面的表格當中,很簡單,代碼實現以下:

複製代碼
function addList(){
        var oNum = document.getElementById('num').value;
        var oUser = document.getElementById('username').value;
        var oPwd = document.getElementById('pwd').value;
        var oBirth = document.getElementById('birth').value;
        var oAddre = document.getElementById('addre').value;
        var oTr = document.createElement('tr');
        var oTd1 = document.createElement('td');
        var oInput = document.createElement('input');
        oTd1.appendChild(oInput);
        oInput.setAttribute('type','checkbox');
        oInput.setAttribute('name','item');
        var oTd2 = document.createElement('td');
        oTd2.innerHTML = oNum;
        var oTd3 = document.createElement('td');
        oTd3.innerHTML = oUser;
        var oTd4 = document.createElement('td');
        oTd4.innerHTML = oPwd;
        var oTd5 = document.createElement('td');
        oTd5.innerHTML = oBirth;
        var oTd6 = document.createElement('td');
        oTd6.innerHTML = oAddre;
        var oTd7 = document.createElement('td');
        var oInput2 = document.createElement('input');
        var oInput3 = document.createElement('input');
        oInput2.setAttribute('type','button');
        oInput2.setAttribute('value','刪除');
        oInput2.setAttribute('onclick','del(this)');
        oInput2.className = 'btn btn-danger';
        oInput3.setAttribute('type','button');
        oInput3.setAttribute('value','修改');
        oInput3.setAttribute('onclick','modify(this)');
        oInput3.className = 'btn btn-info';
        oTd7.appendChild(oInput2);
        oTd7.appendChild(oInput3);
        oTr.appendChild(oTd1);
        oTr.appendChild(oTd2);
        oTr.appendChild(oTd3);
        oTr.appendChild(oTd4);
        oTr.appendChild(oTd5);
        oTr.appendChild(oTd6);
        oTr.appendChild(oTd7);
        var olistTable = document.getElementById('listTable');
        olistTable.appendChild(oTr);
    }
複製代碼

 

注意:建立節點的時候,相關的屬性值,樣式,事件同步一下,我這直接設置的點擊函數,這樣就能夠添加了,如圖下:

2.刪

1)單點對應刪除

思路:添加數據的時候,須要添加對應的點擊事件onclick=del(this),而後removeChild移除。實現代碼以下:

function del(obj){
        var oParentnode = obj.parentNode.parentNode;
        var olistTable = document.getElementById('listTable');
        olistTable.removeChild(oParentnode);
    }

2)多項刪除(所有刪除)

思路:經過checkbox的checked屬性當作開關,而後經過遍歷刪除checked=true對應的tr行。代碼以下:

複製代碼
//全選
    function checkAll(c){
        var status = c.checked;
        var oItems = document.getElementsByName('item');
        for(var i=0;i<oItems.length;i++){
            oItems[i].checked=status;
        }
    }
    //delAll功能
    function delAll(){
        var olistTable = document.getElementById('listTable');
        var items = document.getElementsByName("item");
        for(var j=0;j<items.length;j++){    
            if(items[j].checked)//若是item被選中
            {
                var oParentnode = items[j].parentNode.parentNode;
                olistTable.removeChild(oParentnode);
                j--;
            }
        }
    }
複製代碼

如圖下:

3.修改數據

思路:點擊「修改」後,將td的innerHTML值傳到表單中的input當中,同時用一個參數記錄下點擊的行數rowIndex,"更新"按鈕先找見須要修改的行數的Tr,而後將表單的值傳給每一個td;代碼實現以下:

複製代碼
//修改功能
    function modify(obj){
        var oNum = document.getElementById('num');
        var oUser = document.getElementById('username');
        var oPwd = document.getElementById('pwd');
        var oBirth = document.getElementById('birth');
        var oAddre = document.getElementById('addre');
        var oTr = obj.parentNode.parentNode;
        var aTd = oTr.getElementsByTagName('td');
        rowIndex = obj.parentNode.parentNode.rowIndex;  
        oNum.value = aTd[1].innerHTML;
        oUser.value = aTd[2].innerHTML;
        oPwd.value = aTd[3].innerHTML;
        oBirth.value = aTd[4].innerHTML;
        oAddre.value = aTd[5].innerHTML;
        console.log(aTd[4].innerHTML);
        //alert(i);

    }
    //更新功能
    function update(){
        var oNum = document.getElementById('num');
        var oUser = document.getElementById('username');
        var oPwd = document.getElementById('pwd');
        var oBirth = document.getElementById('birth');
        var oAddre = document.getElementById('addre');
        var oMytable = document.getElementById('mytable');
        //alert(rowIndex);
        //var aTd = rowIndex.cells;
        console.log(oMytable.rows[rowIndex].cells)
        oMytable.rows[rowIndex].cells[1].innerHTML = oNum.value;
        oMytable.rows[rowIndex].cells[2].innerHTML = oUser.value;
        oMytable.rows[rowIndex].cells[3].innerHTML = oPwd.value;
        oMytable.rows[rowIndex].cells[4].innerHTML = oBirth.value;
        oMytable.rows[rowIndex].cells[5].innerHTML = oAddre.value;
    }
複製代碼

實現效果如圖下:

相關文章
相關標籤/搜索