- Framework7 教程
- Framework7 - 首页
- Framework7 - 概览
- Framework7 - 环境
- Framework7 组件
- Framework7 - 布局
- Framework7 - 导航栏
- Framework7 - 工具栏
- Framework7 - 搜索栏
- Framework7 - 状态栏
- Framework7 - 侧边栏
- Framework7 - 内容块
- Framework7 - 布局网格
- Framework7 - 覆盖层
- Framework7 - 预加载器
- Framework7 - 进度条
- Framework7 - 列表视图
- Framework7 - 手风琴
- Framework7 - 卡片
- Framework7 - 芯片
- Framework7 - 按钮
- Framework7 - 操作按钮
- Framework7 - 表单
- Framework7 - 标签页
- Framework7 - Swiper 滑块
- Framework7 - 照片浏览器
- Framework7 - 自动完成
- Framework7 - 选择器
- Framework7 - 日历
- Framework7 - 刷新
- Framework7 - 无限滚动
- Framework7 - 消息
- Framework7 - 消息栏
- Framework7 - 通知
- Framework7 - 懒加载
- Framework7 样式
- Framework7 - 色彩主题
- Framework7 - 分割线
- Framework7 模板
- Framework7 - 模板概览
- Framework7 - 自动编译
- Framework7 - Template7 页面
- Framework7 快速点击
- Framework7 - 活动状态
- Framework7 - 长按事件
- Framework7 - 触摸波纹
- Framework7 有用资源
- Framework7 - 快速指南
- Framework7 - 有用资源
- Framework7 - 讨论
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>'
});
与搜索栏一起使用
您可以使用searchAll 或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
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'
});