我想在 v 数据表中拖动列的标题。我已经使用指令/sortablejs 取得了很大的进展,但我无法弄清楚以下内容:
任何帮助/见解将不胜感激!
我有一个此设置的代码笔: https://codepen.io/uglyhobbitfeet/pen/NWWKVza
codepen 最重要的部分是:
<v-data-table v-sortable-table
和
directives: {
'sortable-table': {
...
}
}
我采用了稍微不同的路线,使用数据表上的键和自定义 onEnd 方法。工作代码在这里:
https://codepen.io/uglyhobbitfeet/pen/ExxaNGO
<v-data-table :headers="headers" :items="desserts"
sort-by="calories"
v-sortable-table="{onEnd:sortTheHeadersAndUpdateTheKey}"
:key="anIncreasingNumber" >
我将 ekjcfn3902039 实现的核心细节放在这里,这样您就不必对伟大的 Pen 中发生的所有其他内容进行排序。完全归功于他/她。
<script>
区域: function watchClass(targetNode, classToWatch) {
let lastClassState = targetNode.classList.contains(classToWatch);
const observer = new MutationObserver((mutationsList) => {
for (let i = 0; i < mutationsList.length; i++) {
const mutation = mutationsList[i];
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
const currentClassState = mutation.target.classList.contains(classToWatch);
if (lastClassState !== currentClassState) {
lastClassState = currentClassState;
if (!currentClassState) {
mutation.target.classList.add('sortHandle');
}
}
}
}
});
observer.observe(targetNode, { attributes: true });
}
v-sortable-table="{onEnd:sortTheHeadersAndUpdateTheKey}"
:key="anIncreasingNumber"
anIncreasingNumber: 1,
this.tableHeaders
更改为您的 headers 对象: sortTheHeadersAndUpdateTheKey(evt) {
const headersTmp = this.tableHeaders;
const oldIndex = evt.oldIndex;
const newIndex = evt.newIndex;
if (newIndex >= headersTmp.length) {
let k = newIndex - headersTmp.length + 1;
while (k--) {
headersTmp.push(undefined);
}
}
headersTmp.splice(newIndex, 0, headersTmp.splice(oldIndex, 1)[0]);
this.table = headersTmp;
this.anIncreasingNumber += 1;
}
directives: {
'sortable-table': {
inserted: (el, binding) => {
el.querySelectorAll('th').forEach((draggableEl) => {
// Need a class watcher because sorting v-data-table rows asc/desc removes the sortHandle class
watchClass(draggableEl, 'sortHandle');
draggableEl.classList.add('sortHandle');
});
Sortable.create(el.querySelector('tr'), binding.value ? { ...binding.value, handle: '.sortHandle' } : {});
},
},
}
我在这支笔中将其简化为最低限度:https://codepen.io/bradlymathews/pen/QWyXpZv?editors=1010
但是当我们使用国际化(希伯来语(RTL 模式))时,这不起作用。