add(cascader): filter

This commit is contained in:
songwanli2025@163.com 2019-12-18 15:58:26 +08:00
parent cd42f85153
commit fe5acc6a62
7 changed files with 153 additions and 9 deletions

View File

@ -0,0 +1,112 @@
# Filter
```html
<n-cascader
v-model="value1"
placeholder="Filterable Multiple (Leaf Only)"
:options="options"
filterable
:filter="filter1"
test="1111"
multiple
style="margin-bottom:10px;"
/>
<n-cascader
v-model="value2"
placeholder="Filterable Multiple"
:options="options"
filterable
:filter="filter2"
multiple
:leaf-only="false"
style="margin-bottom:10px;"
/>
<n-cascader
v-model="value3"
placeholder="Filterable Single (Leaf Only)"
:options="options"
filterable
:filter="filter3"
style="margin-bottom:10px;"
/>
<n-cascader
v-model="value4"
placeholder="Filterable Single"
:options="options"
filterable
:filter="filter4"
:leaf-only="false"
/>
```
```js
function genOptions (depth = 3, iterator = 1, prefix = '') {
const length = 12
const options = []
for (let i = 1; i <= length; ++i) {
if (iterator === 1) {
options.push({
value: `${i}`,
label: `${i}`,
disabled: i % 5 === 0,
children: genOptions(depth, iterator + 1, '' + i)
})
} else if (iterator === depth) {
options.push({
value: `${prefix}-${i}`,
label: `${prefix}-${i}`,
disabled: i % 5 === 0
})
} else {
options.push({
value: `${prefix}-${i}`,
label: `${prefix}-${i}`,
disabled: i % 5 === 0,
children: genOptions(depth, iterator + 1, `${prefix}-${i}`)
})
}
}
return options
}
export default {
data () {
return {
value1: null,
options: genOptions(),
value2: null,
value3: null,
value4: null,
}
},
methods: {
filter1 (pattern, option, path) {
if (option.label.indexOf(pattern + '-1')>-1) {
return true
} else {
return false
}
},
filter2 (pattern, option, path) {
if (path[0].label===pattern) {
return true
} else {
return false
}
},
filter3 (pattern, option, path) {
if (option.label.indexOf(pattern + '-1')>-1) {
return true
} else {
return false
}
},
filter4 (pattern, option, path) {
if (path[0].label===pattern) {
return true
} else {
return false
}
}
}
}
```

View File

@ -13,4 +13,5 @@ single-leaf-only-lazy
multiple-leaf-only-lazy
single-lazy
multiple-lazy
filter
```

View File

@ -5,6 +5,7 @@
placeholder="Please Select Something"
:options="options"
filterable
test="1111"
multiple
/>
```
@ -45,6 +46,6 @@ export default {
value: null,
options: genOptions()
}
}
},
}
```

View File

@ -5,14 +5,12 @@
type="date"
:date-disabled = "dateDisabled"
/>
{{timestamp1}}
<n-date-picker
v-model="timestamp2"
type="datetime"
:date-disabled = "dateDisabled"
:timeDisabled= "timeDisabled"
/>
{{timestamp2}}
<n-date-picker
v-model="timestamp3"
type="daterange"
@ -30,9 +28,9 @@ export default {
data () {
return {
timestamp1: 1576339200000,
timestamp2: 1576339200000,
timestamp3: [1576339200000, 1576339200000],
timestamp4: [1576585320000,1576585320000],
timestamp2: null,
timestamp3: null,
timestamp4: null,
}
},
methods: {

View File

@ -62,6 +62,7 @@
:enable-all-options="enableAllOptions"
:pattern="pattern"
:filterable="filterable"
:filter="filter"
:expand-trigger="expandTrigger"
:active-id.sync="activeId"
:lazy="lazy"
@ -163,6 +164,14 @@ export default {
splitor: {
type: String,
default: ' / '
},
filter: {
type: [String, Function],
default: null
},
test: {
type: String,
default: null
}
},
data () {
@ -259,6 +268,9 @@ export default {
})
}
},
mounted () {
console.log('mounted', this.filter, this.test)
},
methods: {
openMenu () {
if (!this.disabled) {

View File

@ -58,6 +58,10 @@ export default {
onLoad: {
type: Function,
default: () => {}
},
filter: {
type: Function,
default: null
}
},
data () {

View File

@ -134,6 +134,10 @@ export default {
onLoad: {
type: Function,
default: () => {}
},
filter: {
type: [String, Function],
default: null
}
},
data () {
@ -193,9 +197,21 @@ export default {
const filteredSelectOptions = []
const type = this.type
traverseWithCallback(this.menuOptions, option => {
if (Array.isArray(option.path) && option.path.some(
label => ~label.indexOf(this.pattern)
)) {
let flag = false
if (this.filter && option.label) {
let path = option.path.map((item) => {
return {
label: item,
value: item
}
})
flag = this.filter(this.pattern, { label: option.label, value: option.value }, path)
} else {
flag = option.path.some(
label => ~label.indexOf(this.pattern)
)
}
if (Array.isArray(option.path) && flag) {
if (type === 'multiple' || type === 'single') {
// console.log()
if (option.isLeaf) {