wiki:AlphaSort

Version 2 (modified by Fran Boon, 15 years ago) ( diff )

--

AlphaSort

Want to be able to sort a Dropdown alphabetically to make it easier for data entry e.g. the Organisations dropdown in 'Add Office'

We have 2 sorts of dropdowns:

  • Database
  • Dictionary

This one is a Database one using the IS_ONE_OF() custom validator from modules/validators.pty

Massimo has put back-end sorting into Trunk:

  • IS_IN_SET(...,sort=True) and/or IS_IN_DB(...,sort=True)
    • needs porting to IS_ONE_OF()
    • needs testing

Foront-end soring can be done with JS:

  • http://stackoverflow.com/questions/278089/javascript-to-sort-contents-of-select-element/278509#278509
  • This works with capitalised/non merged but means that the NULL in IS_NULL_OR() isn't the default:
    <script>
    jQuery(document).ready(function(){
      jQuery("select").each(function(){
         jQuery(this).html(jQuery(this).children("option").sort(function(a,b) {
           return (a.text.toUpperCase() == b.text.toUpperCase())?0:((a.text.toUpperCase() < b.text.toUpperCase())?-1:1);
          }));
          // jQuery(this).children("option:first").attr("selected","selected");
      });
    });
    </script>
    

Q: Where does one put page-specific Javascript ?
A: views/module/resource_method.html

For Dictionary-based ones can:

In  models/05_or.py change:

or_organisation_type_opts = {
   1:T('Government'),
   2:T('International Governmental Organization'),
   3:T('International NGO'),
   4:T('Misc'),
   5:T('National Institution'),
   6:T('National NGO'),
   7:T('United Nations')
   }
...
db[table].type.requires = IS_NULL_OR(IS_IN_SET(or_organisation_type_opts))

into

or_organisation_type_opts = [
   (1,T('Government')),
   (2,T('International Governmental Organization')),
   (3,T('International NGO')),
   (4,T('Misc')),
   (5,T('National Institution')),
   (6,T('National NGO')),
   (7,T('United Nations'))
]
...
db[table].type.requires = IS_NULL_OR(IS_IN_SET(
   [x[0] for x in or_organisation_type_opts],
   [x[1] for x in or_organisation_type_opts]))
Note: See TracWiki for help on using the wiki.