2

I would like to override the 'items' drop-down list in standard Views by setting more option values (instead of: 20,50,100), when 'can_set_page_size' is set to True.

I thought it would have been an easy task, but I cannot find any way to change the basic behavior. Could you please point me in the right direction?

  • Possible duplicate of [limit choices with dropdown in flask-admin](https://stackoverflow.com/questions/32106940/limit-choices-with-dropdown-in-flask-admin) – MrLeeh Jan 11 '18 at 12:00
  • have a look at form_choices https://flask-admin.readthedocs.io/en/latest/api/mod_contrib_sqla/#flask_admin.contrib.sqla.ModelView.form_choices – MrLeeh Jan 11 '18 at 12:01
  • Thanks for your suggestions, but probably my question was not very focused and confusing. I changed it slightly. The point is that I would like to override an existing drop-down. – Gian Paolo Jesi Jan 22 '18 at 09:47
  • You' ll need to override the default template for this. Did you read through https://flask-admin.readthedocs.io/en/v1.0.7/templates/ already? – MrLeeh Jan 22 '18 at 10:15
  • @MrLeeh , thanks. The key is overriding the _admin/model/list.html_ template in `{{ model_layout.page_size_form(page_size_url) }} `. Unfortunately, I cannot find what object is 'model_layout' as it would be nice to override its method instead of adding some static html as I did! – Gian Paolo Jesi Jan 23 '18 at 15:37

1 Answers1

1

You can override the admin/model/list.html template and replace the macro model_layout.page_size_form() in the body block by your own macro. There you can add some custom values.

{% macro custom_page_size_form(generator, btn_class='dropdown-toggle') %}
    <a class="{{ btn_class }}" data-toggle="dropdown" href="javascript:void(0)">
        {{ page_size }} {{ _gettext('items') }}<b class="caret"></b>
    </a>
    <ul class="dropdown-menu">
        <!-- add your preferred options here -->
        <li><a href="{{ generator(20) }}">20 {{ _gettext('items') }}</a></li>
        <li><a href="{{ generator(50) }}">50 {{ _gettext('items') }}</a></li>
        <li><a href="{{ generator(100) }}">100 {{ _gettext('items') }}</a></li>
    </ul>
{% endmacro %}
MrLeeh
  • 5,321
  • 6
  • 33
  • 51