Virtual List - 图1

Virtual List

Virtual List allows to render lists with huge amount of elements without loss of performance. And it is fully compatible with all Framework7 components which work with lists such as Search Bar, Infinite Scroll, Pull To Refresh, Swipeouts (swipe-to-delete) and Sortable.

Virtual List Layout

Virtual List HTML layout is pretty simple, it is almost the same as for usual List View with only difference: you need to leave it empty:

  1. <!-- Virtual List -->
  2. <div class="list virtual-list">
  3. <!-- keep it empty -->
  4. </div>

Where:

  • virtual-list - required additional class on any list block that uses Virtual List

Virtual List App Methods

Now, when we have list’s HTML, we need to initialize it. We need to use required App method:

app.virtualList.create(parameters)- initialize virtual list with parameters

  • parameters - object - object with virtual list parameters. Required.
  • Method returns initialized Virtual List instance

app.virtualList.destroy(el)- destroy Virtual List instance

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

app.virtualList.get(el)- get Virtual List instance by HTML element

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

Method returns Virtual List’s instance

Note that Virtual List container (list block element) should be in DOM on a moment of initialization.

Virtual List Parameters

Let’s look on list of all available parameters:

ParameterTypeDefaultDescription
elHTMLElement
string
Target List Block element. In case of string - CSS selector of list block element
ulHTMLElement
string
List element <ul> inside of List block.
createUlbooleantrueWill automatically create <ul> element inside of Virtual List block. If disabled, then virtual list can be used on any block element without ul > li structure
itemsarrayArray with list items
rowsBeforenumberAmount of rows (items) to be rendered before current screen scroll position. By default it is equal to double amount of rows (items) that fit to screen
rowsAfternumberAmount of rows (items) to be rendered after current screen scroll position. By default it is equal to the amount of rows (items) that fit to screen
colsnumber1Number of items per row. Doesn’t compatible when using Virtual List with dynamic height
heightnumber or function(item)If number - list item height in px. If function then function should return item height. By default equals to 44 for iOS theme, 48 for MD theme, 38 for Aurora theme.
itemTemplatestring
function
Template7 string template or Template7 compiled template that used to render single item. Template should contain full HTML layout for single item, including wrapping <li></li> tags
renderItemfunction(item)This optional function allows to use custom function to render item HTML. It could be used instead of template parameter
renderExternalfunction(renderParameters)This optional function allows to render DOM items using some custom method. Useful in case it is used (e.g.) with Vue/React plugin to pass DOM rendering and manipulation to Vue/React. renderParameters conaints object with the following properties: fromIndex, toIndex, listHeight, topPosition, items
emptyTemplatestringDefines list item template for the case if empty data passed
dynamicHeightBufferSizenumber1This parameter allows to control buffer size on Virtual Lists with dynamic height (when height parameter is function) as a buffer size multiplier
cachebooleantrueDisable or enable DOM cache for already rendered list items. In this case each item will be rendered only once and all futher manipulations will be with DOM element. It is useful if your list items have some user interaction elements (like form elements or swipe outs) or could be modified
updatableScrollbooleanIs the current device updates and handles scroll events during scroll. By default (if not specified) it is “false” for all iOS devices with iOS version less than 8.
setListHeightbooleantrueWill set height on list block if enabled
showFilteredItemsOnlybooleanfalseOption to show filtered items only set by method
scrollableParentElHTMLElement
string
Virtual list’s scrollable parent. If not specified, then it will look for parent <div class=”page-content”> element
Searchbar
searchByItemfunction(query, item, index)Search function that will be used by Searchbar, it receives search query, item itself and item index. If item matches to search query you need to return true, otherwise this function should return false
searchAllfunction(query, items)Search function that will be used by Searchbar, it receives search query and array with all items. You need to loop through items and return array with indexes of matched items

Virtual List Methods & Properties

So to create Virtual List we have to call:

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

After we initialize Virtual List we have its initialized instance in variable (like virtualList variable in example above) with helpful methods and properties:

