当我们是用可编辑表格JeditTable时,type为FormTypes.select,遇到这么个问题!
原因: 当设置options: this.instrumentOptions 时,此时先加载表格再加载this.instrumentOptions的值;所以加载出来的表格里下拉菜单的值为空;
在表格加载完成后,this.instrumentOptions的已经有值了;但此时表格已经加载完成不会动态刷新这个值;
columns: [
{
title: '生产仪器', key: 'instrumentId', width: '30%', align: "center", type: FormTypes.select, options: this.instrumentOptions, placeholder: '请选择${title}'
}
]
目前我试了两种方案:
一、用type:FormTypes.slot 插槽代替 FormTypes.select;
在solt中插入下拉菜单select模块!
columns: [
{
title: '生产模块', key: 'moduleId', width: '30%', align: "center", type: FormTypes.slot, slotName: 'action'
},
]
<template v-slot:action="props">
<a-select placeholder="选择生产模块" showSearch optionFilterProp="children" :dropdownMatchSelectWidth="false">
<a-select-option v-for="(item, index) in moduleOptions" :key="index" :value="item.id">
{{ item.name }}
</a-select-option>
</a-select>
</template>
这样可以产生同样的效果,而且下拉菜单的值可以实现动态刷新;
***后来发现slot插槽取不到下拉菜单的value,这个问题没有解决掉!!!***
二、使用localStroage或Vue.ls插件存储在本地;
但不推荐这种方法、数据是存储在浏览器缓存中的、一旦缓存清除该数据就没了!
我这里使用 options: Vue.ls.get(INSTRUMENTOPTIONS) 来实现的
columns: [
{
title: '生产仪器', key: 'instrumentId', width: '30%', align: "center", type: FormTypes.select, options: Vue.ls.get(INSTRUMENTOPTIONS), placeholder: '请选择${title}'
},]
这样在加载表格时,直接从本地拿数据,就不会出现空下拉菜单了。
我最终的解决方法:
type为FormTypes.select时下拉菜单没有值,终究是options赋值的时候没有值为空;我输出了table,查看了动态赋值和手动赋值两种的区别;
赋动态值
type: FormTypes.select ,options:this.instrumentOptions
赋静态值
type: FormTypes.select, options: [{ 'text': '父亲', 'value': '父亲', 'disabled': false },{ 'text': '母亲', 'value': '母亲', 'disabled': false }]
前者为空:
后者有选项:
在获取到tables后,就想到了一种简单粗暴的方法;
在第一次加载后,直接在created里手动赋值:
先获取tables实例,是一个数组;后从后端获取集合并转换为options数组;然后把options写进对应table的对应位置;
created() {
this.loadOptionsData()
},
methods: {
// 获取所有的editableTable实例
getAllTable() {
return Promise.all([
getRefPromise(this, 'editablePlanTable'),
getRefPromise(this, 'editableTaskTable')
])
},
loadOptionsData() {
getAction(this.url.getAllInstrument).then((res) => {
if (res) {
this.instrumentOptions =this.thransferOptions(res.data)
// this.instrumentList = res.data
}
})
},
thransferOptions(list) {
let Options = []
list.forEach(element => {
let option = { text: '', value: '' }
option.text = element.name
option.value = element.id.toString()
option.disabled = false
Options.push(option)
});
this.getAllTable().then(editableTables => {
//将转换后的options手动写给可编辑表格中
//editableTables[0]为第一个表格 columns[0]为该表格的第一个列
editableTables[0].$options.propsData.columns[0].options = this.instrumentOptions
})
return Options;
},
}