Fix edit_sort.

This commit is contained in:
邱博亞 2024-07-18 20:54:13 +08:00
parent c74f9f3721
commit cce295ba78
4 changed files with 25 additions and 54 deletions

View File

@ -23,7 +23,7 @@
<%= number_field_tag nil,annc.sort_number,class: 'sort_number',step: 1 %>
</td>
<td>
<%= annc.title %>
<%= annc.title.html_safe %>
</td>
<td>
<%= format_value annc.postdate %>

View File

@ -18,6 +18,9 @@
</div>
<%= render partial: 'edit_sort' %>
<script type="text/javascript">
function parse_val(val){
return (val == '') ? 0 : parseFloat(val);
}
function update_sort(){
var ids = $.map($('#sortable>tr'),function(v){return $(v).data('id')});
$.ajax({
@ -38,12 +41,18 @@
}
});
$('.sort_number').change(function(){
var new_sort_number = parseFloat($(this).val());
var min_number = $('.sort_number').length;
var max_number = 0;
$(this).data('changed', true);
});
$('.sort_number').on('blur', function(){
if(!$(this).data('changed')) {
return;
}
var new_sort_number = parse_val($(this).val());
var min_number = 0;
var max_number = $('.sort_number').length;
var pool = $('.sort_number').not(this);
var same_order = pool.filter(function(){
var tmp_sort = parseFloat($(this).val());
var tmp_sort = parse_val($(this).val());
if (tmp_sort<min_number){
min_number = tmp_sort;
}
@ -52,59 +61,19 @@
}
return tmp_sort==new_sort_number
});
var tmp_same_order = null;
if (same_order.length>0){
var tmp_same_order = $();
if (same_order.length>0) {
tmp_same_order = same_order.eq(0);
tmp_same_order.parents('tr').eq(0).before($(this).parents('tr').eq(0));
}else{
//ex. 1 2 3 5,insert 4
if (parseInt(pool.eq(0).val())<parseInt(pool.eq(-1).val())){ //asc
$.each(pool,function(){
var tmp_sort = parseFloat($(this).val());
if (new_sort_number>max_number){
if (tmp_sort<=new_sort_number){
tmp_same_order = $(this);
}
}else{
if (tmp_sort>=new_sort_number){
tmp_same_order = $(this);
return false;
}
}
});
if (new_sort_number>max_number){
tmp_same_order.parents('tr').eq(0).after($(this).parents('tr').eq(0));
}else{
tmp_same_order.parents('tr').eq(0).before($(this).parents('tr').eq(0));
}
}else{ //desc
//ex. 5 3 2 1,insert 4
$.each(pool,function(){
var tmp_sort = parseFloat($(this).val());
if (new_sort_number<min_number){
if (tmp_sort>=new_sort_number){
tmp_same_order = $(this);
}
}else if (new_sort_number>max_number){
if (tmp_sort<=new_sort_number){
tmp_same_order = $(this);
return false;
}
}else{
if (tmp_sort<=new_sort_number){
tmp_same_order = $(this);
return false;
}
}
});
if (new_sort_number<min_number){
tmp_same_order.parents('tr').eq(0).after($(this).parents('tr').eq(0));
}else{
tmp_same_order.parents('tr').eq(0).before($(this).parents('tr').eq(0));
}
}
} else if (new_sort_number>=max_number) {
tmp_same_order = pool.eq(-1);
tmp_same_order.parents('tr').eq(0).after($(this).parents('tr').eq(0));
} else if (new_sort_number<=min_number) {
tmp_same_order = pool.eq(0);
tmp_same_order.parents('tr').eq(0).before($(this).parents('tr').eq(0));
}
update_sort();
$(this).data('changed', false);
});
}
$(document).ready(function(){

View File

@ -92,6 +92,7 @@ en:
file: File
view_count: View Count
department: Department
sort_number: Sort Number
add_new: Add New
export_to_excel: Export to Excel
export_all_anns: Export all Announcement

View File

@ -93,6 +93,7 @@ zh_tw:
file: 檔案下載
view_count: 瀏覽人次
department: 單位
sort_number: 排序值
add_new: 新建
export_to_excel: 匯出至Excel檔
export_all_anns: 匯出所有公告