动态添加行 AdvancedDataGrid Flex
我有一个从 html 表动态构建的 AdvancedDataGrid。 html 由服务器提供,因此我的代码必须针对不同的列/行动态工作。
我已经构建了列并且它们显示正确,但是当我开始添加行时我遇到了问题。下面的代码片段显示了迭代所有列并将每个列下的值添加到一个对象(以形成完整的行),然后将其添加到 ArrayCollection ,稍后将其设置为 AdvancedDataGrid 的 dataProvider
//create an item to work with
var chartItem:Object = new Object();
for( var j:int = 0; j < columnResult.length ; j++ )
{
//this is the data that goes under the column (headerArray)
var item:String = removeformat(removetd(columnResult[j]));
//grab the header (this is which column the value will be added
var head:String = headerArray[j];
//set the value to header (pair)
chartItem[head] = item;
}
//add the chartItem (row) to the main collection
arr.addItem(chartItem);
我的问题是当“head”的值为 0 时(如列标题为“0”),该项目将添加到位置 [0],而不是作为字符串添加到 0。
我查找了一些示例并尝试使用:
chartItem.head 但这只是假设列标题是“head”而不是获取 head var 的值
I have an AdvancedDataGrid being built dynamically from an html table. The html is provided by a server so my code has to work dynamically for different columns/rows.
I have the columns being built and they display properly, however when I get to adding the rows is where i have issues. the follow code snippet shows iterating over all of the columns and adding a value under each of those columns to an object (to make a complete row) and then adding that to the ArrayCollection that later gets set to the dataProvider for the AdvancedDataGrid
//create an item to work with
var chartItem:Object = new Object();
for( var j:int = 0; j < columnResult.length ; j++ )
{
//this is the data that goes under the column (headerArray)
var item:String = removeformat(removetd(columnResult[j]));
//grab the header (this is which column the value will be added
var head:String = headerArray[j];
//set the value to header (pair)
chartItem[head] = item;
}
//add the chartItem (row) to the main collection
arr.addItem(chartItem);
my issue is that when "head" has a value of 0, as in the column title is '0', the item is added at position [0] instead of at 0 as a string.
I looked up some examples and tried with:
chartItem.head but that just assumes the column title is 'head' instead of grabbing the value of the head var
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法在关联数组中使用数字作为索引。
大多数时候,关联数组中的索引只是对象的变量名。这就是为什么您可以使用
obj["someKey"]
或obj.someKey
访问数据。当然,您可以在键中使用变量名称中不允许的字符(例如空格、特殊符号)。但是,您只能使用方括号访问这些内容,而不能使用点(obj["foo/bar"]
可以,obj.foo/bar
不起作用)。所以,不建议这样做......好吧,回到你的问题:我建议你在所有列名称前加上一个字符(例如使用“_0”)。由于您动态创建 AdvancedDataGrid 列,因此这应该不是问题。您可以显式设置这些列的
headerText
,以便在列标题中仍然显示“0”而不是“_0”。It is not possible to use numbers as an index in associative arrays.
Most of the time the index in an associative array is just a variable name of an Object. That's why you can access the data either with
obj["someKey"]
orobj.someKey
. Of course you can use characters within your keys that are not allowed within variable names (like spaces, special symbols). However, you can access those only with brackets, not with dot (obj["foo/bar"]
works,obj.foo/bar
won't work). So, this is not recommended...Well, back to your problem: I'd suggest you prefix all your column names with a character (e.g. use "_0"). Since you create the AdvancedDataGrid columns dynamically that shouldn't be a problem. You can explicityl set the
headerText
of those columns in order to still show "0" in the column header instead of "_0".