Mik*_*yle 10 sorting ruby-on-rails activeadmin
我正在尝试向Active Admin中的资源添加排序/重新排序.我了解您可以按登录时的不同列进行排序.我想要做的是能够订购商品,以便在前端按特定顺序显示.有关如何实现这一目标的任何想法?
我已经在数据库中有一个排序列.
另外,我想在管理部分中以特定顺序显示项目.
任何人对我如何实现这一点有任何想法?
小智 25
我最近在HomeSlide模型上使用名为position的列实现了这一点.
ActiveAdmin.register HomeSlide do
config.sort_order = 'position_asc'
index do
column :title
default_actions
end
# This action is called by javascript when you drag and drop a column
# It iterates through the collection and sets the new position based on the
# order that jQuery submitted them
collection_action :sort, :method => :post do
params[:home_slide].each_with_index do |id, index|
HomeSlide.update_all(['position=?', index+1], ['id=?', id])
end
render :nothing => true
end
end
Run Code Online (Sandbox Code Playgroud)
将此添加到您的active_admin javascripts(咖啡脚本)
sendSortRequestOfModel = (model_name) ->
formData = $('#' + model_name + ' tbody').sortable('serialize')
formData += "&" + $('meta[name=csrf-param]').attr("content") + "=" + encodeURIComponent($('meta[name=csrf-token]').attr("content"))
$.ajax
type: 'post'
data: formData
dataType: 'script'
url: '/admin/' + model_name + '/sort'
jQuery ($) ->
# home page slides
if $('body.admin_home_slides.index').length
$( "#home_slides tbody" ).disableSelection()
$( "#home_slides tbody" ).sortable
axis: 'y'
cursor: 'move'
update: (event, ui) ->
sendSortRequestOfModel("home_slides")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5406 次 |
| 最近记录: |