Properties
virtualList.itemsArray with items
virtualList.filteredItemsArray with filtered items (after using “.filterItems” method)
virtualList.domCacheObject with cached dom items
virtualList.paramsParameters passed on list initialization
virtualList.elVirtual list target list block element
virtualList.$elDom7 instance of target list block element
virtualList.pageContentElParent “page-content” element
virtualList.$pageContentElDom7 instance of parent “page-content” element
virtualList.currentFromIndexIndex number of currently first rendered item
virtualList.currentToIndexIndex number of currently last rendered item
virtualList.reachEndBoolean property. Equals true if the currently last rendered item is the last item of all specified items
Methods
virtualList.filterItems(indexes);Filter virtual list by passing array with indexes of items to show
virtualList.resetFilter();Disable filter and display all items again
virtualList.appendItem(item);Append item to virtual list
virtualList.appendItems(items);Append array with items to virtual list
virtualList.prependItem(item);Prepend item to virtual list
virtualList.prependItems(items);Prepend array with items to virtual list
virtualList.replaceItem(index, item);Replace item at specified index with the new one
virtualList.replaceAllItems(items);Replace all items with arrays of new items
virtualList.moveItem(oldIndex, newIndex);Move virtual item from oldIndex to newIndex
virtualList.insertItemBefore(index, item);Insert new item before item with specified index
virtualList.deleteItem(index);Delete item at specified index
virtualList.deleteItems(indexes);Delete items at specified array of indexes
virtualList.deleteAllItems();Delete all items
virtualList.clearCache();Clear virtual list cached DOM elements
virtualList.destroy();Destory initialized virtual list and detach all events
virtualList.update();Update virtual list, including recalculation of list sizes and re-rendering of virtual list
virtualList.scrollToItem(index);Scroll Virtual List to specified item by its index number

Virtual List Events

Virtual List will fire the following events on app and virtual list instance:

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

EventTargetArgumentsDescription
itemBeforeInsertvirtualListvirtualList, itemEl, itemEvent will be triggered before item will be added to virtual document fragment
vlItemBeforeInsertapp
itemsBeforeInsertvirtualListvirtualList, fragmentEvent will be triggered after current DOM list will be removed and before new document will be inserted
vlItemsBeforeInsertapp
beforeClearvirtualListvirtualList, fragmentEvent will be triggered before current DOM list will be removed and replaced with new document fragment
vlBeforeClearapp
itemsAfterInsertvirtualListvirtualList, fragmentEvent will be triggered after new document fragment with items inserted
vlItemsAfterInsertapp

Examples

  1. <div class="page">
  2. <div class="navbar">
  3. <div class="navbar-bg"></div>
  4. <div class="navbar-inner sliding">
  5. <div class="title">Virtual List</div>
  6. <div class="subnavbar">
  7. <form data-search-container=".virtual-list" data-search-item="li" data-search-in=".item-title" class="searchbar searchbar-init">
  8. <div class="searchbar-inner">
  9. <div class="searchbar-input-wrap">
  10. <input type="search" placeholder="Search"/>
  11. <i class="searchbar-icon"></i>
  12. <span class="input-clear-button"></span>
  13. </div>
  14. <span class="searchbar-disable-button if-not-aurora">Cancel</span>
  15. </div>
  16. </form>
  17. </div>
  18. </div>
  19. </div>
  20. <div class="searchbar-backdrop"></div>
  21. <div class="page-content">
  22. <div class="list simple-list searchbar-not-found">
  23. <ul>
  24. <li>Nothing found</li>
  25. </ul>
  26. </div>
  27. <div class="list virtual-list media-list searchbar-found"></div>
  28. </div>
  29. </div>
  1. var app = new Framework7();
  2. // Dummy items array
  3. var items = [];
  4. for (var i = 1; i <= 10000; i++) {
  5. items.push({
  6. title: 'Item ' + i,
  7. subtitle: 'Subtitle ' + i
  8. });
  9. }
  10. var virtualList = app.virtualList.create({
  11. // List Element
  12. el: '.virtual-list',
  13. // Pass array with items
  14. items: items,
  15. // Custom search function for searchbar
  16. searchAll: function (query, items) {
  17. var found = [];
  18. for (var i = 0; i < items.length; i++) {
  19. if (items[i].title.toLowerCase().indexOf(query.toLowerCase()) >= 0 || query.trim() === '') found.push(i);
  20. }
  21. return found; //return array with mathced indexes
  22. },
  23. // List item Template7 template
  24. itemTemplate:
  25. '<li>' +
  26. '<a href="#" class="item-link item-content">' +
  27. '<div class="item-inner">' +
  28. '<div class="item-title-row">' +
  29. '<div class="item-title">{{title}}</div>' +
  30. '</div>' +
  31. '<div class="item-subtitle">{{subtitle}}</div>' +
  32. '</div>' +
  33. '</a>' +
  34. '</li>',
  35. // Item height
  36. height: app.theme === 'ios' ? 63 : (app.theme === 'md' ? 73 : 46),
  37. });

← View / Router

Framework7 Icons Cheatsheet →