Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ More usages or getting start you can refer to these clearly [examples](https://g

| **             Prop             ** | **Type** | **Description** |
|------------------|---------------|---------------------------------------------------------------------------------------------------------------------------------------------------|
| `data-key` | String | The unique key get from `data-sources` in each data object, its value **must be unique** in `data-sources`, it is used for identifying item size. |
| `data-key` | String\|Function | The unique key get from `data-sources` in each data object, or generated by the attributes in each data object of `data-sources`. Its value **must be unique** in `data-sources`, it is used for identifying item size. |
| `data-sources` | Array[Object] | The source array built for list, each array data must be an object and has an unique key for `data-key` property. |
| `data-component` | Component | The render item component created / declared by vue, and it will use the data object in `datas-sources` as render prop and named: `source`. |

Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ const VirtualList = Vue.component('virtual-list', {
},

getUniqueIdFromDataSources () {
return this.dataSources.map((dataSource) => dataSource[this.dataKey])
return this.dataSources.map((dataSource) => {
return typeof this.dataKey === 'function' ? this.dataKey(dataSource) : dataSource[this.dataKey]
})
},

// event called when each item mounted or size changed
Expand Down Expand Up @@ -267,15 +269,16 @@ const VirtualList = Vue.component('virtual-list', {
for (let index = start; index <= end; index++) {
const dataSource = dataSources[index]
if (dataSource) {
if (Object.prototype.hasOwnProperty.call(dataSource, dataKey)) {
const uniqueKey = typeof dataKey === 'function' ? dataKey(dataSource) : dataSource[dataKey]
if (uniqueKey) {
slots.push(h(Item, {
// key: dataSource[dataKey],
props: {
index,
tag: itemTag,
event: EVENT_TYPE.ITEM,
horizontal: isHorizontal,
uniqueKey: dataSource[dataKey],
uniqueKey: uniqueKey,
source: dataSource,
extraProps: extraProps,
component: dataComponent,
Expand Down
2 changes: 1 addition & 1 deletion src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

export const VirtualProps = {
dataKey: {
type: String,
type: [String, Function],
required: true
},
dataSources: {
Expand Down