当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JScript实现表格的简单操作

JScript实现表格的简单操作

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了jscript实现表格的简单操作,供大家参考,具体内容如下

实现思路:

1、添加时:获取当前列表的行数,在当前一行添加下一行;
2、用insertcell()方法添加一行,下标从0开始,
3、若要给新一行添加类型、响应事件,就用setattribute()方法,类似于键值对,并用appendchild()方法将数据保存到新一行
4、删除时:获取需要删除行的当前行数this,然后获取父节点,把整一行删掉remove(),而不是单单删除某一行的单个数据
5、修改时:获取当前修改行的行数索引,点击修改时,把表格状态转换为文本格式,并把“修改”改为“确定”

实现代码:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <style>
      table{
        border-top: 1px solid #ccc;
        border-left: 1px solid #ccc;
        width: 400px;
      }
      td,th{
        border-right:1px solid #ccc ;
        border-bottom: 1px solid #ccc;
      }
    </style>
    <script>
      function add(){
        var table = document.getelementbyid("order");
        var index = table.rows.length;//表格行数
        var row = table.insertrow(index);//插入一个行并返回新一行


        var c0 = row.insertcell(0);
        var b0 = document.createelement("input");
        b0.setattribute("type","checkbox");
        b0.setattribute("onclick","seclect("+index+")");
        b0.setattribute("name","sel");
        c0.appendchild(b0);

        var c1 = row.insertcell(1);//在新一行插入一列,并返回新一列
        c1.innerhtml = prompt("请输入商品名称","");

        var c2 = row.insertcell(2);//在新一行插入一列,并返回新一列
        c2.innerhtml = prompt("输入数量","");

        var c3 = row.insertcell(3);//在新一行插入一列,并返回新一列
        c3.innerhtml = prompt("输入价格","");

        var c4 = row.insertcell(4);
        var b1 = document.createelement("input");
        b1.setattribute("type","button");
        b1.setattribute("value","删除");
        b1.setattribute("onclick","del(this)");

        var b2 = document.createelement("input");//创建按钮
        b2.setattribute("type","button");
        b2.setattribute("value","修改");
        b2.setattribute("style","margin-left: 5px");
        b2.setattribute("onclick","update("+index+")");

        c4.appendchild(b1);//把按钮添加到操作的单元格中
        c4.appendchild(b2);
      }

      function del(but){
        //var table = document.getelementbyid("order");
        but.parentnode.parentnode.remove();//根据节点的层级关系删除行
      }

      function update(index){
        var table = document.getelementbyid("order");
        //获得修改按钮
        var cell=table.rows[index].cells[4];

        cell.lastchild.setattribute("value","确定");
        //为按钮重新绑定事件
        cell.lastchild.setattribute("onclick","edit("+index+")");

        //修改数量
        var cellnumer = table.rows[index].cells[2];
        var txt = document.createelement("input"); //创建一个文本框
        txt.setattribute("value",cellnumer.innerhtml);//设置文本框的值
        txt.setattribute("size",5);//文本框长度
        cellnumer.innerhtml = "";//把单元格的数据清除
        cellnumer.appendchild(txt); //把文本框加入到单元格
      }

      function edit(index){
        var table = document.getelementbyid("order");

        var cell = table.rows[index].cells[4];

        cell.lastchild.setattribute("value","修改");
        cell.lastchild.setattribute("onclick","update("+index+")");

        //把单元格中的文本框删除
        var cellnumer = table.rows[index].cells[2];
        var num = cellnumer.firstchild.value;//取文本框的值
        cellnumer.removechild(cellnumer.firstchild);//删除文本框
        cellnumer.innerhtml = num;
      }

      function allselect(ch){
        var item = document.getelementsbytagname("input"); //取所有的input标签
        for(var i=0;i<item.length;i++){ //循环每一个
          if(item[i].type==ch.type){ //判断每一个标签的类型是否为checkbox
            item[i].checked = ch.checked; //复选框的选中与全选的复选框选中相同
          }
        }

      }
      function seclect(sh){
        var item = document.getelementsbyname("sel"); 
        var all = document.getelementbyid("all");
        var tag = true;
        for(var i=0;i<item.length;i++){//判断是否全部选中
          if(item[i].checked == false){
            tag = false;
            break;
          }
        }
        all.checked = tag;
      }
    </script>
  </head>
  <body>
    <center>
      <table id="order" >
        <tr>
          <th>
            <input type="checkbox" onclick="allselect(this)" id="all"/>全选
          </th>
            <th>商品名称</th>
            <th>数量</th>
            <th>单价</th>
            <th>操作</th>
          </tr>
        <tr>
          <td><input type="checkbox" onclick="seclect(this)" name="sel"/></td>
          <td>娃哈哈</td>
          <td>10</td>
          <td>2</td>
          <td><input value="删除" type="button" onclick="del(this)"style="margin-right:5px ;"/><input value="修改" type="button" onclick="update(1)"/></td>
        </tr>

      </table>
      <button onclick="add()">添加商品</button>
    </center>
  </body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网