jQuery增加和删除表格项目及实现表格项目排序的方法


Posted in Javascript onMay 30, 2016

增加和删除行
jquery对表格的操作是老生常谈的问题。最近项目中用到了,今天在这里分享一下!
效果大体如下:

jQuery增加和删除表格项目及实现表格项目排序的方法

分享一下代码吧!
html

<div class="table-responsive" id="Bk_table" style="display:none;">
          <table class="table table-hover table-bordered">
            <thead>
              <tr>
                <th>
            <div class="out"> 
              <b>板块</b> 
              <em>维度</em> 
            </div>
            </th>
            </tr>
            </thead>
            <tbody>
            </tbody>
          </table>
 </div>

js操作如下:

deleteLie: function () { //删除一列
      var index = $(this).parent().index();
      for (var i = 0; i < $(".table tr").length; i++) {
        $($(".table tr")[i]).children().eq(index).remove();
      }
      if ($(".table tr").length == 1 && $(".table tr").eq(0).children().length == 1) {
        $("#Bk_table").hide();
        $(".blankShow").show();
      }
    },
    deleteOneline: function () { //删除一行
      $(this).parent().parent().remove();
      if ($(".table tr").length == 1 && $(".table tr").eq(0).children().length == 1) {
        $("#Bk_table").hide();
        $(".blankShow").show();
      }
    },
    addOneBk: function () { //增加一列
      if ($("#Bk_table").is(":hidden")) {
        $("#Bk_table").show();
      }
      if ($(".blankShow").is(":visible")) {
        $(".blankShow").hide();
      }
      var firstLie = ' <th class="hovershow"><span class="font_zs" style="display:none">中弘西岸3</span>' +
          '<input type="text" class="form-control getPrevalue" placeholder="填写板块名称" />' +
          '<a class="glyphicon glyphicon-remove bkdelete delete_lie"></a></th>';
      $(".table>thead>tr").eq(0).append(firstLie);
      var otherLie = '<td><input type="text" class="form-control" value="" placeholder="1-5之间数字" ' +
          'onkeyup="if(isNaN(value)||parseFloat(value)>5||parseFloat(value)<1)execCommand(\'undo\')"' +
          'onafterpaste="if(isNaN(value)||parseFloat(value)>5||parseFloat(value)<1)execCommand(\'undo\')" /></td>';
      $(".table>tbody>tr").append(otherLie);
    },
    addWd: function () { //增加一行
      if ($("#Bk_table").is(":hidden")) {
        $("#Bk_table").show();
      }
      if ($(".blankShow").is(":visible")) {
        $(".blankShow").hide();
      }
      var Wdhtml_1 = '<tr>' +
          ' <th scope="row" class="hovershow">' +
          '<span class="font_zs t1" style="display:none">维度三</span>' +
          '<input type="text" class="form-control getPrevalue" placeholder="填写维度名称" />' +
          '<a class="glyphicon glyphicon-remove bkdelete deleteoneline"></a>' +
          '</th>';
      var Wdhtml_2 = "";
      var LieLength = $(".table>thead>tr").children().length - 1;
      if (LieLength > 0) {
        for (var i = 0; i < LieLength; i++) {
          Wdhtml_2 = Wdhtml_2 + ' <td><input type="text" class="form-control" value="" placeholder="1-5之间数字" onkeyup="if(isNaN(value)||parseFloat(value)>5||parseFloat(value)<1)execCommand(\'undo\')" onafterpaste="if(isNaN(value)||parseFloat(value)>5||parseFloat(value)<1)execCommand(\'undo\')" /></td>';
        }
      }
      var Wdhtml_3 = '</tr>';
      var allWd = Wdhtml_1 + Wdhtml_2 + Wdhtml_3;
      $(".table>tbody").append(allWd);
}

