如何有条件地将jQuery可排序UI小部件应用于表(ASP.NET GridView)?

Mar*_*ane 5 asp.net jquery gridview jquery-ui jquery-ui-sortable

我有以下代码将jQuery UI可排序插件应用于ASP.NET页面上的所有表(GridView),不包括每个表中的第一行(标题):

function pageLoad() {
    $('table').sortable({
        items: 'tr:not(:first)',
        axis: 'y',
        scroll: true,
        start: startDrag, //store start position
        stop: stopDrag    //get end position, update database
    }).disableSelection();
Run Code Online (Sandbox Code Playgroud)

但是,如果有多行(除了标题行),我只想对表进行排序,否则拖放功能是多余的.

如何有条件地将上述内容仅应用于具有多个内容行的表?谢谢.

Jam*_*ack 5

jquery filter()提供了一个解决方案.它允许您使用测试功能过滤一组匹配的元素.因此,以下代码仅适用sortable于具有多个数据行的表:

function pageLoad() {

    $('table').filter(function () {
                        return $('tr', this).length > 2;
                      })
              .sortable({
                 items: 'tr:not(:first)',
                 axis: 'y',
                 scroll: true,
                 start: startDrag, //store start position
                 stop: stopDrag    //get end position, update database
                 })
              .disableSelection();
}
Run Code Online (Sandbox Code Playgroud)