Skip to content

Sortable shows new added item not at the end but in between #6

@git-no

Description

@git-no

Thank you very much for the great work and Svelte implementation.

Issue

Resorting by drag and drop does not work correct after an additional item is added.
The item the user drops at position A, sortable puts it to position A-1.

Situation

  • There is a data array = [1,2,3]
  • User resorts to [2,3,1]
  • An additional item is added to the array at last position [2,3,1,4]
  • But Sortable shows [2,3,4,1]

Goal

The goal ist to save the sorted data (not html) to database. Therefor a array of data is used.

Steps to Reproduce

  1. Create a svelte page and add the code below.
  2. Run Svelte
  3. Drag Item 1 below Item 3
  4. Add a item to the array by clicking on the Add button.
<script>
	import { SortableList } from '@jhubbardsf/svelte-sortablejs';

	let data = ['Item 1', 'Item 2', 'Item 3'];
	let sortableListData = [...data];

	$: {
		console.log(`data: ${data}`);
	}

	const addItem = () => {
		const item = `Item ${data.length + 1}`;
		data = [...data, item];
		sortableListData = [...data];
	};

	const move = (arr, from, to) => {
		const input = [...arr];
		let numberOfDeletedElm = 1;

		const elm = input.splice(from, numberOfDeletedElm)[0];
		numberOfDeletedElm = 0;
		input.splice(to, numberOfDeletedElm, elm);
		data = [...input];
	};

	const handleEnd = (evt) => {
		move(data, evt.oldIndex, evt.newIndex);
		// data = [...data];
	};
</script>

<div class="margin-auto ">
	<button class="button" on:click={addItem}>Add</button>

	<SortableList class="list-group col" animation={150} ghostClass="bg-info" onEnd={handleEnd}>
		{#each sortableListData as item}
			<div class="list-group-item">
				{item}
			</div>
		{/each}
	</SortableList>
</div>

<style>
	.margin-auto {
		margin: auto;
	}
	.list-group.col {
		background-color: blue;
		color: white;
	}
	.list-group-item {
		background-color: gray;
		color: white;
		margin: 20px 0;
		padding: 8px 8px;
	}
	.button {
		padding: 8px;
		background-color: green;
		color: white;
	}
</style>

Thank you in advanced for your response.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions