I have the following code which applies the jQuery UI Sortable plugin to all tables (GridView) on an ASP.NET page, excluding the first (header) row in each:
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();
However I only want to apply sortable to a table if there is more than one row (in addition to the header row) otherwise the drag/drop functionality is redundant.
How can I conditionally apply the above only to tables having more than one content row? Thanks.
jquery filter() provides a solution. It allows you to filter a set of matched elements using a test function. Therefore the following code applies sortable only to tables that have more than one data row:
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();
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With