使用jQuery显示/隐藏具有colspan的表格列

7
我有一个HTML表格,如http://jsfiddle.net/Lijo/auny3/所示。该表格有10列,分为三个列组。我需要使用“显示合伙人”和“隐藏合伙人”按钮来隐藏/显示名为“Associate Info”的列组(包括其行数据)。
在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所述。
参考:
  1. http://jsfiddle.net/Lijo/auny3/8/
  2. http://jsfiddle.net/Lijo/auny3/12/
  3. http://jsfiddle.net/Lijo/auny3/11/
  4. http://jsfiddle.net/Lijo/auny3/13/
选定答案:
  1. http://jsfiddle.net/Lijo/UqdQp/2/
enter image description here

你能否更改你的HTML?如果可以的话,那么使用jQuery是可能的。 - Rohit Azad Malik
4个回答

2

你好,现在已经习惯了这个。

JQuery

$(document).ready(function(){

$("#show").click(function(){
    $("#showhide").show();
    });



    $("#hide").click(function(){
    $("#showhide").hide();
    });
})  

需要对您的HTML进行一些更改

查看演示


它可以工作。然而,我将无法使用它。我正在使用GridView生成表格。将表格列包装在表格中可能不可行。你能提供一个替代方案吗? - LCJ

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();
    }
});

谢谢。我尝试申请到其他部分,但没有成功。需要改变什么才能使它工作?http://jsfiddle.net/Lijo/auny3/9/ - LCJ
1
@Lijo 在 else 语句中将 hide 更改为 show,并在选择器中添加 th:nth-child(3):first 来隐藏“关联信息”。请参见此链接 - Alex Ball

2

在这里,我们使用您当前的HTML,并且如果您的“关联信息”标题存储了更多列(脚本正在查找其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();
});​

DEMO


你能解释一下它是如何工作的吗?我对“.add(label)”感到困惑。 - LCJ
1
它将我们想要隐藏或显示的每个元素存储到 "associate" 对象中。它首先从第 1 至第 3 行存储列 1 至 4,然后添加(jQuery.add)到这些元素的 "Associate Info" 单元格中。这样,我们就可以在一个 jQuery 对象 "associate" 中处理所有想要使用的元素了。 - aaaaaa
谢谢。所以,您在上面的解释中使用了从零开始的索引。另外,变量名“label”可以重命名为“firstLine”。 - LCJ
我已经发布了一个通用的方法作为答案。请提供您的想法。 - LCJ

1

我已经概括了@Pioul提供的想法。如果您喜欢这个答案,请为@Pioul投票。这种通用方法在http://jsfiddle.net/Lijo/UqdQp/10/中可用。

参考资料:

  1. 在包含跨列单元格的表中使用jQuery查找列索引

  2. 根据单元格中的值选择表格单元格

代码

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;
};


}
);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接