利用promise.all和Table组件的@on-selection-change结合实现批量删除
<tables
ref=“manage_table”
:loading=“t_loading”
editable
v-model=“tableData”
:columns=“columns”
@on-selection-change=‘onSelectionChange’
/>
// 选择Table选项触发事件
onSelectionChange (row) {
console.log(row)
this.currentId = []
for (let i = 0; i < row.length; i++) {
this.currentId[i] = row[i].id
}
console.log(this.currentId)
}
// 点击删除触发事件
onBanConfirm () {
let rm = []
for (let i = 0; i < this.currentId.length; i++) {
// remove函数为一个Promise对象,调用后端接口实现删除
rm.push(remove(this.currentId[i]))
}
Promise.all(rm)
.then(res => {
for (let i = 0; i < this.currentId.length; i++) {
if (res[i].data.code !== 0) {
this.$Message.error(‘系统错误’)
// 初始data中定义rm_result为true,只有返回错误的值才会更改为false
this.rm_result = false
break
}
}
if (this.rm_result) {
this.$Message.info(‘删除成功’)
// 重新获取数据刷新Table
this.getData()
// 关闭删除模态框
this.ban_show = false
}
})
.catch(err => {
console.log(err)
this.$Message.error(‘系统错误’)
})
},
利用Modal的on-visible-change事件使用一个模态框实现增加和修改
// 利用modal_show实现模态框的显示和关闭
<Modal
v-model=“modal_show”
title=“csdn增改”
width=“800”
@on-visible-change=“modalShow”
modalShow (status) {
console.log(status)
if (!status) {
// clearForm为清空模态框输入数据的函数,免得下次点开数据混淆
this.clearForm()
// editStatus判断是否为搜索状态,可以帮助增加和修改不同接口的调用
this.editStatus = false
// 清空一些无法通过清空数据清空的输入框
this.$refs.select.clearSingleSelect()
}
},
补充:
假如同时使用Table组件实现修改功能,修改是数据最好使用深拷贝
{
title: ‘操作’,
align: ‘center’,
key: ‘action’,
width: 125,
minWidth: 125,
render: (h, params) => {
return h(‘div’, [
h(
‘Button’,
{
props: {
type: ‘primary’,
size: ‘small’
},
style: ‘margin-right:5px;’,
on: {
click: () => {
this.newsForm = JSON.parse(JSON.stringify(params.row))
this.add_show = true
this.editStatus = true
this.index = params.row.id
console.log(this.newsForm)
}
}
},
‘修改’
)])
}
}
利用select的label-in-value实现两个输入框的绑定
<Select
v-model=“index”
placeholder=“请选择学校”
filterable
label-in-value
@on-change=“match1”
v-for=“item in universityList”
:label=“item.universityname”
:value=“item.id”
:key=“item.id”
match1 (item) {
if (item) {
this.newsForm.universityid = item.value
this.newsForm.universityname = item.label
}
}
补充:
假如只是实现增加,而无需实现修改,则不需要使用v-model
v-model此处的作用在于修改时将此选项框的选项赋值
利用select的远程搜索实现输入
// 主要通过:remote-method实现远程搜索
<Select
ref=“select”
v-model=“name”
filterable
clearable
remote
:remote-method=“remoteMethod1”
:loading=“select_loading”
@on-clear=“clear”
v-for=“option in options”
:value=“option.name”
:key=“option.id”
{{ option.name }}</Option
clear () {
this.name = ‘’
this.options = []
},
remoteMethod1 (query) {
if (query !== ‘’) {
this.select_loading = true
searchList(1, 10, query).then(res => {
this.select_loading = false
this.options = res.data.data.data
console.log(res.data.data)
})
} else {
this.options = []
}
}
select的远程搜索结合实现两个输入框的绑定以及增改
// 主要通过:remote-method实现远程搜索
<Select
ref=“select”
v-model=“index”
filterable
clearable
remote
:remote-method=“remoteMethod1”
:loading=“select_loading”
@on-change=“match1”
@on-clear=“clear”
v-for=“option in options”
:value=“option.companyName”
:key=“option.id”
{{ option.companyName }}</Option
match1 (item) {
console.log(item)
if (item) {
this.newsForm.companyName = item
console.log(item)
if (item !== ‘’) {
searchCompanyList(1, 1, item).then(res => {
this.newsForm.jobCompanyId = res.data.data.data[0].id
})
}
}
},
clear () {
this.newsForm.jobCompanyId = ‘’
this.options = []
},
remoteMethod1 (query) {
if (query !== ‘’) {
this.select_loading = true
searchCompanyList(1, 10, query).then(res => {
this.select_loading = false
this.options = res.data.data.data
console.log(res.data.data)
})
} else {
this.options = []
}
}
// 修改按钮的render函数
render: (h, params) => {
return h(‘div’, [
h(
‘Button’,
{
props: {
type: ‘primary’,
size: ‘small’
},
style: ‘margin-right:5px;’,
on: {
click: () => {
this.newsForm = JSON.parse(JSON.stringify(params.row))
this.add_show = true
this.editStatus = true