hellion
hellion

Reputation: 4840

how to add a data-source attribute for x-editable in rails

How, in rails, do you supply a dynamic hash to the source data attribute that x-editable accepts?

  <a href="#" data-xeditable="true" data-type="select" data-pk="<%= g.id %>" data-model="graded_item" data-name="teacher_grade_id" data-url="<%= graded_item_url(g) %>" data-source="<%= g.gradescale.grade_scale_items.map{ |x| "{x.id, x.grade}" } %>" data-title="Edit Grade">

      <%= g.teacher_grade.try(:grade) %> - <%= g.teacher_grade.try(:name) %>
  </a>

The docs want a hash like { value: id, text: name } but I can't seem to get a hash that doesn't result in a red inactive select menu (x-editable doesn't like the source).

Anyone have an example of a dynamic data-source for x-editable in rails?

Per docs

If array - it should be in format: [{value: 1, text: "text1"}, {value: 2, text: "text2"}, ...]

For compatibility, object format is also supported: {"1": "text1", "2": "text2" ...} but it does not guarantee elements order.

update

This created an instance variable that is an array of hashes in the exact format the docs says is needed for an array data-source. But, this also results in an error empty select in the editable popup.

  @grade_scale_items = []
  @course.gradescale.grade_scale_items.each do |x| 
      @grade_scale_items << {value: x.id, text: "#{x.grade} - #{x.name}"}
  end

Upvotes: 0

Views: 2017

Answers (1)

hellion
hellion

Reputation: 4840

Pretty simple fix...after digging a bit. The docs aren't real helpful with this, and the rails examples only uses text fields (no select menus). Convert the array of hashes to json...like so.

  @grade_scale_items = []
  @course.gradescale.grade_scale_items.each do |x| 
     @grade_scale_items << {value: x.id, text: "#{x.grade} - #{x.name}"}
  end

  view: data-source="<%= @grade_scale_items.to_json %>"

This puts it in a format that x-editable is happy with as a data-source

Upvotes: 2

Related Questions