2012-01-02 118 views
4

这被问了很多次,我知道但这是不同的!我做了这个:与jQuery隐藏表列

enter image description here

当你点击减号图标,列应该消失,这个工作用PHP,但代码是一个烂摊子,我想获得这个使用jQuery,我发现其他一些线程我这表明:

$("#minus").click(function() { 
    $("#table td:nth-child(2),th:nth-child(2)").hide(); 
}); 

过了一会儿,我想出了这个:

var num = $("#columnid").index();  
$("#table td:nth-child("+ num +"),th:nth-child("+ num +")").hide(); 

这有点儿工作,但我需要调用它的Wi一个onclick="jquery_function();"函数,并让PHP插入每个头的ID,但这可能吗?或者做这个的另一种方式是什么?我卡住了!

这做到了:

$(".minus").click(function() { 
    var num = $(this).parent().index() + 1; 
    $("#table td:nth-child("+ num +"),th:nth-child("+ num +")").fadeOut(250); 
}); 

似乎搞清楚之后简单,吉荣说得对。 :)我没有得到的唯一的事情是为什么你需要(“th”),与和没有工作。谢谢!

+0

欢迎来到SO吉迪恩。 – 2012-01-02 15:43:53

+0

Datatables.net可以显示/隐藏两种方式,http://datatables.net/examples/api/show_hide.html和http://datatables.net/extras/colvis/,它有状态保存 – Anagio 2012-01-02 15:46:59

+0

您的意思是回显'​​'标签里面的ID?那几乎就是''td id =“”>'如果这就是你需要的......但我不认为就是这样。你能解释一下你需要多一点吗? – James 2012-01-02 15:58:36

回答

2

看来你几乎已经完成了。你可以做什么,将它包装在一个函数中,并将事件处理函数附加到表格标题单元格中的按钮上:

$("th .button").click(function(){ 
    var num = $(this).parents("th").index(); // untested, but something like this should do it 
    $("#table td:nth-child("+ num +"),th:nth-child("+ num +")").hide(); 
    return false; 
} 
+0

啊,所以我只需要让var变得更有活力,感谢这个想法,会在一分钟内尝试! – Gideon 2012-01-02 15:55:20

+0

@Gideon准确地说,你不需要这个ID,你只需要找出在按钮上点击了哪个'th'。 – jeroen 2012-01-02 15:57:11

0

这个怎么样?

// note the class for multiple minus 
$(".minus").click(function() { 
    var $this = $(this); 
    var index = $('.minus').index($this); // get the zero based index of this element 
    index++; // adjust for zero based index 
    var selector = '#table td:nth-child(' + index + '), the:nth-child(' + index + ')'; 
    $(selector).hide(); 
}); 

未经测试,并随意忽略代码,因为您认为合适。

+0

看到我现在的工作代码,这应该工作。它只是看起来很混乱,但也许这就是我。虽然谢谢! – Gideon 2012-01-02 16:16:31