sortable-list icon indicating copy to clipboard operation
sortable-list copied to clipboard

Order of elements not updating on change items

Open timur560 opened this issue 8 years ago • 4 comments

Code:

<sortable-list class="items-sortable" on-sort-finish="_onSortFinish" dragging="{{dragging}}"
          sortable=".item" items="{{someItemsSorted}}">
  <template is="dom-repeat" items="[[itemsUpdatableFromApi]]">
    <div class="item"> ....
  ...
...

This issue is that when itemsUpdatableFromApi variable is changing, and the order of them changed, the order of items in component keep not changed. However, the data in items is updating correctly.

To clarify the issue I will describe steps:

  1. Items in sortable-list:
<div id="51">1</div>
<div id="52">2</div>
<div id="53">3</div>
  1. Move item with id 53 up:
<div id="51">1</div>
<div id="53">3</div>
<div id="52">2</div>
  1. Send API request on on-sort-finish to change the priorities (sort order)
  2. Update the items (in code above we just change the property itemsUpdatableFromApi with data, fetched from API)
<div id="51">1</div>
<div id="52">3</div>
<div id="53">2</div>

So, the id is changed and this is the correct data, but the order of displaying should be changed too. Because from API I receive items correctly ordered by priority (1,2,3)). But when I refresh the page, the order of items is correct:

<div id="51">1</div>
<div id="53">2</div>
<div id="52">3</div>

timur560 avatar Sep 07 '17 11:09 timur560

it would be useful if the sort-finish event included the indices of the swapped elements, then the consumer would have an easy way to update the array used by a dom-repeat;

robrez avatar Sep 13 '17 19:09 robrez

+1 Same issue here

RoXuS avatar Oct 05 '17 17:10 RoXuS

@timur560 I made a fork which avoid the dom changes, so the issue is solved (only in this case of we use an API to apply the sort). See https://github.com/RoXuS/sortable-list/commit/555c68c3d83b46c7ab8eb1db33829f55057738d7.

On sort-finish event you have to apply the change on the array by using the new properties items from event ->

  _onSortFinish(event) {
    const myArray = [];
    event.detail.items.forEach((element, elementIndex) => {
...
  }

RoXuS avatar Oct 05 '17 20:10 RoXuS

This has been driving me up the wall (too). @RoXuS 's fork has shown me the way, but unfortunately I can't use it directly because it includes several modifications to the styling. I was going to add a PR, but it seems there are several already that perhaps attempt the same thing, so I'll just add my fork here in case it is useful (same as RoXuS', but reverting some changes): https://github.com/davidmaxwaterman/sortable-list

davidmaxwaterman avatar May 23 '18 22:05 davidmaxwaterman