使用Rails6实现sortablejs。一切都很顺利,直到我尝试使用toArray方法,而现在我无法让它正常工作。
我在刺激控制器中实例化sortable,如下所示:
import { Controller } from "stimulus";
import Sortable from "sortablejs"
export default class extends Controller {
connect() {
this.sortable = Sortable.create(this.element, {
animation: 150,
filter: 'input, button,',
onEnd: this.end.bind(this)
})
console.log($("#sort-me"));
}
end(event) {
console.log(event)
var sortable = this.sortable;
let combo_id = event.item.dataset.id
let entry_id = combo_id.substr(0, combo_id.indexOf("_"));
let id = combo_id.substr(combo_id.indexOf("_") + 1, combo_id.length);
let current_order = $('#image_order_array_' + entry_id).val();
let current_position = $('#image_position_' + id).val();
let data = new FormData()
data.append("current_order", current_order)
data.append("current_position", current_position)
data.append("new_position", event.newIndex)
data.append("old_position", event.oldIndex)
var idsInOrder = $("#sort-me").sortable("toArray");
Rails.ajax({
url: this.data.get("url").replace(":id", id),
type:'PATCH',
data: data
})
}
}当end事件触发时,我会得到:
drag_controller.js:40 Uncaught TypeError: $(...).sortable is not a function我已经尝试了许多不同的排列方式,但都不能正常工作。基本上,一旦一个项目被排序,我需要获得toArray的值,这样我才能知道项目的当前顺序。
发布于 2021-05-15 00:32:19
答案: this.sortable.toArray
https://stackoverflow.com/questions/67522924
复制相似问题