Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Darryl on Sep 13, 2009 20:31
open dhtmlx forum
userData : I wish I could do this

I've searched, and it looks like this doesn't exist:

In certain cases, I'd like to put complex data into userdata.

In one example, I have a dhtmlx menu, which is loaded from XML.

the function which handles the onClick event for my menu.... pulls several values (from userdata) and stores them in clientside variables.

So my xml looks something like this:

<item id="m_1" text="Contacts">
<userdata name="layout">VLD</userdata>
<userdata name="module">Contacts</userdata>
<userdata name="gridCaption">Contacts and Leads</userdata>
<userdata name="view">All</userdata>
</item>
<item id="m_2" text="Projects">
<userdata name="layout">VLD</userdata>
<userdata name="module">Projects</userdata>
<userdata name="gridCaption">Potential Projects</userdata>
<userdata name="view">Potentials_All</userdata>

</item>

The problem, is that the number of variables passed will change.
I really need to LOOP through the userdata on the client side, and get the "name"=>"value" of each userdata in the collection.

Another usefull option would be to use xml within userdata, something like:

<userdata name="vars">
<dataitem name="var1">value1</dataitem>
<dataitem name="var2">value2</dataitem>
</userdata>

And then the ability to turn the userdata contents into an xml object.

Am I missing something?

i have no problems setting and getting userdata...but I don't have a method for looping through all userdata.

Answer posted by Support on Sep 14, 2009 02:25
There is no public method to achieve such functionality, but the next code can be used 

var el = tree._idpull[ID];
var data = el._userdatalist.split(",");
for (var i=0; i<data; i++)
        alert(data[i] + " = " + tree.getUserData(data[i]));
Answer posted on Sep 14, 2009 09:45
Thanks for that... but in my case, I'm trying to use with a dhtmlxMenu....
My menu is attached to a dhtmlxLayout (obj: parent_layout)

main_menu = parent_layout.attachMenu();
main_menu.setImagePath(dhtmlx_imagePath);
main_menu.loadXML("_menuHandlers/MainMenu.php");
main_menu.attachEvent("onClick", function(id, zoneId, casState){main_menu_handler(id);});


The following code doesn't work for me:

function main_menu_handler(btnID){
     
         var el = main_menu._idpull[btnID];
         var data = el._userdatalist.split(",");
         for (var i=0; i<data; i++)
        alert(data[i] + " = " + main_menu.getUserData(data[i]));
}

Answer posted by Alex (support) on Sep 15, 2009 05:38

Hello,

this approach was recommended for dhtmlxtree. Unfortunately it can not be applied to dhtmlxmenu.

Userdata is stored in the userData array as follows

menu.userData[menu.idPrefix+itemId+"_"+name] = value;

Answer posted on Sep 15, 2009 13:47
Thanks again! 

Here is the quick little function I wrote which gets all the userdata items for a the menu button which was clicked:

 function main_menu_handler(btnID){
         var msgStr="";
         var varStrArr;
         var theData = main_menu.userData;
         for (keyStr in theData){
            varStrArr = keyStr.split(btnID+"_");
            if (varStrArr[1]) {
                  msgStr=msgStr+varStrArr[1]+"=>"+theData[keyStr]+"\n";
            }
         }
         alert (msgStr);

}