symfonyselectdropdownknppaginator

knp paginator sortable in select dropdown


I changed sortable links layout to a select dropdown. However I don't know how to get the selected option to be selected.

This is my code so far. I changed the sortable.link htwl twig to this

<option{% for attr, value in options %} {% if attr == 'href' %}value="{{ value }}" {% else %}{{ attr }}="{{ value }}"{% endif %}{% endfor %} >{{ title }}</option>

and my twig file looks like this:

<select onChange="window.location=this.value;">
{{ knp_pagination_sortable(properties, 'Title A-Z', 'a.title', {}, {'direction': 'asc'}) }}
{{ knp_pagination_sortable(properties, 'Title Z-A', 'a.title', {}, {'direction': 'desc'}) }}
</select>

The dropdown onchange works, but it doesn't show the selected option when loading again. Please advice :) Maybe this is also not the best way to set up the select dropdown. Any improvements of my code would be very much appreciated


edit

i looked at the other template examples and saw that you can grab direction and order, it doesn't work completelly yet with the asc/desc it goes wrong. This is my updated option code.

<option{% for attr, value in options %} {% if attr == 'href' %}value="{{ value }}" {% else %}{{ attr }}="{{ value }}"{% endif %}{% endfor %}  {% if app.request.get('direction') == 'desc' and app.request.get('sort') == key  %} selected="selected" {% elseif app.request.get('direction') == 'asc' and app.request.get('sort') == key %} selected="selected"{% else%} no{% endif %}>{{ title }}</option>

Then my updated select on twig

 <select onChange="window.location=this.value;">
    {{ knp_pagination_sortable(properties, 'Title A-Z', 'a.title', {}, {'direction': 'asc'}) }}
    {{ knp_pagination_sortable(properties, 'Title Z-A', 'a.title', {}, {'direction': 'desc'}) }}
    {{ knp_pagination_sortable(properties, 'price (low to high)', 'a.price', {}, {'direction': 'asc'}) }}
    {{ knp_pagination_sortable(properties, 'Price (high to low)', 'a.price', {}, {'direction': 'desc'}) }}
    </select>

Help is appreciated. Thanks


Solution

  • I think i can anwser my own question. I just had to grab direction instead of desc/asc.

    I think it's working :)

    <option{% for attr, value in options %} {% if attr == 'href' %}value="{{ value }}" {% else %}{{ attr }}="{{ value }}"{% endif %}{% endfor %}  {% if app.request.get('direction') == direction and app.request.get('sort') == key  %} selected="selected" {% endif %}>{{ title }}</option>