3

I am trying to implement a drag and drop functionality in lwc and I am getting the below code:

<template for:each={leftTask} for:item="task">
    <div class="draggable" draggable="true" key={task.taskId} ondragstart={handleDragStart(task.taskId)}>{task.name}</div>
</template>

However, it seems that lwc doesn't allow passing parameters directly into handle functions. So the above code won't work. I am wondering is it possible to pass any parameter into js handler at all? Or do I have to make a separate child component in order to handle this?

6

You can read data directly from the target:

<div data-id={task.taskId} class="draggable" draggable="true" key={task.taskId} 
  ondragstart={handleDragStart}>{task.name}</div>

function handleDragStart(event) {
    var taskId = event.target.dataset.id;
    // ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.