一、效果圖
二、參數(shù)配置
1、代碼示例
<t-antd-select
v-model="selectVlaue"
:optionSource="stepList"
@change="selectChange"
/>
2、配置參數(shù)(Attributes)繼承 a-select Attributes
參數(shù) | 說(shuō)明 | 類型 | 默認(rèn)值 |
---|---|---|---|
v-model | 綁定值 | boolean / string / number/Array | - |
mode | 設(shè)置’multiple’'tags’多選 (顯示全選) | String | - |
optionSource | 下拉數(shù)據(jù)源 | Array | - |
width | select寬度(可以設(shè)置百分比或px) | String | 100% |
customLabel | 是否自定義設(shè)置下拉label | String | - |
valueKey | 傳入的 option 數(shù)組中,要作為最終選擇項(xiàng)的鍵值 key | String | ‘key’ |
labelKey | 傳入的 option 數(shù)組中,要作為顯示項(xiàng)的鍵值名稱 | String | ‘label’ |
isShowPagination | 是否顯示分頁(yè)(分頁(yè)不顯示全選框) | Boolean | false |
paginationOption | 分頁(yè)配置項(xiàng) | Object | - |
2-1、paginationOption配置參數(shù)(Attributes)繼承 a-pagination Attributes
參數(shù) | 說(shuō)明 | 類型 | 默認(rèn)值 |
---|---|---|---|
current | 當(dāng)前頁(yè)數(shù) | number | 1 |
pageSize | 每頁(yè)顯示條目個(gè)數(shù) | number | 6 |
total | 總條目數(shù) | number | 0 |
bind | 繼承a-pagination屬性 | Object | - |
3、繼承 a-select&&a-pagination events
事件名 | 說(shuō)明 | 返回值 |
---|---|---|
current-change | 當(dāng)前頁(yè)碼 | 當(dāng)前選中的頁(yè)碼 |
三、源碼
<template>
<div @mousedown="e => {
e.preventDefault()
selectOpen = true
}" ref="main">
<a-select
class="t_select"
v-model="childSelectedValue"
:style="{width: width||'100%'}"
:placeholder="placeholder"
:open="selectOpen"
@select="handleSelect"
v-bind="attrs"
v-on="$listeners"
:mode="mode"
>
<template v-for="(index, name) in $slots" v-slot:[name]>
<slot :name="name" />
</template>
<template v-for="(index, name) in $scopedSlots" v-slot:[name]="data">
<slot :name="name" v-bind="data"></slot>
</template>
<div slot="dropdownRender" slot-scope="menu">
<a-checkbox
v-if="mode&&!isShowPagination"
:checked="selectChecked"
@change="selectAll"
class="all_checkbox"
>全選</a-checkbox>
<v-nodes :vnodes="menu" />
<div class="t_select__pagination" v-if="isShowPagination">
<a-pagination
:page-size.sync="paginationOption.pageSize"
v-model="paginationOption.current"
:total="paginationOption.total"
@change="currentChange"
v-bind="{
size:'small',
'hide-on-single-page':true,
'showQuickJumper': true,
...$attrs,
...paginationOption.bind,
}"
v-on="$listeners"
/>
</div>
</div>
<a-select-option
v-for="(item,index) in optionSource"
:key="index"
:value="item[valueKey]"
>{{customLabel?customLabelHandler(item):item[labelKey]}}</a-select-option>
</a-select>
</div>
</template>
<script>
export default {
name: 'TAntdSelect',
components: {
VNodes: {
functional: true,
render: (h, ctx) => ctx.props.vnodes
}
},
props: {
value: {
type: [String, Number, Array, Boolean, Object]
},
// 多選 'multiple'
mode: {
type: String
},
placeholder: {
type: String,
default: '請(qǐng)選擇'
},
// 選擇框?qū)挾?/span>
width: {
type: String
},
// 是否自定義設(shè)置下拉label
customLabel: {
type: String
},
// 傳入的option數(shù)組中,要作為最終選擇項(xiàng)的鍵值key
valueKey: {
type: String,
default: 'key'
},
// 傳入的option數(shù)組中,要作為顯示項(xiàng)的鍵值名稱
labelKey: {
type: String,
default: 'label'
},
// 下拉框組件數(shù)據(jù)源
optionSource: {
type: Array
},
// 是否顯示分頁(yè)
isShowPagination: {
type: Boolean,
default: false
},
// 分頁(yè)配置項(xiàng)
paginationOption: {
type: Object,
default: () => {
return {
pageSize: 6, // 每頁(yè)顯示條數(shù)
current: 1, // 當(dāng)前頁(yè)
total: 0 // 總條數(shù)
}
}
}
},
data() {
return {
selectOpen: false
}
},
computed: {
childSelectedValue: {
get() {
return this.value || undefined
},
set(val) {
this.$emit('input', val)
}
},
attrs() {
return {
allowClear: true,
showSearch: true,
...this.$attrs
}
},
selectChecked: {
get() {
return this.childSelectedValue?.length === this.optionSource?.length
},
set(val) {
// console.log('set', val)
this.$emit('input', val)
}
}
},
mounted() {
document.addEventListener('click', this.bodyCloseMenus)
},
beforeDestroy() {
document.removeEventListener('click', this.bodyCloseMenus)
},
methods: {
// 點(diǎn)擊空白區(qū)域
bodyCloseMenus(e) {
if (this.$refs.main && !this.$refs.main.contains(e.target)) {
if (this.selectOpen == true) {
this.selectOpen = false
}
}
},
// 點(diǎn)擊全選
selectAll(val) {
const options = JSON.parse(JSON.stringify(this.optionSource))
if (val.target.checked) {
this.childSelectedValue = options?.map(item => {
return item[this.valueKey]
})
setTimeout(() => {
this.$emit('change', this.childSelectedValue)
}, 0)
} else {
this.childSelectedValue = null
}
this.selectOpen = false
},
handleSelect(value, option) {
if (value) {
this.selectOpen = false
}
this.$emit('select', value, option)
},
// 切換分頁(yè)
currentChange(val) {
// console.log('切換分頁(yè)', val)
if (!this.mode) {
this.childSelectedValue = null
}
setTimeout(() => {
this.selectOpen = true
}, 0)
this.$emit('current-change', val)
},
// 自定義label顯示
customLabelHandler(item) {
// eslint-disable-next-line no-eval
return eval(this.customLabel)
}
}
}
</script>
<style lang="scss">
.all_checkbox {
margin-left: 12px;
margin-top: 5px;
}
</style>
四、組件地址
gitHub組件地址
gitee碼云組件地址
五、相關(guān)文章
基于ElementUi再次封裝基礎(chǔ)組件文檔
基于ant-design-vue再次封裝基礎(chǔ)組件文檔文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-715903.html
vue3+ts基于Element-plus再次封裝基礎(chǔ)組件文檔文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-715903.html
到了這里,關(guān)于vue2+ant-design-vue a-select組件二次封裝(支持單選/多選添加全選/分頁(yè)(多選跨頁(yè)選中)/自定義label)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!