0

データが動的に生成される同じテンプレートに 2 つのテーブルがあります。両方のテーブルがデータを持つことができるか、どちらもデータを持つことができないか、どちらか一方だけがデータを持つことができます。他のテーブルにデータがない場合でも、テーブルをデータで並べ替えたいので、jQuery で次のような if 条件を使用しました。

$(document).ready(function() { 
    // call the tablesorter plugin 
    if ($("#product-table tbody td").length > 0){
    $("table").tablesorter({ 
        // sort on the first column and third column, order asc 
        sortList: [[0,0],[2,0]] 
    }); 
    }
}); 

これを使用すると、コンソールにエラーは表示されませんが、1 つのテーブルが空の場合、別のテーブルでは並べ替えが機能しません。両方のテーブルにデータがある場合、並べ替えは機能します。

4

1 に答える 1

0

1 つのテーブルのみのデータの長さをチェックしています。こうやってみて、

$(document).ready(function() { 
    // call the tablesorter plugin 
    if ($("#product-table tbody td").length > 0){
    $("#product-table").tablesorter({ 
        // sort on the first column and third column, order asc 
        sortList: [[0,0],[2,0]] 
    }); 
    }
    if ($("#product-table2 tbody td").length > 0){
    $("#product-table2").tablesorter({ 
        // sort on the first column and third column, order asc 
        sortList: [[0,0],[2,0]] 
    }); 
    }
});
于 2013-06-10T05:20:22.070 に答える