我正在使用vuejs和Rubaxa可排序 JavaScript库。可排序在<ul><li>中很好地工作,但是对于表行,当我按拖放重新排列时,它就会产生这个错误。
Sortable.min.js:3 Uncaught DOMException: Failed to execute 'matches' on 'Element': '>*' is not a valid selector.我使用的是Vue.js v2.5.13和v1.7.0。
Vue.directive('sortable', {
inserted: function (el, binding) {
var sortable = new Sortable(el, binding.value || {});
}
});
new Vue({
el: '#app',
data () {
return {
items: [{id: 1},{id: 2},{id: 3},{id: 4},{id: 5}]
}
},
methods: {
reorder ({oldIndex, newIndex}) {
const movedItem = this.items.splice(oldIndex, 1)[0]
this.items.splice(newIndex, 0, movedItem)
}
}
})<script src="https://vuejs.org/js/vue.min.js"></script>
<script src="https://cdn.rawgit.com/RubaXa/Sortable/c35043730c22ec173bac595346eb173851aece20/Sortable.min.js"></script>
<div id="app">
<h2>With List</h2>
<ul v-sortable="{onEnd: reorder}">
<li v-for="i in items" :key="i.id">{{ i.id }}</li>
</ul>
<hr/>
<h2>With Table</h2>
<table v-sortable="{onEnd: reorder}">
<tr v-for="i in items" :key="i.id">
<td>{{ i.id }}</td>
</tr>
</table>
{{ items }}
</div>
发布于 2018-02-18 11:33:50
<table>不能包含表行(<tr>)。桌子的结构是这样的。
<table>
<thead></thead>
<tbody></tbody>
<tfoot></tfoot>
</table>所以当我们像这样写html表时,
<table>
<tr>First Row</tr>
<tr>Second Row</tr>
</table>浏览器自动在<tbody>中插入所有行,并将其呈现如下。
<table>
<tbody>
<tr>First Row</tr>
<tr>Second Row</tr>
</tbody>
</table>因此,表行不是<table>的直接子行,而是<tbody>的子行。因此,在<tbody>中生成表行,并将v排序添加到该<tbody>中。
<table>
<tbody v-sortable="{onEnd: reorder}"> <!-- v-sortable here -->
<tr v-for="i in items" :key="i.id">
<td>{{ i.id }}</td>
</tr>
</tbody>
</table>可排序的小型化版本有一个bug,当缩小try-catch块时,它会在<ul><li>以外的任何东西被排序时导致它失败。因此,在他们修复之前,请使用开发,即可排序的未压缩版本。
https://stackoverflow.com/questions/48804134
复制相似问题