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. ...
  12. </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>.

Disable Sorting

If we need to disable sorting on some specific items we can add no-sorting or disallow-sorting class to those items:

  1. <!-- Additional "sortable" class on list block -->
  2. <div class="list sortable">
  3. <!-- disable sorting for this item -->
  4. <li class="no-sorting">...</li>
  5. <!-- sortable items -->
  6. <li>...</li>
  7. <li>...</li>
  8. </div>

Note, that it makes sense and work only on first or last items in the list and won’t work correctly if you disable sortable for some of the items in the middle.

Opposite Sortable

If we need to render sortable handler on opposite side (e.g. on left in LTR direction) we need to add sortable-opposite side to sortable list:

  1. <!-- Additional "sortable-opposite" class on sortable list -->
  2. <div class="list sortable sortable-opposite">
  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. ...
  12. </div>

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
moveElementsbooleantrue

When 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.

It is also possible to overwrite this behavior for specific lists by adding data-sortable-move-elements=”true” or data-sortable-move-elements=”false” attribute to list element.

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 and el property with HTML sorted element

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, dataEvent will be triggered after user release currently sorting element in new position. data is an object with from and to properties with from/to index numbers of sorted list item el property with HTML sorted element

Sortable On Tap Hold

It is possible to bypass sortable enable/disable logic and make list items sortable on item tap and hold (long press). To make it work, we just need additional sortable-tap-hold class on sortable list:

  1. <!-- additional "sortable-tap-hold" class on sortable list -->
  2. <div class="list sortable sortable-tap-hold">
  3. ...
  4. </div>

In this case app will rely on custom taphold event. To make it work correctly, make sure you have also enabled touch.tapHold: true app parameter

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

CSS Variables

Below is the list of related CSS variables (CSS custom properties).

  1. :root {
  2. --f7-sortable-handler-color: rgba(0, 0, 0, 0.22);
  3. --f7-sortable-sorting-item-bg-color: rgba(255, 255, 255, 0.8);
  4. }
  5. :root .theme-dark,
  6. :root.theme-dark {
  7. --f7-sortable-sorting-item-bg-color: rgba(50, 50, 50, 0.8);
  8. --f7-sortable-handler-color: rgba(255, 255, 255, 0.55);
  9. }
  10. .ios {
  11. --f7-sortable-handler-width: 36px;
  12. --f7-sortable-sorting-item-box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.6);
  13. }
  14. .md {
  15. --f7-sortable-handler-width: 42px;
  16. --f7-sortable-sorting-item-box-shadow: var(--f7-elevation-2);
  17. }
  18. .aurora {
  19. --f7-sortable-handler-width: 32px;
  20. --f7-sortable-sorting-item-box-shadow: var(--f7-elevation-2);
  21. }

Examples

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-bg"></div>
  4. <div class="navbar-inner">
  5. <div class="left"></div>
  6. <div class="title">Sortable</div>
  7. <div class="right">
  8. <!-- Link to toggle sortable -->
  9. <a class="link sortable-toggle" data-sortable=".sortable">Toggle</a>
  10. </div>
  11. </div>
  12. </div>
  13. <div class="page-content">
  14. <div class="list sortable">
  15. <ul>
  16. <li>
  17. <div class="item-content">
  18. <div class="item-media"><i class="icon icon-f7"></i></div>
  19. <div class="item-inner">
  20. <div class="item-title">1 Jenna Smith</div>
  21. <div class="item-after">CEO</div>
  22. </div>
  23. </div>
  24. <div class="sortable-handler"></div>
  25. </li>
  26. <li>
  27. <div class="item-content">
  28. <div class="item-media"><i class="icon icon-f7"></i></div>
  29. <div class="item-inner">
  30. <div class="item-title">2 John Doe</div>
  31. <div class="item-after">Director</div>
  32. </div>
  33. </div>
  34. <div class="sortable-handler"></div>
  35. </li>
  36. ...
  37. </ul>
  38. </div>
  39. <div class="list media-list sortable">
  40. <ul>
  41. <li>
  42. <div class="item-content">
  43. <div class="item-media"><img src="https://cdn.framework7.io/placeholder/people-160x160-1.jpg" width="80"/></div>
  44. <div class="item-inner">
  45. <div class="item-title-row">
  46. <div class="item-title">Yellow Submarine</div>
  47. <div class="item-after">$15</div>
  48. </div>
  49. <div class="item-subtitle">Beatles</div>
  50. <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>
  51. </div>
  52. </div>
  53. <div class="sortable-handler"></div>
  54. </li>
  55. ...
  56. </ul>
  57. </div>
  58. <div class="block-title">Opposite Side</div>
  59. <div class="list sortable sortable-opposite">
  60. <ul>
  61. <li>
  62. <div class="item-content">
  63. <div class="item-media"><i class="icon icon-f7"></i></div>
  64. <div class="item-inner">
  65. <div class="item-title">1 Jenna Smith</div>
  66. <div class="item-after">CEO</div>
  67. </div>
  68. </div>
  69. <div class="sortable-handler"></div>
  70. </li>
  71. ...
  72. </ul>
  73. </div>
  74. </div>
  75. </div>
  1. var app = new Framework7();
  2. app.on('sortableSort', function (listEl, indexes) {
  3. console.log(indexes);
  4. })

← Smart Select

Statusbar →