Я использую JQuery Sortable для этого, но в случае, если вы используете Vue.js, как я, вот решение, которое создает пользовательскую директиву Vue для инкапсуляции функциональности Sortable, я знаю, что Vue перетаскивается, но он не сортирует столбцы таблицы в соответствии с вопросом ЗДЕСЬ Чтобы увидеть это в действии, ПРОВЕРЬТЕ ЭТО
Код JS
Vue.directive("draggable", {
//adapted from https://codepen.io/kminek/pen/pEdmoo
inserted: function(el, binding, a) {
Sortable.create(el, {
draggable: ".draggable",
onEnd: function(e) {
/* vnode.context is the context vue instance: "This is not documented as it's not encouraged to manipulate the vm from directives in Vue 2.0 - instead, directives should be used for low-level DOM manipulation, and higher-level stuff should be solved with components instead. But you can do this if some usecase needs this. */
// fixme: can this be reworked to use a component?
// https://github.com/vuejs/vue/issues/4065
// https://forum.vuejs.org/t/how-can-i-access-the-vm-from-a-custom-directive-in-2-0/2548/3
// https://github.com/vuejs/vue/issues/2873 "directive interface change"
// `binding.expression` should be the name of your array from vm.data
// set the expression like v-draggable="items"
var clonedItems = a.context[binding.expression].filter(function(item) {
return item;
});
clonedItems.splice(e.newIndex, 0, clonedItems.splice(e.oldIndex, 1)[0]);
a.context[binding.expression] = [];
Vue.nextTick(function() {
a.context[binding.expression] = clonedItems;
});
}
});
}
});
const cols = [
{name: "One", id: "one", canMove: false},
{name: "Two", id: "two", canMove: true},
{name: "Three", id: "three", canMove: true},
{name: "Four", id: "four", canMove: true},
]
const rows = [
{one: "Hi there", two: "I am so excited to test", three: "this column that actually drags and replaces", four: "another column in its place only if both can move"},
{one: "Hi", two: "I", three: "am", four: "two"},
{one: "Hi", two: "I", three: "am", four: "three"},
{one: "Hi", two: "I", three: "am", four: "four"},
{one: "Hi", two: "I", three: "am", four: "five"},
{one: "Hi", two: "I", three: "am", four: "six"},
{one: "Hi", two: "I", three: "am", four: "seven"}
]
Vue.component("datatable", {
template: "#datatable",
data() {
return {
cols: cols,
rows: rows
}
}
})
new Vue({
el: "#app"
})
CSS
.draggable {
cursor: move;
}
table.table tbody td {
white-space: nowrap;
}
Шаблон мопса HTML
#app
datatable
script(type="text/x-template" id="datatable")
table.table
thead(v-draggable="cols")
template(v-for="c in cols")
th(:class="{draggable: c.canMove}")
b-dropdown#ddown1.m-md-2(:text='c.name')
b-dropdown-item First Action
b-dropdown-item Second Action
b-dropdown-item Third Action
b-dropdown-divider
b-dropdown-item Something else here...
b-dropdown-item(disabled='') Disabled action
tbody
template(v-for="row in rows")
tr
template(v-for="(col, index) in cols")
td {{row[col.id]}}