【发布时间】:2021-05-13 16:37:45
【问题描述】:
使用 Rails 6 实现 sortablejs。在我尝试使用 toArray 方法之前,一切都很顺利,现在我这辈子都无法正常工作。
我在激励控制器中实例化可排序如下:
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
})
}
}
当结束事件触发时,我得到:
drag_controller.js:40 Uncaught TypeError: $(...).sortable is not a function
我已经尝试了许多不同的排列,但无法正常工作。基本上,一旦对项目进行排序,我需要获取 toArray 值,以便知道项目的当前顺序。
【问题讨论】:
-
看起来 sortable 由于某种原因为空,对吧?
-
在这个例子中,似乎是这样。我尝试了很多方法...都产生错误。