Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behaviour when not removing from data array #94

Open
SntTGR opened this issue Oct 23, 2023 · 0 comments
Open

Unexpected behaviour when not removing from data array #94

SntTGR opened this issue Oct 23, 2023 · 0 comments

Comments

@SntTGR
Copy link

SntTGR commented Oct 23, 2023

Hi! Im not very familiar with sortablejs

const baseTasks : Array<{ id: Task['id'] }> = [ {id: '1'}, {id: '2'}, {id: '3'} ]
const taskList : Ref<Array<{id: Task['id'], removed?: boolean}>> = ref([]);

Im using the following add/remove handlers without removing the task from the list

function listOnAdd(event: SortableEvent): void {    
    const taskId = event.item.getAttribute('task-id');
    if (typeof taskId !== 'string') throw new Error('Invalid Task Id!');
    
    event.item.remove();
    taskList.value.push({id: taskId});
}

function listOnRemove(event: SortableEvent): void {
    const taskId = event.item.getAttribute('task-id');
    if (typeof taskId !== 'string') throw new Error('Invalid Task Id!');
    
    const taskIndex = taskList.value.findIndex(i => i.id === taskId);
    if (taskIndex === -1) throw new Error('Task not found!');
    
    taskList.value[taskIndex] = {id: taskId, removed: true}; // only mark them as 'removed'
    // taskList.value.splice(taskIndex, 1);
}

function listOnReset() {
    taskList.value = [...baseTasks];
}
 <button @click="listOnReset">reset</button>

 <Sortable
    ref="sortableElement"
    class="task-list"
    :list="taskList"
    item-key="id"
    :options="options"
    @add=     "listOnAdd"
    @remove=  "listOnRemove"
  >
    <template #item="{element}">
      <Task :task-id="element.id"/>
    </template>
  </Sortable>

The elements that are moved between categories, when reset, dont seem to be added again.
https://stackblitz.com/edit/recursive-template-refs-5ip6ao?file=src%2Fcomponents%2FTaskList.vue

23-10-2023-46

Is this correct behaviour? I'm a bit surprised that the elements are not behaving as a clone list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant