7

http://jsfiddle.net/Lijo/auny3/に示すような HTML テーブルがあります。テーブルには 10 列あり、3 つの列グループに分かれています。ボタン「Show Associate」と「Hide Associate」を使用して、「Associate Info」という名前の colgroup (行データを含む) を非表示/表示する必要があります。

jQueryを使用してこれを行うための(パフォーマンスの観点から)最良の方法は何ですか?

http://jsfiddle.net/Lijo/auny3/8/よりも良い答えがあれば答えてください

注: http://www.sacdeveloper.com/Community/Articles/tabid/86/ctl/ArticleView/mid/458/articleId/1/Group_columns_in_an_ASPNet_Gridview.aspxに示されているように、ASP.Net GridView を使用してテーブルを生成しています。

参考

  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/

ここに画像の説明を入力

4

4 に答える 4

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();
    }
});
于 2012-09-17T09:02:59.243 に答える
2

こんにちは、これに慣れました

Jクエリ

$(document).ready(function(){

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



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

そしてあなたのhtmlへのいくつかの変更

ライブデモ

于 2012-09-17T08:49:59.150 に答える
2

colspanここでは、現在の HTML を使用しており、Associate Info ヘッダーがより多くの列を格納した場合でも動作し続けます (スクリプトは、適切な数の列を取得するためにその属性を探しています)。

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

デモ

于 2012-09-17T09:35:29.680 に答える
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;
};


}
);
于 2012-09-18T11:11:19.993 に答える