fix error and add number box for sorting

This commit is contained in:
邱博亞 2022-03-08 22:38:23 +08:00
parent 957ab330f7
commit 847ab9a325
4 changed files with 73 additions and 5 deletions

View File

@ -34,10 +34,10 @@ class UniversalTablesController < ApplicationController
sort_class = "sort hide" sort_class = "sort hide"
when "text" when "text"
if tc.make_categorizable if tc.make_categorizable
select_values = tc.column_entries.distinct(:text) select_values = tc.column_entries.distinct("text.#{I18n.locale.to_s}")
form_field = "<input type=\"hidden\" name=\"authenticity_token\" value=\"#{csrf_value}\"><select class='form-control' name='q'>" form_field = "<input type=\"hidden\" name=\"authenticity_token\" value=\"#{csrf_value}\"><select class='form-control' name='q'>"
select_values.each do |sv| select_values.each do |sv|
form_field = form_field + "<option value='#{sv[I18n.locale.to_s]}'>#{sv[I18n.locale.to_s]}</option>" form_field = form_field + "<option value='#{sv}'>#{sv}</option>"
end end
form_field = form_field + "</select>" form_field = form_field + "</select>"
end end

View File

@ -17,7 +17,7 @@ module Admin::UniversalTablesHelper
end end
def render_unique_texts(f,column,i) def render_unique_texts(f,column,i)
select_values = column.column_entries.distinct(:text).uniq select_values = column.column_entries.distinct(:text)
select = "<select id='#{column.key}_#{i}'>" select = "<select id='#{column.key}_#{i}'>"
s = {"en" => "", "zh_tw" => ""} s = {"en" => "", "zh_tw" => ""}
select = select + "<option class='muted' value='#{s.to_json.html_safe}'>---------------Select---------------</option>" select = select + "<option class='muted' value='#{s.to_json.html_safe}'>---------------Select---------------</option>"
@ -29,7 +29,7 @@ module Admin::UniversalTablesHelper
end end
def render_unique_number(f,column,i) def render_unique_number(f,column,i)
select_values = column.column_entries.distinct(:number).uniq select_values = column.column_entries.distinct(:number)
select = "<select id='#{column.key}_#{i}'>" select = "<select id='#{column.key}_#{i}'>"
s = "" s = ""
select = select + "<option class='muted' value='#{s}'>---------------Select---------------</option>" select = select + "<option class='muted' value='#{s}'>---------------Select---------------</option>"

View File

@ -21,7 +21,7 @@
<% @entries.each do |entry| %> <% @entries.each do |entry| %>
<tr data-id="<%= entry.id %>"> <tr data-id="<%= entry.id %>">
<td> <td>
<%= entry.sort_number %> <%= number_field_tag nil,entry.sort_number,class: 'sort_number',step: 1 %>
</td> </td>
<% @columns.each_with_index do |column, index| %> <% @columns.each_with_index do |column, index| %>
<% ce = entry.column_entries.where(:table_column_id => column.id).first rescue nil %> <% ce = entry.column_entries.where(:table_column_id => column.id).first rescue nil %>

View File

@ -25,6 +25,74 @@
update_sort(); update_sort();
} }
}); });
$('.sort_number').change(function(){
var new_sort_number = parseFloat($(this).val());
var min_number = $('.sort_number').length;
var max_number = 0;
var pool = $('.sort_number').not(this);
var same_order = pool.filter(function(){
var tmp_sort = parseFloat($(this).val());
if (tmp_sort<min_number){
min_number = tmp_sort;
}
if (tmp_sort>max_number){
max_number = tmp_sort;
}
return tmp_sort==new_sort_number
});
var tmp_same_order = null;
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));
}
}
}
});
} }
$(document).ready(function(){ $(document).ready(function(){
$('#update_sort_button').click(update_sort); $('#update_sort_button').click(update_sort);