onDrag Event does not work on D&D of items with childs

Hello,

I have a treegrid and a normal grid on my page to move items from the treegrid to the normal one using drag and drop.

To prevent double entries on my destination grid, I implemented a drag function that is called by the onDrag event of the grid.

Whenever I D&D items without childs from the tree to the grid, the event fires, returns false and does not allow dropping the item as expected, this works perfectly.

When I D&D items that have childs, the event fires, iterates all childs and checks every child if it is already existing in the destination grid. If it is already existing, the function returns false but then the child is dropped anyway. Why?

Here is my function:

function drag_function (source_item_id, target_item_id, source_grid_object, target_grid_object, source_column_index, target_column_index) {

        // * Single Select D&D Check: No Drag and Drop of already assinged items
	if ((source_item_id.indexOf(",") == -1)) {
		
		// * Get Childs of selected item
		var child_items = source_grid_object.getAllSubItems(source_item_id);
		
		// * Check if selected user has childs
		if (child_items != "") {
			
			// * Check all childs if they are already assigned to destinationlist
			var childs = child_items.split (",");
			
			;childs.foreach( function( k, v ) {
				if(typeof(v) !="undefined") {
					
					if (target_grid_object.isItemExists(v)) {
					
						// ********** To be done: Error, Return false does not prevent from adding double items!! **********
						return false;
						// ********** To be done: Error, Return false does not prevent from adding double items!! **********
					}
				}
			});
		}

		// * If item has no childs, check only if his ID already exists
		else {
			
			if (target_grid_object.isItemExists(source_item_id)) {
				return false;
			}
		}
		return true;

Thanks for your help!

Following code works perfectly at local sample:

[code]function drag_function (source_item_id, target_item_id, source_grid_object, target_grid_object, source_column_index, target_column_index) {
// * Single Select D&D Check: No Drag and Drop of already assinged items
if ((source_item_id.indexOf(",") == -1)) {

      // * Get Childs of selected item
      var child_items = source_grid_object.getAllSubItems(source_item_id);
      // * Check if selected user has childs
      if (child_items != "") {
         
         // * Check all childs if they are already assigned to destinationlist
         var childs = child_items.split (",");
         
         for (var i=0; i<childs.length; i++){
            if(typeof(childs[i]) !="undefined") {
               
               if (target_grid_object.isItemExists(childs[i])) {
                alert("Exists");
               
                  // ********** To be done: Error, Return false does not prevent from adding double items!! **********
                  return false;
                  // ********** To be done: Error, Return false does not prevent from adding double items!! **********
               }
            }
         }
      }

      // * If item has no childs, check only if his ID already exists
      else {
         
         if (target_grid_object.isItemExists(source_item_id)) {
            alert("Item exists!");
            return false;
         }
      }
  return true;
  }
  }[/code]

Hello Olga,

thank you very much for your reply. Your code works perfectly!

There is now just one problem left: When an item has childs, and one of his childs already exists in the target group, the event returns false and prevents ALL childs from beeing dropped. Also the ones, that are not yet existing in the target list.

Is there a possibility to block only the childs, that are already in the list and let the other child items pass?

Thanks again!

You can block default dnd operation at all and call moveRowTo() inside “onDrag” event handler, so you will be able to select, which rows from brand must be adder to target grid.
docs.dhtmlx.com/doku.php?id=dhtm … _moverowto