简体   繁体   中英

FIlter collection_select by value in coffeescript

Well I have this coffeeScript:

jQuery ->
    $('#cmbPab').change ->
       ubis = $('#cmbUbi').html()
       pabellon = $(this).val()
       options = $('#cmbUbi').filter("FilterByValue")

I want it to filter options by value, I already saw railcats stuff but he filters by a different way and I need to do it by value cause value is my id

Here is the cod of both collection_select where cmbPab is my main combo:

        <div class="form-group">
         <%= f.label :Pabellon %>
         <%= f.collection_select :idubicacion, Mtopabellon.all, 
             :codpabellon, :nombre, {prompt: 'Seleccione un pabellón'}, 
             :class=>'form-control', :id => 'cmbPab'%>
        </div>

        <div class="form-group">
          <%= f.label :Ubicacion %>
          <%= f.collection_select :idubicacion, CrUbicacion.all, :id, 
          :nombre, {prompt: 'Seleccione una ubicación'}, :class=>'form-
          control', :id => 'cmbUbi'  %>
        </div>

I believe you're after something along these lines:

$('#cmbPab').change ->
  displayFilterFn = (i, el) -> $(el).val() == 'something?' // do your filtering
  $ubis = $('#cmbUbi')
  $ubis.children().hide()
  $ubis.children().filter(displayFilterFn).show()

Without knowing the specifics of how you want to filter your options, other than that it is based on the value, I can't provide you a filtering function implementation ( displayFilterFn above). The basic idea is to first hide all options, and then show the ones that match your filter.

For more info on the .filter() , see the jQuery API Documentation .

Here is the answer guys! I think this is the best way to do it, maybe not the easier but it works!

https://kernelgarden.wordpress.com/2014/02/26/dynamic-select-boxes-in-rails-4/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM