vue+el-select?多數(shù)據(jù)分頁(yè)搜索組件的實(shí)現(xiàn)
project.Vue
//子頁(yè)面1
<template>
<div>
<el-select :value="defaultValue" :loading="loading" :multiple="multiple" :placeholder="placeholder"
:allow-create="allowCreate" :isConcatShowText="isConcatShowText" filterable remote clearable default-first-option :remote-method="remoteMethod"
style="width: 100%;" @input="handleInput" @visible-change="visibleChange" @change="change"
@clear="clearChange">
<el-option v-for="(item, index) in optionsList" :key="'s' + index" :label="isConcatShowText==true?concatenatedLabel2(item):concatenatedLabel(item)"
:value="item[valueString]">
{{ concatenatedLabel(item) }}
</el-option>
<el-pagination class="select-pagination" @size-change="handleSizeChange"
@current-change="handleCurrentChange" :current-page.sync="localCurrentPage" :page-size="pageSize"
:total="total" layout="prev, pager, next, total"></el-pagination>
</el-select>
</div>
</template>
<script>
export default {
name: 'CustomSelect',
props: {
defaultValue: {
type: [Number, String, Array],
default: null
},
loading: {
type: Boolean,
default: false
},
拼接后 被選擇后輸入框是否顯示拼接的字符串
isConcatShowText:{
type: Boolean,
default: false
},
multiple: {
type: Boolean,
default: false
},
placeholder: {
type: String,
default: 'Please select'
},
allowCreate: {
type: Boolean,
default: false
},
remoteMethod: {
type: Function,
default: null
},
optionsList: {
type: Array,
default: () => []
},
label: {
type: String,
default: 'label'
},
labelTwo: {
type: String,
default: ''
},
labelThree: {
type: String,
default: ''
},
labelFour: {
type: String,
default: ''
},
valueString: {
type: String,
default: 'value'
},
currentPage: {
type: Number,
default: 1
},
pageSize: {
type: Number,
default: 10
},
total: {
type: Number,
default: 0
},
},
watch: {
// 監(jiān)聽(tīng) prop 的變化,并更新本地?cái)?shù)據(jù)屬性
currentPage(newValue) {
this.localCurrentPage = newValue;
}
},
data() {
return {
localCurrentPage: 1
}
},
methods: {
handleInput(value) {
this.$emit('input', value);
},
visibleChange(visible) {
this.$emit('visible-change', visible);
},
change(value) {
this.$emit('change', value);
},
clearChange() {
this.$emit('clear');
},
handleSizeChange(size) {
this.$emit('size-change', size);
},
handleCurrentChange(page) {
this.$emit('current-change', page);
},
concatenatedLabel(item) {
return [item[this.label], item[this.labelTwo], item[this.labelThree], item[this.labelFour]].filter(Boolean)
.join(' || ');
},
concatenatedLabel2(item) {
return item[this.label]
}
}
};
</script>
<style scoped>
.select-pagination {
margin-top: 10px;
}
</style>
projectParent.Vue
<template>
<!-- 所有的項(xiàng)目 也可以單獨(dú)用這個(gè)頁(yè)面 -->
<div>
<!-- is-concat 是否拼接字符串 concat-symbol拼接字符 allowCreate是否允許創(chuàng)建條目 默認(rèn)顯示多少條 是否多選 -->
<projectSelect :defaultValue="selectedValue" :loading="isLoading" :multiple="isMultiple"
:isConcatShowText="isConcatShowText" :placeholder="placeholder" :allowCreate="allowCreation"
:remoteMethod="remoteFetch" :optionsList="options" label="projectCode" labelTwo="name" labelThree=""
labelFour="" :valueString="valueString" :currentPage="currentPages" :pageSize="pageSize" :total="totalItems"
@input="handleInput" @visible-change="handleVisibleChange" @change="handleChange" @clear="handleClear"
@size-change="handleSizeChange" @current-change="handleCurrentChange" />
</div>
</template>
<script>
import projectSelect from './project.vue';
//管理員查看往來(lái)單位
import {
listInfo
} from "@/api/project/index.js"; //客戶
export default {
name: 'ParentComponent',
components: {
projectSelect
},
props: {
index: {
type: Number,
default: 0
},
placeholder: {
type: String,
default: '請(qǐng)選擇'
},
valueString: {
type: String,
default: 'id'
},
selectedVal: {
default: null
}
},
watch: {
selectedVal: {
handler(val, oldVal) {
this.selectedValue = val
},
immediate: true,//為要監(jiān)視的prop添加immediate屬性并設(shè)置為true,可以使得watch函數(shù)在組件創(chuàng)建時(shí)立即執(zhí)行一次
deep: true
}
},
data() {
return {
querySearch:null,
isConcatShowText: true, //拼接后 被選擇后輸入框是否顯示拼接的字符串
selectedValue: [], // 如果是多選,則為數(shù)組;否則為單個(gè)值
isLoading: false,
isMultiple: false, // 是否允許多選
allowCreation: false, // 是否允許用戶創(chuàng)建新選項(xiàng)
options: [], // 選項(xiàng)列表,應(yīng)從服務(wù)器或本地獲取
currentPages: 1, // 當(dāng)前頁(yè)碼
pageSize: 6, // 每頁(yè)顯示的數(shù)量
totalItems: 0, // 總項(xiàng)目數(shù),用于分頁(yè)
};
},
mounted() {
this.remoteFetch()
},
methods: {
remoteFetch(query) {
if(query){
this.querySearch=query
}
let that = this
// 遠(yuǎn)程獲取數(shù)據(jù)的函數(shù),根據(jù) query 參數(shù)進(jìn)行搜索
this.isLoading = true;
// 假設(shè) fetchData 是一個(gè)從服務(wù)器獲取數(shù)據(jù)的函數(shù)
listInfo({
isStop:0,//0否 1是 是否停用
search: this.querySearch,
pageSize: that.pageSize,
pageNum: that.currentPages
}).then(response => {
//每次進(jìn)入后 數(shù)據(jù)都是重新填充
if (response.rows.length < 1) {
this.selectedValue = []; // 清空已選值
}
that.options = response.rows;
that.totalItems = response.total;
this.isLoading = false;
});
},
handleInput(value) {
// 處理輸入事件,更新 selectedValue
this.selectedValue = value;
this.$emit('handleInput', value);
},
handleVisibleChange(visible) {
// 處理下拉菜單的可見(jiàn)性變化
this.currentPages = 1;
this.remoteFetch(); // 可能需要重新獲取當(dāng)前頁(yè)的數(shù)據(jù)
},
handleChange(value) {
this.querySearch=null
// 處理選項(xiàng)變化事件
this.$emit('handlePageChange', value);
},
handleClear() {
this.querySearch=null
// 處理清除事件
this.selectedValue = []; // 清空已選值
this.$emit('clear', this.index);
// this.$parent.productClear(this.index)
},
handleSizeChange(size) {
// 處理每頁(yè)顯示數(shù)量變化事件
this.pageSize = size;
this.remoteFetch(this.querySearch); // 可能需要重新獲取當(dāng)前頁(yè)的數(shù)據(jù)
},
handleCurrentChange(page) {
// 處理當(dāng)前頁(yè)碼變化事件
this.currentPages = page;
this.remoteFetch(this.querySearch); // 獲取當(dāng)前頁(yè)的數(shù)據(jù)
}
},
// 其他邏輯和生命周期鉤子...
};
</script>
<style scoped>
/* 父組件的樣式 */
</style>
最后在頁(yè)面引用(因?yàn)槲液芏囗?yè)面用到了,所以用了projectSelect 組件
<el-form>
<el-form-item label="項(xiàng)目" prop="projectName">
<project-select class="width100bfb" valueString="id" :selectedVal="form.projectName" @clear='projectClear()' @handlePageChange="projectChange($event)" placeholder="請(qǐng)選擇項(xiàng)目" />
</el-form-item>
</el-form>
<script>
import projectSelect from '@/views/components/elSelect/projectParent'
export default {
components: {
projectSelect
},
methods: {
/**項(xiàng)目列表change*/
projectChange(val) {
if (val) {
//根據(jù)自己情況去調(diào)用接口
//getProjectInfo(val).then(res => {
// this.form.projectName = res.data.name
//})
}
},
/*清除項(xiàng)目**/
projectClear() {
//根據(jù)自己情況去設(shè)置
//this.form.projectName = null
},
}
}
</script>到此這篇關(guān)于vue+el-select 多數(shù)據(jù)分頁(yè)搜索組件的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)el-select 分頁(yè)搜索內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
區(qū)分vue-router的hash和history模式
這篇文章主要介紹了區(qū)分vue-router的hash和history模式,幫助大家更好的理解和學(xué)習(xí)vue路由,感興趣的朋友可以了解下2020-10-10
基于axios請(qǐng)求封裝的vue應(yīng)用實(shí)例代碼
這篇文章主要給大家介紹了基于axios請(qǐng)求封裝的vue應(yīng)用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
VUE 項(xiàng)目在IE11白屏報(bào)錯(cuò) SCRIPT1002: 語(yǔ)法錯(cuò)誤的解決
這篇文章主要介紹了VUE 項(xiàng)目在IE11白屏報(bào)錯(cuò) SCRIPT1002: 語(yǔ)法錯(cuò)誤的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
構(gòu)建大型 Vue.js 項(xiàng)目的10條建議(小結(jié))
這篇文章主要介紹了構(gòu)建大型 Vue.js 項(xiàng)目的10條建議(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

