Fill in Combobox based on another

Asked

Viewed 39 times

1

I’m designing a website in the Ruby on Rails language. I’m creating all of Javascript now.

I have a javascript question in a Ruby application

I have the following code:

ajaxBar = (unity_id) ->
    $('select#product_bundle_bar_id').empty()
    $.ajax
      url: "populate_bars/#{unity_id}"
      type: "POST"
      dataType: 'json'
      success: (data) ->
        $('select#product_bundle_bar_id').append $("<option>").attr("value", "").text("Selecione o bar")
        bar_value = $("#product_bundle_bar_value").val()
        $.each data, (i, object) ->
          $('select#product_bundle_bar_id').append $("<option>").attr("value", object.id).text(object.name)
          $("select#product_bundle_bar_id option[value=#{bar_value}]").attr('selected', 'selected') if unity_id?
        return

In this site there are several combobox that has Regional, Unit, Bar and Totem. When choosing the regional he returns me the units located within the regional, when I choose the unit he returns me all the bars located in this regional.

Each bar has only one Totem, so I would like to make the user when choosing the Bar Totem to be filled automatically with the name of the Totem that is associated with the Bar.

After the Ajaxbar code I have

ajaxTotem = (bar_id) ->
    $('select#product_bundle_totem_id').empty()
    $.ajax
      url: "populate_totems/#{bar_id}"
      type: "POST"
      dataType: 'json'
      success: (data) ->
        $('select#product_bundle_totem_id').append $("<option>").attr("value", "").text("Selecione o totem")
        totem_value = $("#product_bundle_totem_value").val()
        $.each data, (i, object) ->
          $('select#product_bundle_totem_id').append $("<option>").attr("value", object.id).text(object.name)
          $("select#product_bundle_totem_id option[value=#{totem_value}]").attr('selected', 'selected') if bar_id?
        return

The code of Ajaxtotem makes me return the Totem according to the Bar, only it does not fill automatically, the user has to choose.

How do I do this? So that when choosing the Bar, Totem does a search and automatically fills according to the Id and Name of the Totem located in the bar?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.