我在Rails中使用JQuery Multiselect(http://loudev.com/)。一切都很好,但是没有保存从多选中选择数据的顺序。
我基本上遇到与此问题相同的问题:jquery multiselect selected data order
我需要在Rails中实现类似的解决方案,并且非常感谢任何帮助
这是代码
表格(多选项发生在collection_select中)
:javascript
$(document).ready(function (){
$('#load_format_column_sequence').multiSelect({ keepOrder: true });
});
= form_for [:operate,@biller,@load_format], :remote => true do |form|
= form.fieldset :legend => 'General' do
= form.text :name
= form.text :load_processor
= form.text :processor_options
= form.fields_for(@load_format) do |builder|
= builder.label "Column Sequence"
= collection_select(:load_format, :column_sequence, load_format_column_sequence_select, :last, :first, {}, {:multiple => true})
= form.checkbox :inactivate_removed_accounts
= form.fieldset :legend => 'Schedule and Transfer' do
- @load_format.build_file_transfer_format unless @load_format.file_transfer_format
= render :partial => 'operate/file_transfer_formats/form', :locals => { :form => form, :file_transfer_format => @load_format.file_transfer_format, :parent => @load_format, :show_schedule => true }
数据数组
def load_format_column_sequence_select
{
'EXTERNAL KEY' => :external_key,
'EXTERNAL CODE' => :external_code,
'ACCOUNT TYPE' => :account_type,
'PAYER NAME' => :payer_name,
'ALTERNATE PAYER NAME' => :payer_alternate_name,
'PAYMENT AMOUNT' => :payment_amount,
'PAYOFF AMOUNT' => :payoff_amount,
'ACTIVE' => :active,
'ALTERNATE KEYS' => :alternate_keys,
'BILL EXTERNAL KEY' => :bill_external_key,
'BILL EXTERNAL CODE' => :bill_external_code,
'BILL DUE AMOUNT' => :bill_due_amount,
'BILL DISCOUNT AMOUNT' => :bill_discount_amount,
'BILL ALTERNATE KEY' => :bill_alternate_key,
'DETAIL EXTERNAL KEY' => :detail_external_key,
'DETAIL AMOUNT' => :detail_amount,
'DETAIL DESCRIPTION' => :detail_description,
'GROUP NAME' => :group_name,
'GROUP EXTERNAL KEY' => :group_external_key,
'RECENT CHECK ABA' => :recent_check_aba,
'RECENT CHECK ACCOUNT' => :recent_check_account,
'RECENT CHECK NUMBER' => :recent_check_num,
'RECENT CHECK AMOUNT' => :recent_check_amount,
'PAYER FIRST NAME' => :payer_first_name,
'PAYER LAST NAME' => :payer_last_name,
'ACCOUNT DESCRIPTION' => :account_description,
'HOLD REASON KEYS' => :hold_reason_keys,
'ACCOUNT ADDRESS LINE 1' => :account_address_line_1,
'ACCOUNT ADDRESS LINE 2' => :account_address_line_2,
'ACCOUNT CITY' => :account_city,
'ACCOUNT STATE' => :account_state,
'ACCOUNT ZIP CODE' => :account_zip
}
end
在模型中我有
serialize(:column_sequence, Array)
答案 0 :(得分:0)
...更新
切换到jquery bsmselect,非常相似
解决方案是在load_format_column_sequence_select方法中添加一个参数,传入当前顺序(这是一个字符串数组),在方法内部检查它是否设置,如果没有将其设置为空数组。然后将空哈希设置为新的顺序,迭代当前的字符串顺序数组以重新排序原始列表以匹配列顺序。(得到我的同事的信用)。
def load_format_column_sequence_select(current_order)
current_order ||= []
original_order = {
:external_key => 'EXTERNAL KEY',
:external_code => 'EXTERNAL CODE',
:account_type => 'ACCOUNT TYPE',
.........
}
new_order = {}
current_order.each do |column_key|
column_key = column_key.to_sym
if column_name = original_order.delete(column_key)
new_order[column_key] = column_name
end
end
new_order.merge(original_order)
end
并以形式....(传入的@ load_format.column_sequence是保存在数据库中的字符串数组)
= form.fields_for(@load_format) do |builder|
= builder.label "Column Sequence"
= collection_select(:load_format, :column_sequence, load_format_column_sequence_select(@load_format.column_sequence), :first, :last, {}, {:multiple => true})