vue3+typeScript穿梭框的實(shí)現(xiàn)示例
前言
實(shí)現(xiàn)功能:模仿element穿梭框的簡單功能
每周分享一個(gè)vue3+typeScript的小組件,我只想分享下自己的實(shí)現(xiàn)思路,樓主是個(gè)菜雞前端,記錄下實(shí)現(xiàn)過程,說不定對(duì)你有幫助。
效果展示

開發(fā)過程
思路:用兩個(gè)數(shù)組分別記錄左右框框里面的值,根據(jù)復(fù)選框選中狀態(tài)來實(shí)現(xiàn)刪除增加即可
html部分
<div class="shuttle">
<!-- 左邊列表 -->
<div class="shuttle-box">
<div class="shuttle-box-title">
<div>列表一</div>
<div class="index-num">{{itemLeft.length}}</div>
</div>
<div class="shuttle-box-list">
<div class="shuttle-box-item" v-for="(vo,inx) in itemLeft" :key="inx">
<input type="checkbox" :value="inx" v-model="checkLeft" :disabled="vo.disabled" /> {{vo.label}}
</div>
</div>
</div>
<!-- 左右操作按鈕 -->
<div class="shuttle-click">
<span @click="goLeft">←</span>
<span @click="goRight">→</span>
</div>
<!-- 右邊列表 -->
<div class="shuttle-box">
<div class="shuttle-box-title">
<div>列表二</div>
<div class="index-num">{{itemRight.length}}</div>
</div>
<div class="shuttle-box-list">
<div class="shuttle-box-item" v-for="(vo,inx) in itemRight" :key="inx">
<input type="checkbox" :value="inx" v-model="checkRight" :disabled="vo.disabled" /> {{vo.label}}
</div>
</div>
</div>
</div>
ts部分
<script lang="ts">
import {
defineComponent,
reactive,
toRefs
} from 'vue'
export default defineComponent({
setup() {
const data = reactive({
itemLeft: [{
label: '列表1的第一條數(shù)據(jù)',
disabled: true,
}, {
label: '列表1的第二條數(shù)據(jù)',
disabled: false,
}],
itemRight: [{
label: '列表2的第一條數(shù)據(jù)',
disabled: false,
}, {
label: '列表2的第二條數(shù)據(jù)',
disabled: false,
}],
checkLeft: [],
checkRight: [],
goLeft: () => {
//數(shù)組排序
data.checkRight.sort(data.sortIndex);
data.checkRight.forEach((item) => {
//將itemRight對(duì)應(yīng)索引的數(shù)據(jù)移動(dòng)到左邊去
data.itemLeft.push(data.itemRight[item]);
//移除
data.itemRight.splice(item, 1);
});
//清空
data.checkLeft = [];
data.checkRight = [];
},
goRight: () => {
//數(shù)組排序
data.checkLeft.sort(data.sortIndex);
data.checkLeft.forEach((item) => {
//將itemLeft對(duì)應(yīng)索引的數(shù)據(jù)移動(dòng)到右邊去
data.itemRight.push(data.itemLeft[item]);
//移除
data.itemLeft.splice(item, 1);
});
//清空
data.checkLeft = [];
data.checkRight = [];
},
//checkbox是綁定的是的數(shù)組的索引,所以checkbox的點(diǎn)擊的順序不同的話索引的順序是不同的,這樣刪除有可能找不到會(huì)報(bào)錯(cuò),排個(gè)序從大到小刪除就可以
//這個(gè)是排序參數(shù)
sortIndex: (a, b) => {
return b - a;
}
})
return {
...toRefs(data),
}
}
})
</script>
css部分
.shuttle {
width: 800px;
padding: 50px 0;
display: flex;
justify-content: space-between;
//整個(gè)穿梭框
.shuttle-box {
width: 300px;
height: 500px;
border: 1px solid #ddd;
//標(biāo)題
.shuttle-box-title {
background: #f5f7fa;
padding: 0 20px;
height: 40px;
line-height: 40px;
display: flex;
justify-content: space-between;
.index-num {
color: #909399;
font-size: 12px;
font-weight: 400;
}
}
//列表
.shuttle-box-list {
padding: 20px;
//一個(gè)列表item
.shuttle-box-item {
line-height: 2.0;
}
}
}
//左右穿梭按鈕
.shuttle-click {
padding-top: 60px;
cursor: pointer;
span {
padding: 5px 10px;
display: inline-block;
background: #409eff;
color: #ffffff;
margin: 0 5px;
text-align: center;
}
}
}
到此這篇關(guān)于vue3+typeScript穿梭框的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)vue3+typeScript穿梭框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中代碼傳送(teleport)的實(shí)現(xiàn)
本文主要介紹了Vue中代碼傳送(teleport)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
vue props對(duì)象validator自定義函數(shù)實(shí)例
今天小編就為大家分享一篇vue props對(duì)象validator自定義函數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
詳解vue服務(wù)端渲染瀏覽器端緩存(keep-alive)
在使用服務(wù)器端渲染時(shí),除了服務(wù)端的接口緩存、頁面緩存、組建緩存等,瀏覽器端也避免不了要使用緩存,減少頁面的重繪。這篇文章主要介紹了詳解vue服務(wù)端渲染瀏覽器端緩存(keep-alive),感興趣的小伙伴們可以參考一下2018-10-10
基于Vue實(shí)現(xiàn)一個(gè)"蛇形"步驟條
在現(xiàn)代Web應(yīng)用中,步驟條作為一種常見的UI組件,廣泛應(yīng)用于表單提交、任務(wù)進(jìn)度以及多步驟操作等場景,下面我們來看看如何利用Vue實(shí)現(xiàn)一個(gè)蛇形步驟條吧2024-11-11
vue項(xiàng)目實(shí)現(xiàn)會(huì)議預(yù)約功能(包含某天的某個(gè)時(shí)間段和某月的某幾天)
這篇文章主要介紹了vue項(xiàng)目實(shí)現(xiàn)會(huì)議預(yù)約功能(包含某天的某個(gè)時(shí)間段和某月的某幾天),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02

