V
V
vadimstroganov2016-01-04 00:19:21
JavaScript
vadimstroganov, 2016-01-04 00:19:21

How to add new options to select in Rails_admin via js/coffee?

Hey!
I'm trying to make dependent selects using js/coffee. After selecting a value from the first select, I send a request for new options for the second select. After that, using append, I append them to the second select. But for some reason, they are not displayed in rails_admin if you try to open the second select. Has anyone experienced something similar? I would be grateful for any help!
dependent_pages.js.coffee:

$(document).ready ->
  $('#parent_site').change ->
    $.ajax '/admin/get_depended_pages?site_id=' + this.value,
      type: 'POST'
      dataType: 'html'
      error: (jqXHR, textStatus, errorThrown) ->
        alert("#{textStatus}: #{errorThrown}")
      success: (data, textStatus, jqXHR) ->
        pages = $('#page_parent_id')
        pages.empty()
        data = jQuery.parseJSON(data)
        for element in data
          console.log element
          pages.append($("<option></option>").attr("value", element[1]).text(element[0])).html()

Before selecting the element in the first select(#parent_site):
a48f881e-b277-11e5-8b59-a36584f83d56.png
After selecting the element (you can see that append happened, but the list is not expanded, as if there is nothing):
ab2b5d74-b277-11e5-8397-563b1bbba597.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima, 2016-01-04
@MAXOPKA

There, most likely, some kind of bootstrap trick is used.
It needs to be updated after adding an option, like this:$(pages).selectpicker('refresh')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question