Sortable List - 图1

Sortable List

Sortable lists is an extension of List View that allows you to sort list view elements.

Sortable List Layout

Let’s look at layout structure of sortable lists in your list views:

  1. <!-- Additional "sortable" class on list block -->
  2. <div class="list sortable">
  3. <li>
  4. <div class="item-content">
  5. <div class="item-media">...</div>
  6. <div class="item-inner">...</div>
  7. </div>
  8. <!-- Sortable handler element -->
  9. <div class="sortable-handler"></div>
  10. </li>
  11. </div>

Where:

  • sortable-handler - “draggable” element (hidden by default) that allows you to sort current list view element

As you may see it is pretty simple, all you need is to add **sortable** class to list block and put **<div class="sortable-handler"></div>** as a direct child of <li>

Sortable App Methods

To enable/disable sorting mode on sortable list we need to use following app methods:

app.sortable.enable(listEl)- enable sorting mode on sortable list

  • listEl - HTMLElement or string (with CSS Selector). Sortable list block element.

app.sortable.disable(listEl)- disable sorting mode on sortable list

  • listEl - HTMLElement or string (with CSS Selector). Sortable list block element.

app.sortable.toggle(listEl)- toggle sorting mode on sortable list

  • listEl - HTMLElement or string (with CSS Selector). Sortable list block element.

Sortable App Parameters

It is possible to configure global sortable behavior on app initialisation by passing sortable related paremeters under sortable property.

ParameterTypeDefaultDescription
moveElementsbooleantrueWhen enabled then it will move (reorder) HTML elements according to new sortable order. It is useful to disable it if you use other library for DOM manipulation, like React or Vue

To change these parameters we need to pass them on app init under sortable property, for example:

  1. var app = new Framework7({
  2. sortable: {
  3. moveElements: false
  4. }
  5. });

Sortable Events

Sortable will fire the following DOM events on sortable list block element and events on app instance:

DOM Events

EventTargetDescription
sortable:enableSortable List<div class=”list sortable”>Event will be triggered when sortable mode is enabled
sortable:disableSortable List<div class=”list sortable”>Event will be triggered when sortable mode is disabled
sortable:sortList element<li>Event will be triggered after user release currently sorting element in new position. event.detail will contain object with from and to properties with from/to index numbers of sorted list item

App Instance Events

Sortable instance emit events on app instance.

EventTargetArgumentsDescription
sortableEnableapplistElEvent will be triggered when sortable mode is enabled
sortableDisableapplistElEvent will be triggered when sortable mode is disabled
sortableSortapplistEl, indexesEvent will be triggered after user release currently sorting element in new position. indexes is an object with from and to properties with from/to index numbers of sorted list item

Control Sortable With Links

It is possible to enable and disable sortable mode using special classes and data attributes on links:

  • To enable sortable mode we need to add **sortable-enable** class to any HTML element (prefered to link)

  • To disable sortable mode we need to add **sortable-disable** class to any HTML element (prefered to link)

  • To toggle sortable mode we need to add **sortable-toggle** class to any HTML element (prefered to link)

  • If you have more than one sortable list in app, you need to specify appropriate sortable list via additional **data-sortable=".sortable"** attribute on this HTML element with CSS selector of required sortable list

Examples

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-inner">
  4. <div class="left"></div>
  5. <div class="title">Sortable</div>
  6. <div class="right">
  7. <!-- Link to toggle sortable -->
  8. <a class="link sortable-toggle" data-sortable=".sortable">Toggle</a>
  9. </div>
  10. </div>
  11. </div>
  12. <div class="page-content">
  13. <div class="list sortable">
  14. <ul>
  15. <li>
  16. <div class="item-content">
  17. <div class="item-media"><i class="icon icon-f7"></i></div>
  18. <div class="item-inner">
  19. <div class="item-title">1 Jenna Smith</div>
  20. <div class="item-after">CEO</div>
  21. </div>
  22. </div>
  23. <div class="sortable-handler"></div>
  24. </li>
  25. <li>
  26. <div class="item-content">
  27. <div class="item-media"><i class="icon icon-f7"></i></div>
  28. <div class="item-inner">
  29. <div class="item-title">2 John Doe</div>
  30. <div class="item-after">Director</div>
  31. </div>
  32. </div>
  33. <div class="sortable-handler"></div>
  34. </li>
  35. ...
  36. </ul>
  37. </div>
  38. <div class="list media-list sortable">
  39. <ul>
  40. <li>
  41. <div class="item-content">
  42. <div class="item-media"><img src="http://lorempixel.com/160/160/people/1" width="80"/></div>
  43. <div class="item-inner">
  44. <div class="item-title-row">
  45. <div class="item-title">Yellow Submarine</div>
  46. <div class="item-after">$15</div>
  47. </div>
  48. <div class="item-subtitle">Beatles</div>
  49. <div class="item-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sagittis tellus ut turpis condimentum, ut dignissim lacus tincidunt. Cras dolor metus, ultrices condimentum sodales sit amet, pharetra sodales eros. Phasellus vel felis tellus. Mauris rutrum ligula nec dapibus feugiat. In vel dui laoreet, commodo augue id, pulvinar lacus.</div>
  50. </div>
  51. </div>
  52. <div class="sortable-handler"></div>
  53. </li>
  54. ...
  55. </ul>
  56. </div>
  57. </div>
  58. </div>
  1. var app = new Framework7();
  2. app.on('sortableSort', function (listEl, indexes) {
  3. console.log(indexes);
  4. })