ruby-on-rails - 如何使用 SortablejS 和acts_as_list gem 处理多个列表之间的拖放?
问题描述
我一直在寻找一天半的解决方案。
我有几个表(类别),允许用户拖放每一行(项目)以重新排序每个项目在该表(类别)上的位置。我正在使用SortableJS来处理拖放,并且我拥有它以便在同一类别中重新排序工作。
在我的后端,我有一个使用 Jbuilder 返回 JSON 的 Rails API,并且正在使用acts_as_list gem来处理我的位置。
当我将项目从类别 A 拖到类别 B 时,我在弄清楚如何处理重新排序时遇到问题。我认为问题在于控制器操作以及我无法提出解决方案来接收多个类别的更新,并且然后使用 Jbuilder 返回更新后的类别的位置。一些帮助将不胜感激!
项目列表.vue
<script>
methods: {
async dragReorder({ item, to, from, oldIndex, newIndex, oldDraggableIndex, newDraggableIndex, clone, pullMode }) {
// item that was dragged
const draggedItem = JSON.parse(item.getAttribute('data-item'));
// initial payload
const payload = {
category_id: draggedItem.category_id,
category_item_id: draggedItem.pivot.id,
item_id: draggedItem.id,
new_index: newIndex + 1,
user_id: this.user.id,
};
const newCategory = JSON.parse(to.getAttribute('data-category'));
// if item is dragged to new category
if (pullMode) payload.new_category_id = newCategory;
await categoryService.updatePosition(payload);
},
},
mounted() {
this.$nextTick(() => {
const tables = document.querySelectorAll('.items-table-container');
tables.forEach(table => {
const el = table.getElementsByTagName('tbody')[0];
Sortable.create(el, {
animation: 150,
direction: 'vertical',
easing: 'cubic-bezier(.17,.67,.83,.67)',
group: 'items-table-container',
onEnd: this.dragReorder,
});
});
})
},
</script>
类别.rb
class Category < ApplicationRecord
has_many :categories_items, -> { order(position: :asc) }
has_many :items, through: :categories_items, source: :item
accepts_nested_attributes_for :items
end
categories_item.rb
class CategoriesItem < ApplicationRecord
belongs_to :category
belongs_to :item
acts_as_list scope: :category
end
项目.rb
class Item < ApplicationRecord
has_many :categories_items
has_many :categories, through: :categories_items, source: :category
end
类别控制器.rb
def update_position
item = CategoriesItem.find_by(category: params[:category_id], item: params[:item_id])
# if item was moved to new category
categories = []
if params[:new_category_id]
item.update(category_id: params[:new_category_id])
Item.find(params[:item_id]).update(category_id: params[:new_category_id])
item.insert_at(params[:new_index]) unless !item
categories << Category.find(params[:category_id])
categories << Category.find(params[:new_category_id])
else
item.insert_at(params[:new_index]) unless !item
categories << Category.find(params[:category_id])
end
@categories = categories
end
update_position.json.jbuilder
json.array! @categories do |category|
json.(category, :id, :name, :created_at, :updated_at)
json.categories_items category.categories_items do |category_item|
json.(category_item, :id, :category_id, :item_id, :created_at, :updated_at, :position)
end
end
解决方案
acts_as_list
允许您设置列表项的新范围参数和位置,然后只需保存该项目,该项目将自动移出旧范围并进入您想要的位置的新范围(使用after_save
回调)。
在这方面,您应该能够这样做,然后重新获取两个范围中的每个项目并将它们返回到您的前端以更新您的显示。
推荐阅读
- ios - 有谁知道为什么 AVAsset 类无法读取使用 MTLTextures 编写的视频文件
- python - Django Query.get 引发对象匹配查询不存在 Try & except 错误
- python - 在 RabbitMQ 队列中缺少从 Celery 发送的消息
- python - 在表中添加多个列,例如在 flask-sqlalchemy 中使用 for / while 循环
- python - 计算方法未能分配 product.template 。奥多 14
- amazon-s3 - 从 Lambda 函数生成预签名 S3 URL 时出现“访问密钥不存在”
- unix-timestamp - TradingView 轻量级图表 API:将时间轴显示为 UNIX 时间戳
- anylogic - AnyLogic 中的 Lift 如果在拾取之前失败,则在 repair() 上抛出 NoSuchElementException
- python - 是否可以使用 split() 对一串数字进行分区?
- json - 有没有办法在 bodyFileName 中为wiremock存根映射使用通配符?