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

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

2016530164711889.gif (712×315)

分享一下代码吧!
html

<div> <table> <thead> <tr> <th> <div> <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><span>中弘西岸3</span>' + '<input type="text" placeholder="填写板块名称" />' + '<a></a></th>'; $(".table>thead>tr").eq(0).append(firstLie); var otherLie = '<td><input type="text" 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">' + '<span>维度三</span>' + '<input type="text" placeholder="填写维度名称" />' + '<a></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" 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&lt;aTdCont.length;i++){ var trCont = aTdCont[i]; $(&quot;tbody tr&quot;).each(function() { var thisText = $(this).children(&quot;td:eq(&quot;+tdIndex+&quot;)&quot;).text(); if(thisText == trCont){ $(&quot;tbody&quot;).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){ $(&quot;tbody tr&quot;).each(function() { var tdCont = $(this).children(&quot;td:eq(&quot;+thIndex+&quot;)&quot;).text(); aTdCont.push(tdCont); }); } //点击时需要执行的函数 var clickFun = function(thindex){ aTdCont = []; //获取点击当前列的索引值 var nThCount = thindex; //调用sortTh函数 取出要比较的数据 fSetTdCont(nThCount); } //点击事件绑定函数 $(&quot;th&quot;).toggle(function(){ thi= $(this).index(); clickFun(thi); //调用比较函数,降序 fSort(compare_up); //重新排序行 setTrIndex(thi); },function(){ clickFun(thi); //调用比较函数 升序 fSort(compare_down); //重新排序行 setTrIndex(thi); }) })

示例:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzpwjp.html