Question

I am developing a worklight project using DOJO toolkit.Since i am a newbie in Worklight and Dojo i just created a list and trying to get get the index of clicked list,so that i can show corresponding description/action in the next view. i tried passing index value in listItem "onClick " function,but after passing index parameter i cant move to anothe view.

HTML File

<div data-dojo-type="dojox.mobile.ScrollableView" id="view0"
        data-dojo-props="selected:false,scrollDir:'v'">
        <div data-dojo-type="dojox.mobile.Heading"
            data-dojo-props="label:'View0'" ></div>
        <ul data-dojo-type="dojox.mobile.RoundRectList" id="dataList">

        </ul>
<div data-dojo-type="dojox.mobile.Heading"
            data-dojo-props="fixed:'bottom'"></div>
    </div>

JS File

function abcd()
    {
    ..
    ...
    ...
     var dlist = dijit.registry.byId("view0");
    for(var i=1;i<=3;i++)
{
    var lstId = "list" + i;
    var list = new dojox.mobile.ListItem({label: labl,rightIcon:"mblDomButtonBlueCircleArrow",transition:"slide",id:lstId,onClick:dispData(lstId),moveTo:"#"});
}
                    dlist.addChild(list);


    }

    function dispData(index)
    {
        //debugger;
        alert(index);
        dijit.registry.byId("view0").performTransition("**someview***", 1, "slide");
    }

How to get the index value of selected list in dojo.Is there any other method to do this.

Any help is appreciated.

Was it helpful?

Solution

If you are using the loop index as a closure variable for a click handler being defined in the loop ... that isn't going to work. (see JavaScript closure inside loops – simple practical example for example)

I find it easier to just add the id to the ListItem and then use the event to find the item in a single click handler:

        function listItemHandler(event) {
            // Look up the clicked ListItem and get its id 
            var id = dijit.registry
                    .getEnclosingWidget(event.currentTarget).id;

            alert(id);
        }

        var dlist = dijit.registry.byId("dataList");
        for ( var i = 1; i <= 3; i++) {
            var lstId = "list" + i;
            var list = new dojox.mobile.ListItem({
                label : "label" + lstId,
                rightIcon : "mblDomButtonBlueCircleArrow",
                transition : "slide",
                id : lstId,
                moveTo : "#"
            });
            dlist.addChild(list);
        }
        dojo.query("#dataList .mblListItem").on("click", listItemHandler);

OTHER TIPS

Build the row such:

..'label': 'xx', 'moveTo':'#', 'onClick':function(){  yourFunction('id_of_item')}   ..

Use this in function for move to another view

var w = dijit.byId('currentView');
w.performTransition('newView',1,"slide",null); //or #newView

it works for me. The alert is displayed? It enter in function?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top