XMLListCollection查找索引值

时间:2012-05-28 23:16:05

标签: actionscript-3 flex flash-builder

我想在XMLListCollection上找到特定的值。

我尝试使用这样的东西,但它不起作用!

var xmllisteRDV:XMLList= XML(event.result).RDVClinik;
xmlCollSuivi = new XMLListCollection(xmllisteRDV);
var index:Number = -1;
for(var i:Number = 0; i < xmllisteRDV.length(); i++)
{
    if(XML(xmllisteRDV[i]).@grDateDeb == todayDate)
    {
        index = i;
        break;
    }
}

2 个答案:

答案 0 :(得分:0)

首先尝试指出原始代码中的错误:

var xmllisteRDV:XMLList= XML(event.result).RDVClinik;  //Unnecessary cast, event.result is Object compiler will not check or know the run-time type, doesn't care because Object is declared dynamic meaning properties can be added to it dynamically, if RDVClinik didn't exist on the particular Object type it would simply be null casting as XML gives it no information about this "property"
xmlCollSuivi = new XMLListCollection(xmllisteRDV);
 var index:Number = -1;
        for(var i:Number = 0; i < xmllisteRDV.length(); i++) //length is a property not a method on XMLListCollection this should throw a compile time error
        {
            if(XML(xmllisteRDV[i]).@grDateDeb == todayDate)// I see no type when debugging for the result of xmllisteRDV[i] not positive here but this cast is at the least unnecessary
            {
                index = i;
                break;
            }
        }

这是我认为可能会改变todayDate构建方式的版本

var date:Date = new Date();
var todayDate:String = date.dateUTC+"/"+date.dayUTC+"/"+date.fullYear;
var index:int=-1;
for(var i:int = 0; i < flex3Projects.length; i++)
{
    trace(xmllisteRDV[i].@grDateDeb)
    if(xmllisteRDV[i].@grDateDeb.toString() == todayDate)
    {
        index = i;
        break;
    }
}

答案 1 :(得分:0)

在你的帮助下,我找到了解决方案

private function setSelectedItem():void
{
    var gData:Object = dgSuiviClini.dataProvider;
    var todayDate:String= new DateUtility().DateAsToString(new Date());

    for(var i:Number=0; i < gData.length; i++)
    {
        var thisObj:Object = gData.getItemAt(i);
        if(thisObj.grDateDeb == todayDate)
        {
            dgSuiviClini.selectedIndex = i;
            //sometimes scrollToIndex doesnt work if validateNow() not done
            dgSuiviClini.validateNow();
                //dgSuiviClini.scrollToIndex(i);
        }
        else{
            dgSuiviClini.validateNow();
                // dgSuiviClini.scrollToIndex(gData.length);


        }
    }
    dgSuiviClini.validateNow();
    dgSuiviClini.editedItemPosition = { rowIndex: gData.length-1, columnIndex: nColSaisie };


}

由于