Cannot select certain rows in List component or DataGrid component
The problem is that for each item in data provider, we must get a unique ID (UID) for it, if two or more items in data provider get the same UID after call itemToUID method, then we only can select one of them and can never successful to select the rest of them. To avoid this problem, refer to the following itemToUID method to understand how Flex components generate the UID and avoid generating the same UID.
Please Note, the following method itemToUID are quoted from class ListBase of SDK source code.
We can use: copyOfOldObject= ObjectUtil.copy(oldObject) to get a new instance, then when we add the copy to the ArrayCollection which is the binding source of dataProvider, we will get different UID for each instance, then we won’t have the problem.
/**
* Determines the UID for a data provider item. All items
* in a data provider must either have a unique ID (UID)
* or one will be generated and associated with it. This
* means that you cannot have an object or scalar value
* appear twice in a data provider. For example, the following
* data provider is not supported because the value "foo"
* appears twice and the UID for a string is the string itself:
*
* <blockquote>
* <code>var sampleDP:Array = ["foo", "bar", "foo"]</code>
* </blockquote>
*
* Simple dynamic objects can appear twice if they are two
* separate instances. The following is supported because
* each of the instances will be given a different UID because
* they are different objects:
*
* <blockquote>
* <code>var sampleDP:Array = [{label: "foo"}, {label: "foo"}]</code>
* </blockquote>
*
* Note that the following is not supported because the same instance
* appears twice.
*
* <blockquote>
* <code>var foo:Object = {label: "foo"};
* sampleDP:Array = [foo, foo];</code>
* </blockquote>
*
* @param data The data provider item.
*
* @return The UID as a string.
*/
protected function itemToUID(data:Object):String
{
if (data == null)
return "null";
return UIDUtil.getUID(data);
}
The problem is that for each item in data provider, we must get a unique ID (UID) for it, if two or more items in data provider get the same UID after call itemToUID method, then we only can select one of them and can never successful to select the rest of them. To avoid this problem, refer to the following itemToUID method to understand how Flex components generate the UID and avoid generating the same UID.
Please Note, the following method itemToUID are quoted from class ListBase of SDK source code.
We can use: copyOfOldObject= ObjectUtil.copy(oldObject) to get a new instance, then when we add the copy to the ArrayCollection which is the binding source of dataProvider, we will get different UID for each instance, then we won’t have the problem.
/**
* Determines the UID for a data provider item. All items
* in a data provider must either have a unique ID (UID)
* or one will be generated and associated with it. This
* means that you cannot have an object or scalar value
* appear twice in a data provider. For example, the following
* data provider is not supported because the value "foo"
* appears twice and the UID for a string is the string itself:
*
* <blockquote>
* <code>var sampleDP:Array = ["foo", "bar", "foo"]</code>
* </blockquote>
*
* Simple dynamic objects can appear twice if they are two
* separate instances. The following is supported because
* each of the instances will be given a different UID because
* they are different objects:
*
* <blockquote>
* <code>var sampleDP:Array = [{label: "foo"}, {label: "foo"}]</code>
* </blockquote>
*
* Note that the following is not supported because the same instance
* appears twice.
*
* <blockquote>
* <code>var foo:Object = {label: "foo"};
* sampleDP:Array = [foo, foo];</code>
* </blockquote>
*
* @param data The data provider item.
*
* @return The UID as a string.
*/
protected function itemToUID(data:Object):String
{
if (data == null)
return "null";
return UIDUtil.getUID(data);
}
本文介绍了Flex框架中组件如何生成唯一标识符(UID),确保数据提供者中的每一项都有唯一的UID,避免了在List或DataGrid组件中选择特定行时出现的问题。详细解释了简单动态对象重复时的UID分配原则,并提供了itemToUID方法的具体实现。

被折叠的 条评论
为什么被折叠?



