<el-dialog
title="banner排序"
:visible.sync="sortDialogVisible"
width="30%"
>
<el-table
:data="gridSortData"
border
height="300"
>
<el-table-column property="title" label="分類名稱" />
<el-table-column property="activityStatus" label="banner狀態(tài)">
<template slot-scope="scope">
// 這是自己封裝的組件
<dict-tag
:options="dict.type.sass_banner_status"
:value="scope.row.activityStatus"
/>
</template>
</el-table-column>
<el-table-column property="sort" label="當(dāng)前排序">
<template #default="{row,$index}">
<el-select
:value="row.sort"
class="select-spec"
@change="sortChange($event,row,$index)"
>
<el-option
v-for="item in bannerSortOptions"
:key="item.value"
:label="item.label"
:value="item.value"
:disabled="item.disabled"
/>
</el-select>
</template>
</el-table-column>
</el-table>
<div class="sort-number">總共{{ sortOptions.length }}條數(shù)據(jù)</div>
<span slot="footer" class="dialog-footer">
<el-button @click="sortDialogVisible = false">取 消</el-button>
<el-button type="primary" @click="onSortConfirm">確 定</el-button>
</span>
</el-dialog>
data(){
sortOptions: [], // 排序選項(xiàng)
bannerSortOptions: [] // 過濾排序選項(xiàng)
},
methods:{
// 獲取banner排序列表
async headSortClick() {
this.sortDialogVisible = true
this.loading = true
await bannerSortList().then(({ code, data }) => {
if (code === 0) {
this.gridSortData = data
this.sortOptions = data
this.sortOptions.map((item, index) => {
item.label = index + 1
item.value = index + 1
})
const temp = JSON.parse(JSON.stringify(this.sortOptions))
const res = this.filterArr(this.sortOptions, temp)
this.bannerSortOptions = res
this.loading = false
}
})
.catch(() => {
this.loading = false
})
},
// 過濾
filterArr(a, b) {
const sortSet = new Set()
for (const item of a) {
sortSet.add(item.sort * 1)
}
// 遍歷數(shù)組b,根據(jù)id是否在sortSet中設(shè)置disabled屬性
for (const item of b) {
if (sortSet.has(item.value)) {
item.disabled = true
} else {
item.disabled = false
}
}
return b
},
// 修改排序
sortChange($event, row) {
this.sortBannerId = row.id
this.sortId = row.sort = $event
for (var i = 0; i < this.sortArray.length; i++) {
if (this.sortArray[i].bannerId == this.sortBannerId) {
this.sortArray[i].bannerId = this.sortBannerId
this.sortArray[i].sort = parseInt(this.sortId)
return
}
}
this.sortArray.push({
bannerId: this.sortBannerId,
sort: parseInt(this.sortId)
})
// 這里是修改當(dāng)前行生效的,你可以忽略 ok
// this.sortOptions.filter((item, index) => index + 1 == this.sortId).map(item => {
// item.disabled = true
// })
},
}