How to ignore th with specific class name in tablesorter?

孤人 提交于 2019-12-08 12:25:31

问题


I use the tablesorter to sort the table, but not all columns, I want to ignore the specific column with the defined class name, eg:

<table class="basicList" cellpadding="0" cellspacing="0"> 
                    <thead> 
                        <tr> 
                            <th class="ck_field"><input type="checkbox" id="check_all" name="check_all" /></th>
                            <th class="col_filter"><a href="#" class="btn_filter">&nbsp;</a></th>
                            <th>Name <span class="sort_indicator">&nbsp;</span></th> 
                        </tr> 
                        <tr class="filter_row"> 
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td> <input type="text" id="the_name" name="name" class="filter_field"/></td> 
                        </tr>  
                    </thead>
                </table>

In this example, I don't want to sort the first two columns.

And I tried:

$(document).ready(function() {

    console.log($(".basicList .col_filter").index());
    console.log($(".basicList .ck_field").index());

    var ck_ignore = $(".basicList .ck_field").index();
    var filter_ignore = $(".basicList .col_filter").index();

    $(".basicList").tablesorter({ widgets: ['zebra'],headers: { 
            //disable the first checkbox cell            
            $ck_ignore: {                
                sorter: false 
            },
            $filter_ignore : {                
                sorter: false 
            }
        }  
    });

Which doesn't work, how to solve this problem?


回答1:


Use it like this:

var myHeaders = {};
myHeaders[ck_ignore] = { sorter: false };
myHeaders[filter_ignore] = { sorter: false };

$(".basicList").tablesorter({ widgets: ['zebra'], headers: myHeaders });

Note: if you have more columns with these classes, it will work only for the first occurences.




回答2:


As per the documentation, you can now use class="sorter-false"




回答3:


Ran into the same problem, and here's what I came up with. Assuming your th class for not sorting is called nosort:

function setupTablesorter() {
    $('table.tablesorter').each(function (i, e) {
        var myHeaders = {}
        $(this).find('th.nosort').each(function (i, e) {
            myHeaders[$(this).index()] = { sorter: false };
        });

        $(this).tablesorter({ widgets: ['zebra'], headers: myHeaders });
    });    
}

This has a few advantages over the other answers I saw here. First, it works even if you have multiple non-sortable columns. Second, it also still works if you have multiple tables on the same page, each with different sortable columns.




回答4:


Just use the headers argument:

$(document).ready(function() {

    console.log($(".basicList .col_filter").index());
    console.log($(".basicList .ck_field").index());

    var ck_ignore = $(".basicList .ck_field").index();
    var filter_ignore = $(".basicList .col_filter").index();

    $(".basicList").tablesorter({ widgets: ['zebra'],headers: {       
             // pass the headers argument and assing a object
        headers: {
            // assign the first column (we start counting zero)
            0: {
                // disable it by setting the property sorter to false 
                sorter: false 
            }
        }
        ,
            $filter_ignore : {                
                sorter: false
            }
        }  
    });


来源:https://stackoverflow.com/questions/5672993/how-to-ignore-th-with-specific-class-name-in-tablesorter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!