使用el-select的自定义模板,在<span>中添加两个按钮
<el-form-item label="处理方式:" prop="sortingList">
<el-select v-model="form.sortingList" multiple placeholder="请选择" style="width: 580px">
<el-option v-for="item in wayOptions" :key="item.operateRank" :label="item.name"
:value="item.operateRank">
<span style="float: left">{{ item.name }}</span>
<span style="float: right;color: var(--el-text-color-secondary);font-size: 13px;">
<div>
<span @click.stop="handleUp(item)">升级 |</span>
<span @click.stop="handleDown(item)"> 降级</span>
</div>
</span>
</el-option>
</el-select>
</el-form-item>
这里我需要拿到选择的元素的下标,确定他是第几位元素,从而和前一位或者后一位交换位置
const handleUp = (e: any) => {
let array: number[] = form.value.sortingList as number[]
for (let index = 0; index < array.length; index++) {
const element = array[index];
if (e.operateRank === element) {
changeUp(array, index)
}
}
}
const handleDown = (e: any) => {
let array: number[] = form.value.sortingList as number[]
for (let index = 0; index < array.length; index++) {
const element = array[index];
if (e.operateRank as number === element) {
changeDown(array, index)
break
}
}
}
//数组 换第几个元素 升级
const changeUp = (arr: number[], index: number) => {
let temp;
if (index === 0 || index > arr.length - 1) {
return arr
}
temp = arr[index]
arr[index] = arr[index - 1]
arr[index - 1] = temp
return arr
}
//数组 换第几个元素 降级
const changeDown = (arr: number[], index: number) => {
let temp;
if (index === arr.length - 1) {
return arr
}
console.log(arr);
temp = arr[index]
arr[index] = arr[index + 1]
arr[index + 1] = temp
return arr
}