15sortable.js onChoose 选中事件

This commit is contained in:
bunny 2024-02-01 09:03:24 +08:00
parent 2bd87cee7b
commit 6ff75ca25b
2 changed files with 177 additions and 13 deletions

View File

@ -0,0 +1,129 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>sortable.js onChoose事件例子</title>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no, minimal-ui">
<script src="https://www.itxst.com/package/sortable/sortable.min.js"></script>
<style>
.box {
display: flex;
justify-content: space-around;
width: 50%;
}
.itxst {
margin: 10px auto;
width: 80%;
float: left;
margin-right: 10px;
}
.itxst>div {
padding: 6px;
background-color: #fdfdfd;
border: solid 1px #eee;
margin-bottom: 10px;
cursor: move;
}
.itxst .innner {
padding: 6px;
background-color: #fdfdfd;
border: solid 1px #eee;
margin-bottom: 10px;
cursor: move;
}
#msg {
clear: both;
width: 100%;
}
.ghost {
background-color: red !important;
}
.chosen {
border: solid 2px #3089dc !important;
}
</style>
</head>
<body>
<div class="box">
<div id="g1" class="itxst">
<div class="item" data-id="item1">item 1</div>
<div class="item" data-id="item2">item 2</div>
<div class="item" data-id="item3">item 3</div>
</div>
<div id="g2" class="itxst">
<div class="item" data-id="item1">item 1</div>
<div class="item" data-id="item2">item 2</div>
<div class="item" data-id="item3">item 3</div>
</div>
</div>
<div id="msg"></div>
<script>
const msg = document.getElementById("msg");
//第一组
const g1 = document.getElementById("g1");
const ops = {
animation: 1000,
delay: 1,
// 拖拽的元素
draggable: ".item",
// 选中的样式
chosenClass: "chosen",
//停靠位置样式
ghostClass: 'ghost',
forceFallback: true,
group: { name: 'itxst.com', pull: "clone", put: true },
// ******** 选中执行的方法 ********
onChoose(event) {
const index = event.oldIndex
msg.innerHTML = `选中了第${arr1[index]}`
},
// ******** 拖动结束 ********
onEnd(event) {
console.log(JSON.stringify(arr1));
}
}
const sortable = Sortable.create(g1, ops)
// 获取数据
const arr1 = sortable.toArray()
// 第二组
const g2 = document.getElementById("g2")
const ops2 = {
animation: 166,
delay: 1,
// 拖拽的元素
draggable: ".item",
// 选中的元素
chosenClass: "chosen",
// 停靠位置样式
ghostClass: "ghost",
// 禁用html5原生拖拽
forceFallback: true,
group: { name: 'itxst.com', pull: "clone", put: true },
// ******** 选中的方法 ********
onChoose(event) {
const index = event.oldIndex
msg.innerHTML = `选中第${arr2[index]}`
},
// ******** 拖动结束 ********
onEnd(event) {
console.log(JSON.stringify(arr2))
}
}
const sortable2 = Sortable.create(g2, ops2)
const arr2 = sortable2.toArray()
</script>
</body>
</html>

61
temp.js
View File

@ -1,21 +1,56 @@
var g1 = document.getElementById('g1');
var ops1 = {
const msg = document.getElementById("msg");
//第一组
const g1 = document.getElementById("g1");
const ops = {
animation: 1000,
delay: 1000,
delay: 1,
// 拖拽的元素
draggable: ".item",
// 选中的样式
chosenClass: "chosen",
//停靠位置样式
ghostClass: 'ghost',
forceFallback: true,
group: { name: 'itxst.com', pull: "clone", put: true },
// ******** 选中执行的方法 ********
onChoose(event) {
const index = event.oldIndex
msg.innerHTML = `选中了第${arr1[index]}`
},
// ******** 拖动结束 ********
onEnd(event) {
console.log(JSON.stringify(arr1));
}
}
const sortable = Sortable.create(g1, ops)
// 获取数据
const arr1 = sortable.toArray()
// 第二组
const g2 = document.getElementById("g2")
const ops2 = {
animation: 166,
delay: 1,
draggable: '.item',
//停靠位置样式
ghostClass: 'ghost',
//************* 选中样式 ***********
//选中样式
chosenClass: 'chosen',
//禁用html5原生拖拽行为
forceFallback: true,
group: { name: 'itxst.com', pull: true, put: true },
//拖动结束
onEnd: function (evt) {
console.log(evt);
//获取拖动后的排序
var arr = sortable1.toArray();
document.getElementById('msg').innerHTML = 'A组排序结果' + JSON.stringify(arr);
group: { name: 'itxst.com', pull: "clone", put: true },
//*********** 选中后执行的方法 ***********
onChoose: function (event) {
const index = event.oldIndex
msg.innerHTML = '你选中了第' + arr2[index] + '元素';
},
};
var sortable1 = Sortable.create(g1, ops1);
//拖动结束
onEnd: function (event) {
//获取拖动后的排序
console.log(JSON.stringify(arr2));
},
}
const sortable2 = Sortable.create(g2, ops2)
const arr2 = sortable2.toArray()