jQGrid Drag and Drop Row Check
Asked Answered
G

1

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?

Gambrel answered 13/4, 2012 at 19:10 Comment(0)
D
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.

Drennan answered 16/4, 2012 at 19:39 Comment(9)
Perfect! Awesome job once again Oleg. This will save me hours of searching and work! Thanks for the other answer as well.Gambrel
@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.Drennan
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.Gambrel
Hey Oleg: Can you have a look at this quick question about the CSS of the dragged row?Gambrel
Hey Oleg, can you please have a look at this question about dropping onto a DIV? I'd REALLY appreciate any help you could offer.Gambrel
@FastTrack: It's possible what you need to do. What you need is jut to understand how jQuery Droppable works. I recommend you additionally to examine and debug the code of gridDnD 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.Drennan
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?Derangement
@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. 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.Drennan
Hey Oleg, please have a look at this #37754645 question alsoHoudini

© 2022 - 2024 — McMap. All rights reserved.