List Index - 图1

List Index

List Index allows to access a particular section of the list view instantly without scrolling through each section.

List Index Layout

Single List Index layout is pretty simple:

  1. <div class="page">
  2. <div class="navbar">...</div>
  3. <!-- List Index element, must be a direct child of page -->
  4. <div class="list-index"></div>
  5. <!-- Scrollable page content -->
  6. <div class="page-content">...</div>
  7. </div>

List Index App Methods

Let’s look at related App methods to work with List Index:

app.listIndex.create(parameters)- create List Index instance

  • parameters - object. Object with list index parameters

Method returns created List Index’s instance

app.listIndex.destroy(el)- destroy List Index instance

  • el - HTMLElement or string (with CSS Selector) or object. List Index element or List Index instance to destroy.

app.listIndex.get(el)- get List Index instance by HTML element

  • el - HTMLElement or string (with CSS Selector). List Index element.

Method returns List Index’s instance

List Index Parameters

Now let’s look at list of available parameters we need to create List Index:

ParameterTypeDefaultDescription
elHTMLElement
string
List Index element. HTMLElement or string with CSS selector of list index element
listElHTMLElement
string
Related List View element. HTMLElement or string with CSS selector of List View element
indexesarray
string
autoArray with indexes. If not passed then it will automatically generate it based on item-divider and list-group-title elements inside of passed List View element in listEl parameter
scrollListbooleantrueWill automatically scroll related List View to the selected index
labelbooleanfalseEnables label bubble with selected index when you swipe over list index
iosItemHeightnumber14Single index item height. It is required to calculate dynamic index and how many indexes fit on the screen. For iOS theme
mdItemHeightnumber14Single index item height. It is required to calculate dynamic index and how many indexes fit on the screen. For MD theme
onobjectObject with events handlers. For example:
  1. var listIndex = app.listIndex.create({
  2. el: ‘.list-index’,
  3. on: {
  4. select: function () {
  5. console.log(‘Index selected’)
  6. },
  7. },
  8. })

List Index Methods & Properties

So to create List Index we have to call:

  1. var listIndex = app.listIndex.create({ /* parameters */ })

After that we have its initialized instance (like listIndex variable in example above) with useful methods and properties:

Properties
listIndex.appLink to global app instance
listIndex.elList index HTML element
listIndex.$elDom7 instance with list index HTML element
listIndex.ulDynamically created inner <ul> HTML element
listIndex.$ulDom7 instance with dynamically created inner <ul> HTML element
listIndex.listElRelated List HTML element, passed in listEl parameter
listIndex.$listElDom7 instance with related List HTML element, passed in listEl parameter
listIndex.indexesArray with calculated indexes
listIndex.paramsList index parameters
Methods
listIndex.update()Recalculates indexes, sizes and rerenders list index
listIndex.scrollToList(itemContent)Scrolls related list to specified index content
listIndex.destroy()Destroys list index instance
listIndex.on(event, handler)Add event handler
listIndex.once(event, handler)Add event handler that will be removed after it was fired
listIndex.off(event, handler)Remove event handler
listIndex.off(event)Remove all handlers for specified event
listIndex.emit(event, …args)Fire event on instance

List Index Events

List Index will fire the following DOM events on list index element and events on app and list index instance:

DOM Events

EventTargetDescription
listindex:selectList Index Element<div class=”list-index”>Event will be triggered on index select rather by click or swiping
listindex:clickList Index Element<div class=”list-index”>Event will be triggered on index click
listindex:beforedestroyList Index Element<div class=”list-index”>Event will be triggered right before List Index instance will be destroyed

App and List Index Instance Events

List Index instance emits events on both self instance and app instance. App instance events has same names prefixed with listIndex.

EventArgumentsTargetDescription
select(listIndex, itemContent)listIndexEvent will be triggered on index select rather by click or swiping. As an argument event handler receives list index instance and selected index item content
listIndexSelect(listIndex, itemContent)app
click(listIndex, itemContent)listIndexEvent will be triggered on index click. As an argument event handler receives list index instance and clicked index item content
listIndexClick(listIndex, itemContent)app
beforeDestroy(listIndex)listIndexEvent will be triggered right before List Index instance will be destroyed. As an argument event handler receives list index instance
listIndexBeforeDestroy(listIndex)app

List Index Auto Initialization

If you don’t need to use List Index API and your List Index is inside of the page and presented in DOM on moment of page initialization then it can be auto initialized with just adding additional list-index-init class:

  1. <!-- Add list-index-init class -->
  2. <div class="list-index list-index-init"></div>

In this case if you need to access created List Index instance you can use the app.listIndex.get app method:

  1. var listIndex = app.listIndex.get('.list-index');
  2. var indexes = listIndex.indexes;
  3. // do something with indexes

When using auto init you may need to pass additional parameters. you can set all available parameters via data- attributes on list index element:

  1. <div class="page">
  2. ...
  3. <!-- parameters set via data- attributes -->
  4. <div class="list-index"
  5. data-list-el=".contacts-list"
  6. data-label="true"
  7. data-indexes="auto"
  8. ></div>
  9. <div class="page-content">
  10. <div class="list contacts-list">
  11. ...
  12. </div>
  13. </div>
  14. </div>

Examples

  1. <div class="page">
  2. <div class="navbar">
  3. ...
  4. </div>
  5. <!-- List index element -->
  6. <div class="list-index"></div>
  7. <div class="page-content">
  8. <!-- Contacts list -->
  9. <div class="list simple-list contacts-list">
  10. <div class="list-group">
  11. <ul>
  12. <li class="list-group-title">A</li>
  13. <li>Aaron</li>
  14. <li>Adam</li>
  15. ...
  16. </ul>
  17. </div>
  18. <div class="list-group">
  19. <ul>
  20. <li class="list-group-title">B</li>
  21. <li>Benjamin</li>
  22. <li>Blake</li>
  23. <li>Bobby</li>
  24. </ul>
  25. </div>
  26. <div class="list-group">
  27. <ul>
  28. <li class="list-group-title">C</li>
  29. <li>Caleb</li>
  30. <li>Callum</li>
  31. ...
  32. </ul>
  33. </div>
  34. ...
  35. </div>
  36. </div>
  37. </div>
  1. var app = new Framework7();
  2. var listIndex = app.listIndex.create({
  3. // ".list-index" element
  4. el: '.list-index',
  5. // List el where to look indexes and scroll for
  6. listEl: '.contacts-list',
  7. // Generate indexes automatically based on ".list-group-title" and ".item-divider"
  8. indexes: 'auto',
  9. // Scroll list on indexes click and touchmove
  10. scrollList: true,
  11. // Enable bubble label when swiping over indexes
  12. label: true,
  13. });