增加和删除行
jquery对表格的操作是老生常谈的问题。最近项目中用到了,今天在这里分享一下!
效果大体如下:
分享一下代码吧!
html
| 板块 维度 |
|---|
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 = ' 中弘西岸3' +
'' +
' ';
$(".table>thead>tr").eq(0).append(firstLie);
var otherLie = '5||parseFloat(value)<1)execCommand('undo')"' +
'onafterpaste="if(isNaN(value)||parseFloat(value)>5||parseFloat(value)<1)execCommand('undo')" /> ';
$(".table>tbody>tr").append(otherLie);
},
addWd: function () { //增加一行
if ($("#Bk_table").is(":hidden")) {
$("#Bk_table").show();
}
if ($(".blankShow").is(":visible")) {
$(".blankShow").hide();
}
var Wdhtml_1 = '' +
' ' +
'维度三' +
'' +
'' +
' ';
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 + ' ';
}
}
var Wdhtml_3 = ' ';
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);
})
})
示例:
*{ 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;}
名称 价格 地址 备注 时间 商品1 10.5 商品2 11.3 商品3 9.8 商品4 12.6 商品5 13.9 商品6 18 商品7 21.3 商品8 6.5 商品9 7.4
效果:



