【问题标题】:Flex Data Grid - Prevent Duplicates before they happen?Flex 数据网格 - 防止重复发生?
【发布时间】:2013-11-06 15:08:40
【问题描述】:

我有一个填充信息数组的数据网格。数据网格由用户使用,因此他们可以在数据网格上添加记录或删除记录。示例:如果用户添加以下信息:“名称:Apples / 描述:水果” 如果此信息已存在于数据网格中,我如何防止他们再次添加它?可能会提示“此项目已列出,请重试”。任何人对我如何使代码对我有利的想法?

功能:

        public function addRow():void {
        var st:AttributeVO = AttributeVO(attCombo.selectedItem);
        st.countryCode = countriesAvailable.selectedLabel;
        var nt:AttributeVO = st.clone();
        var list:ArrayCollection = model.selectedCategory.tai;
        nt.attributeValue = "";
        list.addItem(nt);
        templatePropertiesDG.invalidateList();
    }


    <mx:HBox>
            <mx:ComboBox  id="attCombo" dataProvider="{model.selectedCategory.completeList}" width="300" prompt="Select a Template Attribute" enabled="{model.userInEditMode}" labelField="attributeName" />
            <mx:Button id="addButton" click="addRow();" styleName="addButtonOff" enabled="{model.userInEditMode}" label="ADD" />
    </mx:HBox>

    <mx:DataGrid id="templatePropertiesDG" dataProvider="{model.selectedCategory.tai}" width="100%" height="100%" editable="{model.userInEditMode}">
      <mx:columns>
        <mx:DataGridColumn id="Name" dataField="Name" headerText="Name" width="25" editable="false"/>
        <mx:DataGridColumn id="Value" dataField="Value" headerText="Value" width="25" editable="{model.userInEditMode}"/>
        <mx:DataGridColumn id="Country" dataField="Code" headerText="Country" width="10" editable="false"/>
        <mx:DataGridColumn id="Info" dataField="Info" headerText="Information" width="40" editable="false"/>
      </mx:columns>
    </mx:DataGrid>

【问题讨论】:

    标签: apache-flex datagrid duplicate-removal


    【解决方案1】:

    这个问题的答案取决于几件事。如果列表不是太大,您可以简单地遍历 dataProvider 并将每个项目与您正在添加的项目进行比较,如果它们相同,则显示消息或其他内容。

    for (var i:int = 0; i < list.length; i++)
    {
        if (list[i].name == nt.name && list[i].description == nt.description)
        {
             Alert.show("This item already exists", "Error" /*etc*/);
             return;
        }
    }
    

    问题显然是,对于一长串复杂对象,这可能会变得非常缓慢。

    (附带说明,ArrayCollections 的 .contains() 函数通过引用而不是按值工作 - 后者会更昂贵 - 这就是为什么您必须自己编写搜索代码。如果所有属性都必须相同,您可以使用 ObjectUtil.compare 函数)

    数据从何而来?任何类型的 SQL 数据库都旨在快速有效地获取数据,并对可以添加的信息类型有限制(包括防止重复的索引)。尝试从 Flex 客户端运行这种测试可能不是最好的做法,除非您正在处理相对较小的列表。

    如果可能,服务器端检查可能是最好的方法

    【讨论】:

    • 感谢 Michael 的建议,我在已完成的工作中添加了一个帖子。
    【解决方案2】:

    我回答了我自己的问题:这是用于可能遇到此问题的其他人的。

            public function addRow():void {
            var st:AttributeVO = AttributeVO(attCombo.selectedItem);
            var country:String = countriesAvailable.selectedLabel;
    
            if(selectAtt == null) {
                Alert.show("select an attribute.");
                return;
            }
    
            if(!isDuplicate(selectAtt, country)){
                var newAtt:AttributeVO = selectAtt.clone() as AttributeVO;
                newAtt.country = country;
                var list:ArrayCollection = model.category.tAttributes;
                newAtt.attributeValue = "";
                list.addItem(newAtt);
                templatePropertiesDG.invalidateList();
            }
            else{
                Alert.show("Country exists.");
            }
        }
    
        public function isDuplicate(selectAtt:TempAttributeVO, country:String ):Boolean {
            var result:Boolean = false;
            var attributes:ArrayCollection = model.category.tAttributes;
            for(var i:int = 0; i < attributes.length; i++) {
                if(attributes[i].attributeId == selectAtt.attributeId && attributes[i].country == country){
                    result = true;
                    break;
                }
            }
    
            return result;
        }
    

    【讨论】:

      猜你喜欢
      • 2015-02-03
      • 2012-03-11
      • 2012-11-03
      • 1970-01-01
      • 2020-07-24
      • 2020-05-06
      • 2023-01-03
      • 2015-09-11
      • 2011-07-23
      相关资源
      最近更新 更多