<template>
<div class="main-box">
<div class="List">
<div class="ltitle title">
<input type="checkbox" v-model="lcheck" name="" id="list1" @click="lallChecked">
<label for="list1">List 1</label>
<span class="num1">{{lnum}}/{{lAllNum}}</span>
</div>
<ul class="eul">
<li v-for="jsonList in leftJsonArrayList">
<div><input class="check" @change="loneCheck" type="checkbox" v-model="jsonList.check"
:disabled="jsonList.disabled">{{jsonList.content}}</div>
</li>
</ul>
</div>
<div class="btn">
<button class="lBtn" @click="leftBtnCurr"><</button>
<button class="rBtn " @click="rightBtnCurr">></button>
</div>
<div class="List">
<div class="rtitle title">
<input type="checkbox" v-model="rcheck" @click="rallChecked" name="" id="list2">
<label for="list2">List 2</label>
<span class="num2">{{rnum}}/{{rAllNum}}</span>
</div>
<ul class="eul rul">
<li v-for="jsonList in rightJsonArrayList">
<div><input class="check" @change="roneCheck" type="checkbox"
v-model="jsonList.check">{{jsonList.content}}</div>
</li>
</ul>
</div>
</div>
</template>
<script setup>
import { ref, watch, onUpdated, nextTick, onMounted } from "vue";
let leftJsonArrayList = ref([
{ check: true, content: "Opsition1", disabled: false },
{ check: false, content: "Opsition2", disabled: true },
{ check: false, content: "Opsition3", disabled: false },
{ check: false, content: "Opsition4", disabled: false },
{ check: false, content: "Opsition5", disabled: false },
{ check: true, content: "Opsition6", disabled: false },
{ check: true, content: "Opsition7", disabled: false },
{ check: true, content: "Opsition8", disabled: false },
{ check: true, content: "Opsition9", disabled: false },
{ check: true, content: "Opsition10", disabled: false },
{ check: true, content: "Opsition11", disabled: false },
{ check: true, content: "Opsition12", disabled: false },
{ check: true, content: "Opsition13", disabled: false },
{ check: true, content: "Opsition14", disabled: false },
{ check: true, content: "Opsition15", disabled: false },
]);
let rightJsonArrayList = ref([]);
let lcheck = ref(false);
let lnum = ref(0);
let lAllNum = ref(leftJsonArrayList.value.length);
let rcheck = ref(false);
let rnum = ref(0);
let rAllNum = ref(rightJsonArrayList.value.length);
const rightBtnCurr = () => {
let selectArr = leftJsonArrayList.value.filter(item => item.check);
rightJsonArrayList.value.push(...selectArr);
leftJsonArrayList.value = leftJsonArrayList.value.filter((item) => !item.check);
rightJsonArrayList.value.forEach(item => item.check = false);
lcheck.value = false;
rcheck.value = false;
}
const leftBtnCurr = () => {
let selectArr = rightJsonArrayList.value.filter(item => item.check);
leftJsonArrayList.value.push(...selectArr);
rightJsonArrayList.value = rightJsonArrayList.value.filter((item) => !item.check);
leftJsonArrayList.value.forEach(item => item.check = false);
rcheck.value = false;
lcheck.value = false;
}
watch(
() => leftJsonArrayList.value,
(valNew, valOld) => {
let jsonArrayResult = valNew.filter(item => item.check);
lnum.value = jsonArrayResult.length;
let jsonArrayResultAll = valNew.filter(item => !item.disabled);
if (lnum.value > 0 && lnum.value == jsonArrayResultAll.length) {
lcheck.value = true;
} else {
lcheck.value = false;
}
nextTick(() => {
lightBtn();
})
},
{ deep: true, immediate: true }
)
watch(
() => rightJsonArrayList.value,
(valNew, valOld) => {
let jsonArrayResult = valNew.filter(item => item.check);
rnum.value = jsonArrayResult.length;
let jsonArrayResultAll = valNew.filter(item => !item.disabled);
rAllNum.value = jsonArrayResultAll.length;
if (rnum.value > 0 && rnum.value == jsonArrayResultAll.length) {
rcheck.value = true;
} else {
rcheck.value = false;
}
nextTick(() => {
lightBtn();
})
},
{ deep: true, immediate: true }
);
const lallChecked = () => {
if (!lcheck.value) {
let notdisabled = leftJsonArrayList.value.filter((item) => !item.disabled);
notdisabled.filter((item) => { item.check = true });
lightBtn()
} else {
let notdisabled = leftJsonArrayList.value.filter((item) => !item.disabled);
notdisabled.filter((item) => { item.check = false });
lightBtn()
}
}
const rallChecked = () => {
if (rcheck.value == false) {
let notdisabled = rightJsonArrayList.value.filter((item) => !item.disabled);
notdisabled.filter((item) => { item.check = true });
} else {
let notdisabled = rightJsonArrayList.value.filter((item) => !item.disabled);
notdisabled.filter((item) => { item.check = false });
}
}
function lightBtn() {
let rBtn = document.querySelector(".rBtn");
let lBtn = document.querySelector(".lBtn");
lnum.value > 0 ? rBtn.classList.add("currBtn") : rBtn.classList.remove("currBtn");
rnum.value > 0 ? lBtn.classList.add("currBtn") : lBtn.classList.remove("currBtn");
}
</script>
<style scoped>
* {
margin: 0;
padding: 0;
list-style: none;
box-sizing: border-box;
}
.main-box {
width: 800px;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.btn {
margin: 0px 20px;
}
.lBtn,
.rBtn {
border: 0;
background-color: #8CB5E7;
padding: 10px 20px;
color: #fff;
border-radius: 5px;
cursor: pointer;
}
.lBtn {
margin-right: 5px;
}
.currBtn {
background-color: #3086EB;
}
.currops {
color: #3086EB;
}
.List {
border: 1px solid rgba(170, 170, 170, 0.339);
border-radius: 5px;
overflow: hidden;
width: 200px;
height: 300px;
}
.title {
background-color: #F1F1F3;
padding: 10px 20px;
}
.num1,
.num2 {
float: right;
}
ul {
width: 100%;
height: 85%;
overflow-x: hidden;
}
li>div {
padding: 10px 20px;
}
li>div:hover {
color: #3086EB;
}
li>div>input {
margin-right: 10px;
;
}
.title>input[type="checkbox"] {
margin-top: 7px;
cursor: pointer;
position: relative;
width: 14px;
height: 14px;
font-size: 14px;
margin-right: 8px;
background-color: #fff;
}
.title>input[type="checkbox"]::after {
position: absolute;
top: 0;
color: #fff;
width: 14px;
height: 14px;
display: inline-block;
visibility: visible;
text-align: center;
border-radius: 2px;
content: " ";
}
.title>input[type="checkbox"]:checked::after {
border: none;
border-radius: 2px;
content: "✔";
background-color: #0075FF;
border-radius: 2px;
content: "-";
font-size: 12px;
font-weight: lighter;
}
</style>