- Framework7 Tutorial
- Framework7 - Home
- Framework7 - Overview
- Framework7 - Environment
- Framework7 Components
- Framework7 - Layouts
- Framework7 - Navbars
- Framework7 - Toolbars
- Framework7 - Search Bar
- Framework7 - Status Bar
- Framework7 - Side Panels
- Framework7 - Content Block
- Framework7 - Layout Grid
- Framework7 - Overlays
- Framework7 - Preloaders
- Framework7 - Progress Bar
- Framework7 - List Views
- Framework7 - Accordion
- Framework7 - Cards
- Framework7 - Chips
- Framework7 - Buttons
- Framework7 - Action Button
- Framework7 - Forms
- Framework7 - Tabs
- Framework7 - Swiper Slider
- Framework7 - Photo Browser
- Framework7 - Autocomplete
- Framework7 - Picker
- Framework7 - Calendar
- Framework7 - Refresh
- Framework7 - Infinite Scroll
- Framework7 - Messages
- Framework7 - Message Bar
- Framework7 - Notifications
- Framework7 - Lazy Load
- Framework7 Styling
- Framework7 - Color Themes
- Framework7 - Hairlines
- Framework7 Templates
- Framework7 - Templates Overview
- Framework7 - Auto Compilation
- Framework7 - Template7 Pages
- Framework7 Fast Clicks
- Framework7 - Active State
- Framework7 - Tap Hold Event
- Framework7 - Touch Ripple
- Framework7 Useful Resources
- Framework7 - Quick Guide
- Framework7 - Useful Resources
- Framework7 - Discussion
Framework7 - Virtual List
Description
Virtual list is a type of list view, which includes large number of data elements without dropping their performance. You can create HTML layout of virtual list by using the virtual-list class along with the list-block class.
Initialize Virtual List
You can initialize the virtual list by using the following method −
myApp.virtualList(listBlockContainer, parameters)
The method contains the following parameters −
listBlockContainer − It is a required HTML or string element for the list block container.
parameters − It is a required object, which includes virtual list parameters.
Virtual List Parameters
The following table provides a list of virtual parameters −
S.No | Parameter & Description | Type | Default |
---|---|---|---|
1 | items Provides a list of array items. |
array | - |
2 | rowsBefore It defines the number of rows to be displayed before scrolling the screen position. |
number | - |
3 | rowsAfter It defines the number of rows to be displayed after scrolling the screen position. |
number | - |
4 | cols It specifies the number of items per row. |
number | 1 |
5 | height This parameter returns item height in px. |
number or function (item) | 44 |
6 | template It represents the single item. |
string or function | - |
7 | renderItem It uses custom function to display the item HTML. |
function (index, item) | - |
8 | dynamicHeightBufferSize It specifies the buffer size on virtual list along with the dynamic height. |
number | 1 |
9 | cache You can enable or disable the DOM cache for list of items. |
boolean | true |
10 | updatableScroll It updates the device and manipulates the scroll events when you scroll the page. |
boolean | - |
11 | showFilteredItemsOnly It displays the filtered items by using the filter() method. |
boolean | false |
12 | searchByItem It is used to search the item by using search bar and uses search query, item index and item itself as parameters. |
function (query, index, item) | - |
13 | searchAll It is used to search all items by using search bar and uses search query and array of items as parameters. |
function (query, items) | - |
14 | onItemBeforeInsert It executes the callback function before inserting item to virtual document fragment. |
function (list, item) | |
15 | onBeforeClear It executes the callback function before removing DOM list and replaced with new document fragment. |
function (list, item) | |
16 | onItemsBeforeInsert It executes the callback function after removing DOM list and before inserting new document fragment. |
function (list, item) | |
17 | onItemsAfterInsert It executes the callback function after inserting the items with new document fragment. |
function (list, item) |
Virtual List Properties
S.No | Property & Description |
---|---|
1 | myList.items It displays the array with items. |
2 | myList.filteredItems It displays the array with filtered items. |
3 | myList.domCache It represents the items with DOM cache. |
4 | myList.params It displays the parameters that are passed on initialization. |
5 | myList.listBlock It specifies the list block container of DOM7 instance. |
6 | myList.pageContent It specifies the page content container of DOM7 instance. |
7 | myList.currentFromIndex It displays the first rendered item. |
8 | myList.currentToIndex It displays the last rendered item. |
9 | myList.reachEnd It displays the last item of all specified items if it is true. |
Virtual List Methods
S.No | Method & Description |
---|---|
1 | myList.filterItems(indexes); You can filter the items by using array with indexes. |
2 | myList.resetFilter(); Displays all the items by preventing the filter. |
3 | myList.appendItem(item); It appends the items to a virtual list. |
4 | myList.appendItems(items); It appends the array of items to a virtual list. |
5 | myList.prependItem(item); It prepends the items to a virtual list. |
6 | myList.prependItems(items); It prepends the array of items to a virtual list. |
7 | myList.replaceItem(index, items); It replaces the item with new item at the specified index. |
8 | myList.replaceAllItems(items); It replaces all the items with the new items. |
9 | myList.moveItem(oldIndex, newIndex); It transfers items from old to new index. |
10 | myList.insertItemBefore(index, item); You can insert the item before the specified index. |
11 | myList.deleteItem(index); You can delete the item at the specified index. |
12 | myList.deleteItems(indexes); You can delete the items at the specified array of indexes. |
13 | myList.deleteAllItems(); It deletes all the items. |
14 | myList.clearCache(); It is used to clear the cache of DOM elements. |
15 | myList.destroy(); It destroys the virtual list and its events. |
16 | myList.update(); It updates the virtual list and re-renders the virtual list. |
17 | myList.scrollToItem(index); You can scroll the virtual list to item by using the index number. |
Templating
Sometimes you need to filter or load the items from JSON data using some logic. To do this, you can pass array with data objects using items parameter and template parameter or using renderItem parameter.
You can use Framework7 template parameter as follows −
var myVal = myApp.virtualList('.list-block.virtual-list', { //List of array items items: [ { name: 'Element 1', image: '/path/image1.jpg' }, { name: 'Element 2', image: '/path/image2.jpg' }, // ... { name: 'Element 50', image: '/path/image50.jpg' }, ], // Display the each item using Template7 template parameter template: '<li class = "item-content">' + '<div class = "item-media"><img src = "{{image}}"></div>' + '<div class = "item-inner">' + '<div class = "item-title">{{name}}</div>' + '</div>' + '</li>' });
You can also use custom render function as shown below −
var myVal = myApp.virtualList('.list-block.virtual-list', { //List of array items items: [ { name: 'Element 1', image: '/path/image1.jpg' }, { name: 'Element 2', image: '/path/image2.jpg' }, // ... { name: 'Element 50', image: '/path/image50.jpg' }, ], // Display the each item using custom render function renderItem: '<li class = "item-content">' + '<div class = "item-media"><img src = "{{image}}"></div>' + '<div class = "item-inner">' + '<div class = "item-title">{{name}}</div>' + '</div>' + '</li>' });
Using With Search Bar
You can use searchAll or searchByItem parameters in order to use the search bar with virtual list that supply the search function in parameters.
var myVal = myApp.virtualList('.list-block.virtual-list', { //List of array items items: [ { name: 'Element 1', image: '/path/image1.jpg' }, { name: 'Element 2', image: '/path/image2.jpg' }, // ... { name: 'Element 50', image: '/path/image50.jpg' }, ], //Here you can searches all items in array and send back array with matched items searchAll: function (query, items) { var myItems = []; for (var i = 0; i < items.length; i++) { // Here check if name contains query string if (items[i].name.indexOf(query.trim()) >= 0) myItems.push(i); } // Returns array with indexes of matched items return myItems; } });
Using searchByItem parameter −
var myVal = myApp.virtualList('.list-block.virtual-list', { //List of array items items: [ { name: 'Element 1', image: '/path/image1.jpg' }, { name: 'Element 2', image: '/path/image2.jpg' }, // ... { name: 'Element 50', image: '/path/image50.jpg' }, ], //Here you can searches all items in array and send back array with matched items searchByItem: function (query, index, items) { // Here check if name contains query string if (items[i].name.indexOf(query.trim()) >= 0){ return true; } else { return false; } } } });
Dynamic Height
You can use dynamic height for the items by using the height parameter instead of a number.
var myVal = myApp.virtualList('.list-block.virtual-list', { //List of array items items: [ { name: 'Element 1', image: '/path/image1.jpg' }, { name: 'Element 2', image: '/path/image2.jpg' }, // ... { name: 'Element 50', image: '/path/image50.jpg' }, ], //template parameter template: '...', //using height function height: function (item) { if (item.image) return 120; //display the image with 100px height else return 50; //display the image with 50px height } });
API Methods
You can use the API methods to add, remove, replace or move virtual list items.
//Here you can initialize the list var myVal = myApp.virtualList('.list-block.virtual-list', { //List of array items items: [ { name: 'Element 1', image: '/path/image1.jpg' }, { name: 'Element 2', image: '/path/image2.jpg' }, // ... { name: 'Element 50', image: '/path/image50.jpg' }, ], //template parameter template: '...', }); //Here append your item myVal.appendItem({ image: 'Element 55' }); // You can append multiple items when clicking on button $('.button.append-items').on('click', function () { //You can append multiple items by passing array with items myVal.appendItem ([ { image: 'Element 60' }, { image: 'Element 61' }, { image: 'Element 62' } ]); }); //replace the first item myList.replaceItem(0, { name: 'Element 4' }); //you can display first 10 items when clicking on button $('.button.show-first-10').on('click', function () { //Passing array with indexes to show items myList.filter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); }); //Reset the filter $('.button.reset-filter').on('click', function () { myList.resetFilter(); }); //You can insert the item before 4th and 5th item myList.insertItemBefore(1, { name: 'Element 4.5' });