diff options
| author | Teddy Wing | 2018-04-19 13:59:41 +0200 | 
|---|---|---|
| committer | Johan Van Ryseghem | 2018-04-26 09:16:15 +0200 | 
| commit | e5215f229e3dfa5d84a43bcf75e2edf8c99667a3 (patch) | |
| tree | f4d18422ecbcda411e7382eff94fea677ce4f606 | |
| parent | 37cef4eca7eec2dbfc4be10334c787ce207b466e (diff) | |
| download | chouette-core-e5215f229e3dfa5d84a43bcf75e2edf8c99667a3.tar.bz2 | |
ReferentialVehicleJourneys: Show selected line in drop-down
Previously we weren't showing the currently-selected line filter in the
drop-down label.
If a line is selected, get its ID and name and use those to pre-fill the
select.
Using Select2's `initSelection` option to configure this instead of
putting `selected: ` in the Rails field definition. That would have
looked like this:
    collection: [[@filtered_line.name, @filtered_line.id]],
    selected: params[:q] && params[:q][:route_line_id_eq],
But doing it that way wasn't possible because this is an AJAX-populated
Select2.
Since we already have a way to use `initSelection`, this was the
quickest and easiest way to go, but apparently Select2 v4.0 deprecated
this option, instead doing it with a data adapter. Couldn't really be
bothered right now to figure out how to set that up as it seemed like a
lot of work for not much payoff right now. Also, I was a little
disappointed in Select2's docs regarding this. From what I understood,
it looks like you basically need to create your own custom data adapter
to do this, which sounds like a pain, and I'm not sure if I would have
to mess with the rest of our existing Select2 configuration setup in
order to integrate that.
Refs #5889
| -rw-r--r-- | app/controllers/referential_vehicle_journeys_controller.rb | 10 | ||||
| -rw-r--r-- | app/views/referential_vehicle_journeys/_filters.html.slim | 6 | 
2 files changed, 15 insertions, 1 deletions
| diff --git a/app/controllers/referential_vehicle_journeys_controller.rb b/app/controllers/referential_vehicle_journeys_controller.rb index b07d6c600..c35a06087 100644 --- a/app/controllers/referential_vehicle_journeys_controller.rb +++ b/app/controllers/referential_vehicle_journeys_controller.rb @@ -12,6 +12,16 @@ class ReferentialVehicleJourneysController < ChouetteController    requires_feature :referential_vehicle_journeys +  def index +    if params[:q] && params[:q][:route_line_id_eq].present? +      @filtered_line = Chouette::Line +        .select(:id, :name) +        .find(params[:q][:route_line_id_eq]) +    end + +    index! +  end +    private    def collection diff --git a/app/views/referential_vehicle_journeys/_filters.html.slim b/app/views/referential_vehicle_journeys/_filters.html.slim index f9fa4fcf7..8cd17ca0a 100644 --- a/app/views/referential_vehicle_journeys/_filters.html.slim +++ b/app/views/referential_vehicle_journeys/_filters.html.slim @@ -29,7 +29,11 @@                  'select2-ajax': 'true',                  'select2ed-placeholder': t('referentials.filters.line'),                  url: autocomplete_referential_lines_path(@referential, format: :json), -                'select2ed-allow-clear': true \ +                'select2ed-allow-clear': true, \ +                initvalue: { \ +                  id: @filtered_line&.id, +                  text: @filtered_line&.name \ +                } \                } \              },              label: false, | 