表格排序
这个就稍微复杂点了...
主要思路:
因为JS有SORT的方法,对数组进行排序,那么通过个方法,我们就会想到数组了。
1.点标表格标头的时候,取出点击的是那一列。即列的索引值。因为下面要进行排序的就是该列。所以我要知道是点的那一列。
2.对表格的数据部分,也就是tbody部分,进行点击的列的取值,把这些值存入到一个数组当中。
3.将存入数据的数组,通过SORT方法进行排序。(这里写了两种,升,或降,因为是点击时要切换排序的方式。第一次降,第二次升,第三降,第四升,依次进行)
4.将排序好的数组的值进行遍历,在遍历过程中,和每一行TR的点击列的那个TD当中的数据进行一个比较。如果相等,就插入到tbody的最后去.(最先插入的,将是在第一行。)

$(function(){
  //存入点击列的每一个TD的内容;
  var aTdCont = [];

  //点击列的索引值
  var thi = 0
  
  //重新对TR进行排序
  var setTrIndex = function(tdIndex){
    for(i=0;i<aTdCont.length;i++){
      var trCont = aTdCont[i];
      $("tbody tr").each(function() {
        var thisText = $(this).children("td:eq("+tdIndex+")").text();
        if(thisText == trCont){
          $("tbody").append($(this));
        }
       });    
    }
  }
  
  //比较函数的参数函数
  var compare_down = function(a,b){
      return a-b;
  }
  
  var compare_up = function(a,b){
      return b-a;
  }
  
  //比较函数
  var fSort = function(compare){
    aTdCont.sort(compare);
  }
  
  //取出TD的值,并存入数组,取出前二个TD值;
  var fSetTdCont = function(thIndex){
      $("tbody tr").each(function() {
        var tdCont = $(this).children("td:eq("+thIndex+")").text();
        aTdCont.push(tdCont);
      });
  }
  //点击时需要执行的函数
  var clickFun = function(thindex){
    aTdCont = [];
    //获取点击当前列的索引值
    var nThCount = thindex;
    //调用sortTh函数 取出要比较的数据
    fSetTdCont(nThCount);
  }
  
  //点击事件绑定函数
  $("th").toggle(function(){
    thi= $(this).index();
    clickFun(thi);
    //调用比较函数,降序
    fSort(compare_up);
    //重新排序行
    setTrIndex(thi);
  },function(){
    clickFun(thi);
    //调用比较函数 升序
    fSort(compare_down);
    //重新排序行
    setTrIndex(thi);
  })  
})

示例:

<style type="text/css">
*{ margin:0px; padding:0px;}
table{
  border-collapse:collapse;}
table td{
  border:1px solid #036;
  text-align:center;
  }
thead tr th{
  cursor:pointer;
  background:#066;
  color:#FFFFFF;
  }
