Framework7 - 虛擬列表



描述

虛擬列表是一種列表檢視,包含大量資料元素,而不會降低其效能。您可以使用virtual-list 類以及list-block 類建立虛擬列表的 HTML 佈局。

初始化虛擬列表

您可以使用以下方法初始化虛擬列表:

myApp.virtualList(listBlockContainer, parameters)

該方法包含以下引數:

  • listBlockContainer - 它是列表塊容器的必需 HTML 或字串元素。

  • parameters - 它是必需的物件,其中包含虛擬列表引數。

虛擬列表引數

下表提供了虛擬引數列表:

序號 引數及描述 型別 預設值
1

items

提供陣列項列表。

陣列 -
2

rowsBefore

它定義了在滾動螢幕位置之前要顯示的行數。

數字 -
3

rowsAfter

它定義了在滾動螢幕位置之後要顯示的行數。

數字 -
4

cols

它指定每行的專案數。

數字 1
5

height

此引數以 px 為單位返回專案高度。

數字或函式 (item) 44
6

template

它表示單個專案。

字串或函式 -
7

renderItem

它使用自定義函式來顯示專案 HTML。

函式 (index, item) -
8

dynamicHeightBufferSize

它在虛擬列表以及動態高度上指定緩衝區大小。

數字 1
9

cache

您可以啟用或停用專案列表的 DOM 快取。

布林值 true
10

updatableScroll

當您滾動頁面時,它會更新裝置並操作滾動事件。

布林值 -
11

showFilteredItemsOnly

它使用 filter() 方法顯示過濾後的專案。

布林值 false
12

searchByItem

它用於使用搜索欄搜尋專案,並使用搜索查詢、專案索引和專案本身作為引數。

函式 (query, index, item) -
13

searchAll

它用於使用搜索欄搜尋所有專案,並使用搜索查詢和專案陣列作為引數。

函式 (query, items) -
14

onItemBeforeInsert

在將專案插入虛擬文件片段之前執行回撥函式。

函式 (list, item)
15

onBeforeClear

在刪除 DOM 列表並替換為新文件片段之前執行回撥函式。

函式 (list, item)
16

onItemsBeforeInsert

在刪除 DOM 列表並在插入新文件片段之前執行回撥函式。

函式 (list, item)
17

onItemsAfterInsert

在使用新文件片段插入專案後執行回撥函式。

函式 (list, item)

虛擬列表屬性

序號 屬性及描述
1

myList.items

它顯示包含專案的陣列。

2

myList.filteredItems

它顯示包含過濾後項目的陣列。

3

myList.domCache

它表示具有 DOM 快取的專案。

4

myList.params

它顯示初始化時傳遞的引數。

5

myList.listBlock

它指定 DOM7 例項的列表塊容器。

6

myList.pageContent

它指定 DOM7 例項的頁面內容容器。

7

myList.currentFromIndex

它顯示第一個渲染的專案。

8

myList.currentToIndex

它顯示最後一個渲染的專案。

9

myList.reachEnd

如果為真,則顯示所有指定專案中的最後一項。

虛擬列表方法

序號 方法及描述
1

myList.filterItems(indexes);

您可以使用包含索引的陣列過濾專案。

2

myList.resetFilter();

透過阻止過濾器顯示所有專案。

3

myList.appendItem(item);

它將專案附加到虛擬列表。

4

myList.appendItems(items);

它將專案陣列附加到虛擬列表。

5

myList.prependItem(item);

它將專案前置到虛擬列表。

6

myList.prependItems(items);

它將專案陣列前置到虛擬列表。

7

myList.replaceItem(index, items);

它用新專案替換指定索引處的專案。

8

myList.replaceAllItems(items);

它用新專案替換所有專案。

9

myList.moveItem(oldIndex, newIndex);

它將專案從舊索引轉移到新索引。

10

myList.insertItemBefore(index, item);

您可以在指定索引之前插入專案。

11

myList.deleteItem(index);

您可以在指定索引處刪除專案。

12

myList.deleteItems(indexes);

您可以在指定的索引陣列中刪除專案。

13

myList.deleteAllItems();

它刪除所有專案。

14

myList.clearCache();

它用於清除 DOM 元素的快取。

15

myList.destroy();

它銷燬虛擬列表及其事件。

16

myList.update();

它更新虛擬列表並重新渲染虛擬列表。

17

myList.scrollToItem(index);

您可以使用索引號將虛擬列表滾動到專案。

模板

有時您需要使用某些邏輯從 JSON 資料中過濾或載入專案。為此,您可以使用items 引數和template 引數或使用renderItem 引數傳遞包含資料物件的陣列。

您可以如下使用 Framework7 template 引數:

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>'
});

您還可以使用如下所示的自定義渲染函式:

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>'
});

與搜尋欄一起使用

您可以使用searchAllsearchByItem 引數以便將搜尋欄與虛擬列表一起使用,並在引數中提供搜尋函式。

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;
   }
});

使用searchByItem 引數:

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;
         }
      }
   }
});

動態高度

您可以使用height 引數代替數字來為專案使用動態高度。

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 方法

您可以使用 API 方法新增、刪除、替換或移動虛擬列表專案。

//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'
});
framework7_list_views.htm
廣告

© . All rights reserved.