select2不保存编辑的输入

Posted

技术标签:

【中文标题】select2不保存编辑的输入【英文标题】:select2 not saving input for Edit 【发布时间】:2016-03-21 18:54:32 【问题描述】:

由于某种原因,当用户去编辑一个条目时,他通过.select2 输入的数据没有显示出来。但数据已保存。

irb(main):001:0> Routine.find(1)
=> #<Routine id: 1, missed_days: 0, date_started: "2015-10-24 04:00:00", trigger: "brew tea", action: "stretch", user_id: 1, created_at: "2015-11-24 21:55:25", updated_at: "2015-12-14 21:00:09", committed: ["sun", "mon", "tue", "wed", "thu", "fri", "sat", ""], days_challenged: 30>

show.html.erb

edit.html.erb

_form.html.erb

<%= simple_form_for(@routine, remote: request.xhr?, html:  data:  modal: true  ) do |f| %> 
  <%= f.date_select :date_started, :order => [:month, :day, :year], class: 'date-select' %>
  <%= f.collection_check_boxes :committed, Date::ABBR_DAYNAMES, :downcase, :to_s %>
  <%= f.number_field :days_challenged, value: 30, class: 'day-challenge' %> <b>Day Challenge
  <label>After I</label> 
    <%= f.grouped_collection_select(:trigger, @trigger, :last, :first, :to_s, :to_s, include_blank: true) %><font color="#DDD">,</font>
  <label>I will</label> 
    <%= f.grouped_collection_select(:action, @action, :last, :first, :to_s, :to_s, include_blank: true) %>.
  <%= button_tag(type: 'submit', class: "btn")  do %>
   Save
  <% end %>
<% end %>

<script>
  $("#routine_trigger").select2(
    placeholder: "Existing Habit (Optional)",
    theme: "classic",
    allowClear: false,
    tags: true,
    multiple: false,
  );
  $("#routine_action").select2(
    placeholder: "Enter Challenge",
    allowClear: false,
    tags: true,
    multiple: false,
  );
</script>

routines_controller

  def new
    @trigger = 
    [
    ['Morning', ['Get Out of Bed', 'Clean Up Breakfast', 'Brush My Teeth', 'Sit at Desk', 'Start My Computer']], 
    ['Afternoon', ['Drink Coffee', 'Read Email', 'Eat Lunch', 'Use the Bathroom', 'Go for a Walk']],
    ['Evening', ['Enter My Home', 'Eat a Snack', 'Make a Drink', 'Shower', 'Finish Dinner']]
    ]
    @action = 
    [
    ['Mind', ['Write 500 Words', 'Read a Chapter', 'Study 20 min', 'Watch a Ted Talk', 'Take a Picture']], 
    ['Body', ['Do 25 Pushups', 'Juice Cleanse', 'Walk 10,000 Steps', 'Exercise', 'Eat an Apple']],
    ['Spirit', ['Meditate', 'Write 3 Gratitudes', 'Journal', 'Not Complain', 'Do a Random Act of Kindness']]
    ]
    if current_user == nil
      @routine = Routine.new
    else
      @routine = current_user.routines.build
      respond_modal_with @routine
    end
  end

  def edit
    @trigger = 
    [
    ['Morning', ['Get Out of Bed', 'Clean Up Breakfast', 'Brush My Teeth', 'Sit at Desk', 'Start My Computer']], 
    ['Afternoon', ['Drink Coffee', 'Read Email', 'Eat Lunch', 'Use the Bathroom', 'Go for a Walk']],
    ['Evening', ['Enter My Home', 'Eat a Snack', 'Make a Drink', 'Shower', 'Finish Dinner']]
    ]
    @action = 
    [
    ['Mind', ['Write 500 Words', 'Read a Chapter', 'Study 20 min', 'Watch a Ted Talk', 'Take a Picture']], 
    ['Body', ['Do 25 Pushups', 'Juice Cleanse', 'Walk 10,000 Steps', 'Exercise', 'Eat an Apple']],
    ['Spirit', ['Meditate', 'Write 3 Gratitudes', 'Journal', 'Not Complain', 'Do a Random Act of Kindness']]
    ]
    respond_modal_with @routine
  end

【问题讨论】:

你试过selected: f.object.something吗? 提交成功后是否重置select2值? 如果我在编辑表单上点击保存,那么它将重置任何值,除非我将值放回 @xkcd149 我的意思是在 html 的 selected 选项中 你能提供一个小提琴让我们看看吗? 【参考方案1】:

这就是我在 select2 字段上所做的事情

<%= f.input :field_name, as: :select, collection: f.object.field_name, include_blank: false, selected: f.object.field_name, input_html:  class: "json_data" , %>

json_data 是我如何将文本字段变成选择 2

咖啡

$('.json_datas').before ()->
    "<input name='#@.name' type='hidden' />"
  $('.json_data').select2
    allowClear: true,
    placeholder: "Select a value",
    ajax:
      url: '/api/call_to_data'
      dataType: 'json'
      delay: 250
      data: (params) ->
        
          q: params.term
          page: params.page
        
      processResults: (data, page) ->
        # parse the results into the format expected by Select2.
        # since we are using custom formatting functions we do not need to
        # alter the remote JSON data
         results: data.items 
      cache: true

希望对你有帮助

【讨论】:

我在这里为您的代码苦苦挣扎。 grouped_collection_select有没有办法做到这一点? 我所做的是我有一个常规文本字段,然后 select2 将其变成一个选择字段。重要的是要知道我使用了一个名为json_data 的类,jquery 会对它执行操作。使用 select2 时,我喜欢包含一个与我想使用 select2 的字段同名的隐藏字段,以防出现 nil 值。但重要的是要知道填充 vales 的数据来自 ajax url【参考方案2】:

由于您对选择字段使用 grouped_collection_select 方法,因此在选项散列中传递 :selected 值以进行选择会更有意义。显然,当您从 new.html.erb 调用部分时,您可以将其作为空白传递,当您从 edit.html.erb 调用部分进行编辑时,您可以传递一些值来选择。 例如grouped_collection_select(:city, :country_id, @continents, :countries, :name, :id, :name, :selected =&gt; [1, 5, 6 ] )

阅读更多:http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/grouped_collection_select#1538-Preselecting-options

【讨论】:

当然,然后在渲染表单进行编辑时将 :selected 设置为您想要的值,否则将 :selected 数组留空。 由于您使用的是部分表单,您可以传递一个带有 :selected 选项值的变量。从 new.html.erb 调用部分表单时,将此变量作为 [] 发送,并在从 edit.html.erb 调用表单部分时将此变量作为 :selected 选项的 [some_value] 传递。因此,选择框中的值只会在编辑表单时出现。希望,它有帮助!

以上是关于select2不保存编辑的输入的主要内容,如果未能解决你的问题,请参考以下文章

从 JSON 请求中自动选择 Select2:使用数据表编辑器

select2 initSelection 用于重新显示表单

在 select2 中捕获输入键

select2 required oninvalid custom text not working

如何从 Select2 Widget Yii2 将多个值保存到数据库

Select2 已输入的过帐值