function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Faizan Nusrat 27Faizan Nusrat 27 

Drag and drop functionality of table in lwc?

When draging the record and droping it, is showing in console but not happenng on screen. Need help on this in LWC
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Faizan,

Can you please check the below examples for Drag and Drop option using LWC.

https://www.salesforcetutorial.com/field-dependency-in-salesforce/

https://lovesalesforceyes.blogspot.com/2020/12/sorting-table-rows-by-drag-and-drop-in.html

It would be helpful if you share the code so expert advice what is the issue in it.

If this solution helps you, Please mark it as best answer.

Thanks,
 
Faizan Nusrat 27Faizan Nusrat 27

Hi Praveen,

Code is as below

HTML:-

<template>
    <div class="slds-grid slds-wrap">
        <div class="slds-col slds-size_4-of-4">
            <div>
                <lightning-input variant="label-hidden" label="Search Record" value={searchKey} type="search" onchange={handleChange} placeholder="Search Driver here"></lightning-input>
            </div>
        </div>
    </div>
    <div class="slds-grid slds-grid_vertical">
        <div class="slds-scrollable slds-grow">
            <div class="slds-scrollable_none">
            <!--lightning-card title="Allocate Orders to Drivers" icon-name="custom:custom14" -->
                    <table class=" slds-table slds-no-row-hover slds-table_bordered slds-table_fixed-layout slds-table_resizable-cols" role="grid" >
                        <thead>
                            <tr class="slds-line-height_reset">
                                <th>Order Number</th>
                                <th>Customer Name</th>
                                <th>Area</th>
                                <th>Slot of Delivery</th>
                                <th>Delivery Type</th>
                            </tr>
                        </thead>
                        <tbody>
                            <template for:each={selectedContactRows} for:item="ord" for:index="index">
                                <tr
                        key={ord.Id}
                        onchange={Change}
                        draggable="true"
                        ondrop={Drop}
                        ondragstart={DragStart}
                        ondragover={DragOver}
                        title={index}
                        ><!--tr key={ord.Id} onchange={handleChange} draggable="true" ondrop={Drop} ondragstart={DragStart} ondragover={DragOver} title={index}-->
                                    <td role="gridcell">
                                        <div class="slds-cell-wrap" title={index}>{ord.OrderNumber}</div>
                                    </td>
                                    <td role="gridcell">
                                        <div class="slds-cell-wrap" title="Client Name">{ord.Customer_Name__c}</div>
                                    </td>
                                    <td role="gridcell">
                                        <div class="slds-cell-wrap" title="Status">{ord.BillingCity}</div>
                                    </td>
                                    <td role="gridcell">
                                        <div class="slds-cell-wrap" title="Zone">{ord.Delivery_Day_Slot__c}</div>
                                    </td>
                                    <td role="gridcell">
                                    <div class="slds-cell-wrap" title="Zone">{ord.Order_Delivery_Method__c}</div>
                                </td>
                                </tr>
                            </template>
                        </tbody>
                    </table>                    
                    <div slot="footer">
                        <lightning-button title="Update" label="Allocate Selected Orders" variant="brand" icon-name="utility:update" icon-position="right" type="button" onclick={updateOrderRowAction}></lightning-button>
                    </div>
            </div>
        </div>
    </div>
</template>


-------------------
JS:-

import { LightningElement,api,track } from 'lwc';
import fetchOrderRecord from '@salesforce/apex/lwcAppOrderApex.fetchOrderRecord';
import findRecords from '@salesforce/apex/CustomLookupController.findRecords';
import updateMultipleOrderRecord from '@salesforce/apex/lwcAppOrderApex.updateMultipleOrderRecord';
export default class DragNDrop extends LightningElement {
    @track records;
    @track error;
    @track selectedRecord;
    @api index;
    @api relationshipfield;
    @api iconname = "standard:user";
    @api objectName = 'User';
    @api searchfield = 'Name';
    //@api displayFields = 'Name';
    @track searchKey;
    @api selectedContactRows=[];
    DragStart(event) {
        this.dragStart = event.target.title;
        console.log(event.target.title);
        event.target.classList.add("drag");
        }
   
        DragOver(event) {
        event.preventDefault();
        return false;
        }
       
        Drop(event) {
        event.stopPropagation();
        const DragValName = this.dragStart;
        const DropValName = event.target.title;
        console.log(DragValName);
        console.log(DropValName);
        if (DragValName === DropValName) {
          console.log('equal');
          return false;
        }
        const index = DropValName;
        const currentIndex = DragValName;
        const newIndex = DropValName;
        console.log(index);
        console.log(currentIndex);
        console.log(newIndex);
        //Array.prototype.move = function (from, to) {
          //this.splice(to, 0, this.splice(from, 1)[0]);
        //};
        this.selectedContactRows.move(currentIndex, newIndex);
        //console.log(this.ElementList);
       
       
        }
        updateOrderRowAction(){
            debugger;
            console.log('----this.selectedContactIdList---'+JSON.stringify(this.selectedContactRows));
                updateMultipleOrderRecord({conObj: JSON.stringify(this.selectedContactRows)})        
                .then(()=>{
                    this.template.querySelector('lightning-datatable').selectedContactRows=[];
       
                   
                        /*const toastEvent = new ShowToastEvent({
                        title:'Success!',
                        message:'Record updated successfully',
                        variant:'success'
                      });
                      this.dispatchEvent(toastEvent);*/
                   
                      //eval("$A.get('e.force:refreshView').fire();");
                      //return refreshApex(this.getAccList);
                     
                //eval("$A.get('e.force:refreshView').fire();");
           
                })
                .catch(error =>{
                    this.errorMsg =error;
                    window.console.log('unable to update the record due to ' + JSON.stringify(this.errorMsg));
                });
               
            }
           
    handleChange(event){
        debugger;
        /* eslint-disable no-console */
        console.log('Search Event Started '+searchKey);
        const searchKey = event.target.value;
        /* eslint-disable no-console */
        event.preventDefault();
        const searchEvent = new CustomEvent(
            'change',
            {
                detail : searchKey
            }
        );
        console.log('----22222222--- '+searchKey);
        this.dispatchEvent(searchEvent);
    }
    handleOnchange(event){
        //event.preventDefault();
        const searchKey = event.detail.value;
        //this.records = null;
        /* eslint-disable no-console */
        //console.log(searchKey);
        /* Call the Salesforce Apex class method to find the Records */
        findRecords({
            searchKey : searchKey,
            objectName : this.objectName,
            searchField : this.searchfield
        })
        .then(result => {
            this.records = result;
            for(let i=0; i < this.records.length; i++){
                const rec = this.records[i];
                this.records[i].Name = rec[this.searchfield];
            }
            this.error = undefined;
            //console.log(' records ', this.records);
        })
        .catch(error => {
            this.error = error;
            this.records = undefined;
        });
    }
}