3

I have my primary grid dragging/dropping rows correctly into a secondary grid. My question is, how do I perform a check just before the row is dropped into my secondary grid, which determines if the row I am attempting to drop is already there? If it is already there in the secondary grid, don't let the user drop it there, basically stop the drag/drop function.

I'm thinking I can grab the key value from the row I am attempting to drop. Then, check to see if that value already exists as the key value in one of the rows I already dropped. I'm assuming I will have to use this function in some way:

beforedrop : function(e,ui,data,source,target) { }

OR this function:

ondrop: function (ev, ui, getdata) { }

Anyone have any ideas?

FastTrack
  • 8,810
  • 14
  • 57
  • 78

1 Answers1

5

The example of the usage could be about the following

$("#grid1").jqGrid('gridDnD', {
    connectWith: '#grid2',
    beforedrop: function (ev, ui, getdata, $source, $target) {
        var names = $target.jqGrid('getCol', 'name2');
        if ($.inArray(getdata.name2, names) >= 0) {
            // prevent data for dropping
            ui.helper.dropped = false;
            alert("The row is already in the destination grid");
        }
    }
});

On the demo you would be unable to drop the rows "test1" from the first grid to the second one:

enter image description here

Other rows will be dropped without any problems.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Perfect! Awesome job once again Oleg. This will save me hours of searching and work! Thanks for the other [answer](http://stackoverflow.com/questions/10163970/jqgrid-subgrid-with-local-data) as well. – FastTrack Apr 16 '12 at 20:28
  • @FastTrack: You are welcome! Bye the way I found your both questions very interesting. I think that other people could have the same problems too. – Oleg Apr 16 '12 at 20:32
  • Oleg: Yes! I am a newbie on here but I try to get up as many questions as I can while I'm working with jqGrid. It's a great tool, but the documentation is lacking. This website has become a great resource because of knowledgeable people such as yourself. – FastTrack Apr 17 '12 at 12:43
  • Hey Oleg: Can you have a look at [this quick question](http://stackoverflow.com/questions/10193503/change-the-css-of-a-jqgrid-row-being-dragged) about the CSS of the dragged row? – FastTrack Apr 17 '12 at 14:48
  • Hey Oleg, can you please have a look at [this question](http://stackoverflow.com/questions/11567866/jqgrid-drag-and-drop-onto-a-div) about dropping onto a DIV? I'd REALLY appreciate any help you could offer. – FastTrack Jul 19 '12 at 19:25
  • @FastTrack: It's possible what you need to do. What you need is jut to understand how [jQuery Droppable](http://jqueryui.com/demos/droppable/) works. I recommend you additionally to examine and debug the code of [gridDnD](https://github.com/tonytomov/jqGrid/blob/v4.4.0/js/grid.jqueryui.js#L326-476) function. I am now in the business trip in another country (Swaziland) and write now from a hotel. So I can't help you more now. – Oleg Jul 19 '12 at 19:51
  • One question though: what if I want to remove the row from the source grid but still avoid inserting it into the target grid where the row already exists. Is that possible? – IcedDante Mar 26 '14 at 20:05
  • @IcedDante: You are welcome! I think that the most easy solution you can get if you would delete dropped row from the target grid inside of `ondrop` callback of `gridDnD`. See [the answer](http://stackoverflow.com/a/2858459/315935). Alternatively you can try to modify `ui.helper.dropped` to false inside of `beforedrop` callback. I don't tested it, but I hope it will wort too and it will prevent inserting of the row in the target grid. – Oleg Mar 26 '14 at 20:45
  • Hey Oleg, please have a look at this http://stackoverflow.com/questions/37754145/drag-jqgrid-one-column-value-to-another-like-in-excel question also – Xravn Jun 28 '16 at 09:09