Restrict the drag if drag item is already exist - Ej2 javascript list box

Please find the below Sample

https://stackblitz.com/edit/hu2feb?file=index.ts,index.html

In this sample has two list box controls and shared with same data source.

Both list box has same item "Australia". While drag an item(Australia) and drop into another list box.

Need to cancel the drag action if item is already exists in dropped list box.

Is any direct property available in list box control to achieve this scenario?
I have achieved this with my own logic but it raise more control level issues. 
Can you please share any workaround?


1 Reply

YA YuvanShankar Arunagiri Syncfusion Team July 18, 2022 09:17 AM UTC

Hi Durai,


We have validated your reported query and prepared the sample based on your requirement. We can achieve your requirement by using the beforeDrop event of the listbox. Please refer the below code and sample link.


[index.ts]:

let listObj1: ListBox = new ListBox({

  // Set the scope of the ListBox.

  scope: 'combined-list',

 

  // Set the dragAndDropA to the data source.

  dataSource: dataA,

 

  // Set allowDragAndDrop as `true`.

  allowDragAndDrop: true,

 

  beforeDrop: onDropGroupA,

 

  height: '330px',

 

  // Map the appropriate columns to fields property.

  fields: { text: 'Name' },

});

 

listObj1.appendTo('#listbox1');

 

function onDropGroupA(args: DropEventArgs): void {

  if (args.droppedElement.offsetParent.id !== args.target.offsetParent.id) {

    var items = listObj2.getDataList();

    for(var i = 0; i < items.length; i++) {

      if (items[i] === args.items[0]) {

        args.cancel = true;

      }

    }

  }

}



Sample link: https://stackblitz.com/edit/hu2feb-7sde4b?file=index.ts


Could you please check the above sample and get back to us, if you need any further assistance on this. 


Regards,

YuvanShankar A


Loader.
Up arrow icon