Question

I have a list controller in my Dashcode project, it pulls its data from a dynamic source.

After my list controller has loaded its data I'd like to set it's selected index to 0 - so that information for the first item in the list is shown. I can't for the life of me figure out how to do this. I've tried:

function load()
{
    dashcode.setupParts();
    var list = document.getElementById("itemsList");
    //list.setSelectionIndexes(0); // nope
    //list.selectedIndex = 0; // nope
}
Was it helpful?

Solution

No need to write any code for that. Edit the properties for the list in the inspector and uncheck allow empty selection in the attributes tab. With no null selection allowed the first item in the list will be selected when the list loads.

OTHER TIPS

Here's an example for selecting a specific item based on its name. For this question, you could simply explicitly set the 0 index - I think Vicente's answer is more appropriate for the question asked, but this can be useful for selecting an item based on a querystring, etc.

function load()
{
    dashcode.setupParts();

    setSelectedItem();
}

function setSelectedItem()
{
    var itemSource = dashcode.getDataSource("itemsSource");
    var inProgress = itemSource.valueForKey("queryInProgress");

    if(inProgress)
    {
        setTimeout("setSelectedItem()", 200);
    }
    else
    {
        var items = itemSource.valueForKeyPath("content.items")
        for(i=0;i<items.length;i++)
        {
            if(items[i].name.toLowerCase() == "specificItemName")
            {
                document.getElementById("list").object.setSelectionIndexes([i]);
            }
        }
    }
}
function load()
{
    dashcode.setupParts();
    var list = document.getElementById("itemsList");
    list.setSelectionIndexes([0]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top