Exclude a column from being sorted using jQuery tablesorter

后端 未结 8 1758
有刺的猬
有刺的猬 2021-02-01 21:30

I am looking for a way to exclude a single column from being sorted using jQuery\'s tablesorter plugin. Specifically, I have a fairly large table and would like to keep a \"row

相关标签:
8条回答
  • 2021-02-01 21:45
    $("table").tablesorter({
        headers: {4: {sorter: false},8: {sorter: false}}
    });
    
    0 讨论(0)
  • 2021-02-01 21:53

    Here is a widget you can use that will accomplish what you are looking for:

    $(function() {
        // add new widget called indexFirstColumn
        $.tablesorter.addWidget({
            // give the widget a id
            id: "indexFirstColumn",
            // format is called when the on init and when a sorting has finished
            format: function(table) {               
                // loop all tr elements and set the value for the first column  
                for(var i=0; i < table.tBodies[0].rows.length; i++) {
                    $("tbody tr:eq(" + i + ") td:first",table).html(i+1);
                }                                   
            }
        });
    
        $("table").tablesorter({
            widgets: ['zebra','indexFirstColumn']
        });
    
    });
    
    0 讨论(0)
  • 2021-02-01 21:55

    This would skip sorting for the first column, and allow it for the second one. Just set true/false for all columns starting with first column as zero.

    <script>
    $(document).ready(function() { 
        $("table").tablesorter({
            headers: {
                0: {sorter: false},
                1: {sorter: true},
                3: {sorter: false}
            }//headers
        }); 
    });            
    </script>
    
    0 讨论(0)
  • 2021-02-01 21:58

    The answer of Brian Fisher is correct, but it is too slow in large tables (+1600 rows in my example). I improved the way of iterating through every rows. Everything else is the same.

    $(function() {
        // add new widget called indexFirstColumn
        $.tablesorter.addWidget({
            // give the widget a id
            id: "indexFirstColumn",
            // format is called when the on init and when a sorting has finished
            format: function(table) {               
                // loop all tr elements and set the value for the first column  
                $(table).find("tr td:first-child").each(function(index){
                    $(this).text(index+1);
                })                                  
            }
        });
    
        $("table").tablesorter({
            widgets: ['zebra','indexFirstColumn']
        });
    });
    
    0 讨论(0)
  • 2021-02-01 22:02

    Hrm. From tablesorter's method of reorganizing the table, I'm pretty sure that this isn't entirely possible. Tablesorter pulles each tr out of the DOM one by one and sorts them based on an indexed field, reinserting the entire tr without changing the tr's contents in any way. Your requested solution would then need to iterate back through the table after each sort and re-enumerate the first column. Tablesorter does have a plugin method, which is used by the zebrastripe and other extensions. Perhaps this could be used to hook the sort methods?

    0 讨论(0)
  • 2021-02-01 22:05

    For those who find this while looking for a way to exclude a column from being sortable (i.e. clickable header on the column), the below example excludes column 4 (zero-indexed) from being sorted):

    $("table").tablesorter({
        headers: {4: {sorter: false}}
    });
    
    0 讨论(0)
提交回复
热议问题