Hi all,

I want to share with you some code that came very handy in my project while
working with lists and entries in listbox and selectbox widgets.
My application supports english and german and this creates a problem if you
let the user select e.g. an unit like hour or day from a selectbox. Saving
the text from the selected Item back to the database or do string
comparision is not realy an option as you will end up with localized values
you have to check against and this will complicate your code once you want
to add another language.
Using a lookup table to match a value from the database against a text
string and vica versa complicates the translation process as you have to
translate the po files and the lookup table and keep them in sync.
The easy solution is to use an integer as alias which represents the
selected value - independent from language - and assign such an alias as
user data to the qooxdoo listitem
E.g.
For a selectbox where the user can select between day/week/month I define
the alias as follows:
day|1;week|2;month|3. The "|" seperates the value from the alias
in the javascript code this would look like

var tmpValues = this.tr("day|1;week|2;month|3");

above is the string you have to translate - that's all. Adding another
language has no effect on the code. I then call a function from my selectbox
class to split the string and add the values to the selectbox

mySelectbox.setEntriesAliasFromList(tmpValues);
    //=====================================================================
    setEntriesAliasFromList : function(strList, defSelection, append) {
//=====================================================================
      var seperator = ";";
      var tmpItem = '';
      var aliasSeperator = "|";

      this._strList = strList;
      this._default = defSelection;

     //clear Listbox first to avoid redundant entries
      if (!append) {
        this.removeAll();
      }

      var allEntries = strList.split(seperator);

      for (var i=0; i<allEntries.length; i++)
      {
        var entry = allEntries[i].split(aliasSeperator);
        tmpItem = new qx.ui.form.ListItem(entry[0]);
        tmpItem.setUserData("alias", entry[1]);
        this.add(tmpItem);

        if (entry[0] == defSelection) {
          this.setSelection([ tmpItem ]);
        }
      }
    },

In order to get the selection I implemented a getAlias function which
returns the alias for the selected item - which is language independend
    //=====================================================================
    getAlias : function() {
//=====================================================================
      var tmpSelectionStr = "";
      var tmpSelection = this.getSelection();

      if (tmpSelection.length == 0) {
        return '';
      }
      else {
        for (var i=0; i<tmpSelection.length; i++) {
          if (i > 0) {
            tmpSelectionStr += ";";
          }
          tmpSelectionStr += tmpSelection[i].getUserData("alias");
        }
        return tmpSelectionStr;
      }
    },

I attach you my class file for the selectbox as sample as it has some more
functions (e.g. setSelectionFromLabel) you may find useful working with
values/strings received from a backend 

regards

Raimund 
http://qooxdoo.678.n2.nabble.com/file/n5830932/sntFldSelectBox.js
sntFldSelectBox.js 
-- 
View this message in context: 
http://qooxdoo.678.n2.nabble.com/Working-with-alias-in-listbox-selectbox-tp5830932p5830932.html
Sent from the qooxdoo mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Oracle to DB2 Conversion Guide: Learn learn about native support for PL/SQL,
new data types, scalar functions, improved concurrency, built-in packages, 
OCI, SQL*Plus, data movement tools, best practices and more.
http://p.sf.net/sfu/oracle-sfdev2dev 
_______________________________________________
qooxdoo-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

Reply via email to