2014-05-21 28 views
1

我正在使用jQuery表排序插件。jquery - 基於所有列的屬性排序

表列是基於sql查詢生成的。

如何根據屬性值對(asc和desc)進行排序。

注意:我不想像下面的代碼,因爲表列是生成多個(有時50列),所以我需要基於相同的屬性名稱對所有列進行排序。

$.tablesorter.addParser({ 
    // set a unique id 
    id: 'myParser', 
    is: function(s) { 
     // return false so this parser is not auto detected 
     return false; 
    }, 
    format: function(s, table, cell, cellIndex) { 
     // get data attributes from $(cell).attr('data-something'); 
     // check specific column using cellIndex 
     return $(cell).attr('data-something'); 
    }, 
    // set type, either numeric or text 
    type: 'text' 
    }); 

回答

2

如果您使用我的tablesorter的叉子,你什麼數據屬性您正在使用您的表中可以設置textAttribute option來。默認情況下,它被設置爲data-text

<td data-text="1">First</td> 

該小區將然後使用從data-text,而不是「第一」的值進行排序。

+0

你好,在我的情況下,表列是基於查詢(mysql數據庫)生成的。所以,我認爲這種方法不適合我的情況。 http://mottie.github.io/tablesorter/docs/example-option-text-extraction.html – RSKMR