thead tr th:hover{
  background:#369;}
</style>
<table class="tabSort" width="546" height="300" border="0" align="center" cellpadding="0" cellspacing="0">
<thead>
  <tr>
   <th scope="col">名称</th>
   <th scope="col">价格</th>
   <th scope="col">地址</th>
   <th scope="col">备注</th>
   <th scope="col">时间</th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td>商品1</td>
   <td>10.5</td>
   <td> </td>
   <td> </td>
   <td> </td>
  </tr>
  <tr>
   <td>商品2</td>
   <td>11.3</td>
   <td> </td>
   <td> </td>
   <td> </td>
  </tr>
  <tr>
   <td>商品3</td>
   <td>9.8</td>
   <td> </td>
   <td> </td>
   <td> </td>
  </tr>
  <tr>
   <td>商品4</td>
   <td>12.6</td>
   <td> </td>
   <td> </td>
   <td> </td>
  </tr>
  <tr>
   <td>商品5</td>
   <td>13.9</td>
   <td> </td>
   <td> </td>
   <td> </td>
  </tr>
  <tr>
   <td>商品6</td>
   <td>18</td>
   <td> </td>
   <td> </td>
   <td> </td>
  </tr>
  <tr>
   <td>商品7</td>
   <td>21.3</td>
   <td> </td>
   <td> </td>
   <td> </td>
  </tr>
  <tr>
   <td>商品8</td>
   <td>6.5</td>
   <td> </td>
   <td> </td>
   <td> </td>
  </tr>
  <tr>
   <td>商品9</td>
   <td>7.4</td>
   <td> </td>
   <td> </td>
   <td> </td>
  </tr>
 </tbody>
</table>

效果:

jQuery增加和删除表格项目及实现表格项目排序的方法

Javascript 相关文章推荐
用 JSON 处理缓存
Apr 27 Javascript
js实现基于正则表达式的轻量提示插件
Aug 29 Javascript
JavaScript 七大技巧(二)
Dec 13 Javascript
jQuery中的on与bind绑定事件区别实例详解
Feb 28 Javascript
详解Angular 开发环境搭建
Jun 22 Javascript
web前端vue实现插值文本和输出原始html
Jan 19 Javascript
webpack4 css打包压缩问题的解决
May 18 Javascript
详解Angular-ui-BootStrap组件的解释以及使用
Jul 13 Javascript
Vue源码中要const _toStr = Object.prototype.toString的原因分析
Dec 09 Javascript
js Math数学简单使用操作示例
Mar 13 Javascript
VUE中V-IF条件判断改变元素的样式操作
Aug 09 Javascript
antd中table展开行默认展示,且不需要前边的加号操作
Nov 02 Javascript
使用jQuery判断浏览器滚动条位置的方法
May 30 #Javascript
JS检测移动端横竖屏的代码
May 30 #Javascript
BootStrap中Tab页签切换实例代码
May 30 #Javascript
js小数计算小数点后显示多位小数的实现方法
May 30 #Javascript
拥Bootstrap入怀——导航栏篇
May 30 #Javascript
基于Bootstrap实现tab标签切换效果
Apr 15 #Javascript
JavaScript编写带旋转+线条干扰的验证码脚本实例
May 30 #Javascript
You might like
Cappuccino 卡布其诺咖啡之制作
2021/03/03 冲泡冲煮
创建数据库php代码 用PHP写出自己的BLOG系统
2010/04/12 PHP
基于asp+ajax和数据库驱动的二级联动菜单
2010/05/06 PHP
PHP实现的下载远程图片自定义函数分享
2015/01/28 PHP
PHP设计模式之模板方法模式定义与用法详解
2018/04/02 PHP
php5与php7的区别点总结
2019/10/11 PHP
js活用事件触发对象动作
2008/08/10 Javascript
Javascript 陷阱 window全局对象
2008/11/26 Javascript
一个很酷的拖动层的js类,兼容IE及Firefox
2009/06/23 Javascript
基于jquery的滚动新闻列表
2010/06/19 Javascript
基于jQuery实现网页打印功能
2015/12/01 Javascript
ES6中如何使用Set和WeakSet
2016/03/10 Javascript
利用bootstrapValidator验证UEditor
2016/09/14 Javascript
JavaScript实现经典排序算法之选择排序
2016/12/28 Javascript
input框中自动展示当前日期yyyy/mm/dd的实现方法
2017/07/06 Javascript
node.js-v6新版安装具体步骤(分享)
2017/09/06 Javascript
[30:37]【全国守擂赛】第三周擂主赛 Dark Knight vs. Leopard Gaming
2020/05/04 DOTA
Python编程实现二叉树及七种遍历方法详解
2017/06/02 Python
关于python3中setup.py小概念解析
2019/08/22 Python
pytorch多GPU并行运算的实现
2019/09/27 Python
Python class的继承方法代码实例
2020/02/14 Python
python实现PolynomialFeatures多项式的方法
2021/01/06 Python
上级检查欢迎词
2014/01/18 职场文书
金融管理应届生求职信
2014/02/20 职场文书
目标责任书范本
2014/04/16 职场文书
小学假期安全广播稿
2014/09/28 职场文书
2014社区健康教育工作总结
2014/12/16 职场文书
实习介绍信模板
2015/01/30 职场文书
我们的节日元宵节活动总结
2015/02/06 职场文书
趣味运动会通讯稿
2015/07/18 职场文书
2016年优秀共青团员事迹材料
2016/02/25 职场文书
Nginx下配置Https证书详细过程
2021/04/01 Servers
python spilt()分隔字符串的实现示例
2021/05/21 Python
node快速搭建后台的实现步骤
2022/02/18 NodeJs
Python列表的索引与切片
2022/04/07 Python
Windows Server 2019 配置远程控制以及管理方法
2022/04/28 Servers