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 %> <%= number_field_tag nil,annc.sort_number,class: 'sort_number',step: 1 %>
</td> </td>
<td> <td>
<%= annc.title %> <%= annc.title.html_safe %>
</td> </td>
<td> <td>
<%= format_value annc.postdate %> <%= format_value annc.postdate %>

View File

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

View File

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

View File

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