ruby-position 排序

1.gem ‘acts_as_list’

2.model

1
acts_as_list

3.controller

1
2
3
4
5
6
7
8
9
10
11
up
@recruit_info.move_higher

down
@recruit_info.move_lower

top
@recruit_info.move_to_top

bottom
@recruit_info.move_to_bottom

4.view
一般置顶 最末会刷新页面
上移 下移用js

1
2
3
4
5
6
7
8
9
10
<%= link_to content_tag(:i, "", class: "fa fa-arrow-up"), "javascript:;", method: :post,
class: "info-up", data: { id: recruit_info.id, url: up_comp_recruit_info_path(recruit_info),
toggle: "popover", placement: "top", content: "上移" } %>
<%= link_to content_tag(:i, "", class: "fa fa-arrow-down"), "javascript:;", method: :post,
class: "info-down", data: { id: recruit_info.id, url: down_comp_recruit_info_path(recruit_info),
toggle: "popover", placement: "top", content: "下移" } %>
<%= link_to content_tag(:i, "", class: "fa fa-arrow-circle-up"), top_comp_recruit_info_path(recruit_info),
method: :post, class: "info-top", data: { toggle: "popover", placement: "top", content: "置顶" } %>
<%= link_to content_tag(:i, "", class: "fa fa-arrow-circle-down"), bottom_comp_recruit_info_path(recruit_info),
method: :post, class: "info-bottom", data: { toggle: "popover", placement: "top", content: "最末" } %> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<script type="text/javascript">

$prevLink = $('.pagination').find('.active').prev().find('a').attr('href');
$nextLink = $('.pagination').find('.active').next().find('a').attr('href');

$('.info-up').on('click', function(){
const pageFirst = $('.recruit-info-item').first();
$page = $(this).closest('.recruit-info-item');
$.ajax({
url: $(this).data('url'),
type: 'POST',
data: {
id: $(this).data('id')
},
success: function() {
if ($page.is(pageFirst)) {
if (<%= @recruit_infos.current_page %> === 1) {
$.notify('不可上移!!','error')
} else {
window.location.href = $prevLink
}
} else {
$page.prev().before($page);
}
}
});
});

$('.info-down').on('click', function(){
const pageLast = $('.recruit-info-item').last();
$page = $(this).closest('.recruit-info-item');
$.ajax({
url: $(this).data('url'),
type: 'POST',
data: {
id: $(this).data('id')
},
success: function() {
if ($page.is(pageLast)) {
if (<%= @recruit_infos.total_pages == @recruit_infos.current_page %>) {
$.notify('不可下移!!','error')
} else {
window.location.href = $nextLink
}
} else {
$page.next().after($page);
}
}
});
});
</script>

ps: 必须有position字段如果是后加的 在migration文件中需要

1
2
3
ExampleCv.order(updated_at: :desc).each.with_index(1) do |todo_item, index|
todo_item.update_column :position, index
end