2

I have a jqGrid working with the SortableRows option enabled. Previously i had been allowing rows to always be sorted. When the rows are dropped, then the database is updated via an ajax call. To do this I have been using the update option.

Now i need to implement the sorting to NOT be allowed for certain rows. Namely those rows who have a column value of "1" for one of the visible columns. I have successfully blocked the ajax updating, but visually the row still gets dropped in the new location. I need to visually revert the row to the original location.

So far I have this code, which works, but the area with the comment is where I am stuck as to how to revert the row to the previous position.

jQuery("#all_driver_runs").jqGrid('sortableRows', {
    update: function (ev, ui) {
        //first check the value of the 'placed_in_runs' column
        if ($('#all_driver_runs').jqGrid('getRowData', ui.item[0].id)['placed_in_run'] !== "1") { //here update the database
            if (!ui.item[0].previousSibling) {
                $.post("scripts/update_driver_run_sort.php", {
                    this_one: ui.item[0].id,
                    prev: 'none',
                    next: ui.item[0].nextSibling.id
                });
            } else if (!ui.item[0].nextSibling) {
                $.post("scripts/update_driver_run_sort.php", {
                    this_one: ui.item[0].id,
                    prev: ui.item[0].previousSibling.id,
                    next: 'none'
                });
            } else {
                $.post("scripts/update_driver_run_sort.php", {
                    this_one: ui.item[0].id,
                    prev: ui.item[0].previousSibling.id,
                    next: ui.item[0].nextSibling.id
                });
            }
        } else {
            /*
             *no DB update, and now I need to revert to previous position here ???
             */
        }
    }
});
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
jeffery_the_wind
  • 17,048
  • 34
  • 98
  • 160

1 Answers1

4

Since the sortableRows is compatible with jQueryUI's sortable widget, you could block the rows from being sortable when you load them by adding a class to the row and then specifying it in the items (or cancel) members of the sortableRows' options.

So, supposing that class is unmovable, you could pass ".jqgrow:not(.unmovable)" to sortableRows. For example:

$('#all_driver_runs').jqGrid('sortableRows', {
    items: ".jqgrow:not(.unmovable)",
    /* remaining options if needed */
});

In order to add a class to a row, see this answer.

Community
  • 1
  • 1
ssarabando
  • 3,397
  • 2
  • 36
  • 42
  • 1
    That is some good stuff, thanks for the help! Works perfectly. – jeffery_the_wind Dec 16 '13 at 18:19
  • I came accross a problem with this setup. Now if you have 2 unmovable rows next to each other, then it is impossible to drop a movable row in-between them. I would still like to be able to drop a movable row in-between 2 unmovable rows. – jeffery_the_wind Dec 17 '13 at 16:24
  • 1
    See the `Cancel` member. Check [this fiddle](http://jsfiddle.net/ssarabando/C58z8/). The "DSK1" columns are unsortable while the "APPR" are. Try moving the "APPR"'s around and see if that's what you wanted. – ssarabando Dec 17 '13 at 17:35
  • Yes thanks for the help, I found that `cancel` method, and it seems to work well. Unfortunately the JS is messing up another method when I use `cancel`... oh well – jeffery_the_wind Dec 18 '13 at 12:22
  • 1
    Combine this with the jqGrid `rowattr` configuration option described here: http://stackoverflow.com/a/10531680/134120 and you can easily restrict dragging based on row data :) – AsGoodAsItGets Aug 11 '15 at 15:02