我有一个HTML表格,如http://jsfiddle.net/Lijo/auny3/所示。该表有10列 - 分为三个col组。我需要使用“Show Associate”和“Hide Associate”按钮隐藏/显示名为“Associate Info”的colgroup(包括其行数据)。
使用jQuery执行此操作的最佳方式(在性能方面)是什么?
如果答案比http://jsfiddle.net/Lijo/auny3/8/
更好,请回答注意:我使用的ASP.Net GridView生成表格 http://www.sacdeveloper.com/Community/Articles/tabid/86/ctl/ArticleView/mid/458/articleId/1/Group_columns_in_an_ASPNet_Gridview.aspx
参考:
答案 0 :(得分:2)
你现在习惯了这个
Jquery
$(document).ready(function(){
$("#show").click(function(){
$("#showhide").show();
});
$("#hide").click(function(){
$("#showhide").hide();
});
})
并对您的HTML进行了一些更改
的 Live demo 强> 的
答案 1 :(得分:2)
或者您可以使用nth-child
选择器:
$('input').click(function(){
if($(this).val() == "Hide Associate"){
$('th:nth-child(2), td:nth-child(2), th:nth-child(3):not(:first), td:nth-child(3), th:nth-child(4), td:nth-child(4), th:nth-child(5), td:nth-child(5)').hide();
}else{
$('th:nth-child(2), td:nth-child(2), th:nth-child(3):not(:first), td:nth-child(3), th:nth-child(4), td:nth-child(4), th:nth-child(5), td:nth-child(5)').show();
}
});
答案 2 :(得分:2)
在这里,使用您当前的HTML,并且如果您的Associate Info标头存储了更多列(脚本正在查找其colspan
属性以获取适当数量的列),它将继续工作。
$("input").on("click", function(e){
e.preventDefault();
var label = $(".resultGridTable .tableColGroupAssociate"),
colspan = parseInt(label.attr("colspan"), 10),
associate = $("tr:gt(0)")
.find("th:gt(0):lt("+ colspan +"), td:gt(0):lt("+ colspan +")")
.add(label);
if($(this).val() == 'Hide Associate') associate.hide();
else associate.show();
});
答案 3 :(得分:1)
我概括了@Pioul提供的想法。因此,如果你喜欢这个答案,请为@Pioul投票。这种通用方法可在http://jsfiddle.net/Lijo/UqdQp/10/
中找到参考文献:
Finding column index using jQuery when table contains column-spanning cells
CODE
var associateStartElementString = "EmpID";
var financialStartElementString = "Type";
var associateHTMLElements;
var financialHTMLElements;
var associateHideClass = '.tableColGroupAssociate';
var financialHideClass = '.tableColGroupTransaction';
$(document).ready(function () {
////////Hide Sections
//Associate Hide
$('.associateHide').live("click", function (e) {
e.preventDefault();
var hideClass = associateHideClass;
associateHTMLElements = HideSection(hideClass, associateStartElementString);
$('.btnAssociate').show();
});
//Financial Hide
$('.financialHide').live("click", function (e) {
e.preventDefault();
var hideClass = financialHideClass ;
financialHTMLElements = HideSection(hideClass, financialStartElementString);
$('.btnFinancial').show();
});
////SHOW
$('.btnAssociate').click(function()
{
associateHTMLElements.show();
var firstHeaderLineElement = $(".resultGridTable").find(associateHideClass );
firstHeaderLineElement.show();
});
$('.btnFinancial').click(function()
{
financialHTMLElements.show();
var firstHeaderLineElement = $(".resultGridTable").find(financialHideClass );
firstHeaderLineElement.show();
});
//Function 1
function HideSection(hideClass, startElementString)
{
var htmlElement = GetTableSections(hideClass, startElementString);
var firstHeaderLineElement = $(".resultGridTable").find(hideClass);
var variableToSet;
if (!(htmlElement === undefined)) {
variableToSet = htmlElement;
htmlElement.hide();
firstHeaderLineElement.hide();
}
return variableToSet;
}
//Function 2
function GetTableSections(hideClass, referenceHeaderCellValue) {
var firstHeaderLineElement = $(".resultGridTable").find(hideClass);
var colspan = parseInt(firstHeaderLineElement.attr("colspan"));
var startElementIndex = GetNonColSpanIndex(referenceHeaderCellValue);
if (startElementIndex > 0) {
startElementIndex = (startElementIndex - 1);
var selectedElements = $("tr:gt(0)")
.find("th:gt(" + startElementIndex + "):lt(" + colspan + "), td:gt(" + startElementIndex + "):lt(" + colspan + ")");
return selectedElements;
}
}
//Function 3
function GetNonColSpanIndex(referenceHeaderCellValue) {
var selectedCell = $("th").filter(function (i) {
return ($.trim($(this).html())) == referenceHeaderCellValue;
});
var allCells = $(selectedCell).parent('tr').children();
var normalIndex = allCells.index($(selectedCell));
var nonColSpanIndex = 0;
allCells.each(
function (i, item) {
if (i == normalIndex)
return false;
var colspan = $(selectedCell).attr('colspan');
colspan = colspan ? parseInt(colspan) : 1;
nonColSpanIndex += colspan;
}
);
return nonColSpanIndex;
};
}
);