Question

I have a datagrid which populates an array of information. The datagrid is used by the user, so they have the ability to add a record or delete a record on the datagrid. Example: if a user adds the following information: "Name: Apples / Description: Fruit" If this information exist already on the datagrid, how can I prevent them from adding it again? Maybe with a prompt that says "This item is already listed, Please try again". Any thoughts from anyone on how I can make the code work to my advantage?

Functionality:

        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>
Was it helpful?

Solution 2

I answered my own question: Here is what was used for someone else that may run into this issue.

        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;
    }

OTHER TIPS

The answer to this question depends on a couple of things. If the list isn't too big, you could simply loop through the dataProvider and compare each item to the item you're adding, and if they're the same, display the message or whatever.

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;
    }
}

The problem obviously is that with a long list of complicated objects this can become tremendously slow.

(on a side note, the .contains() function of ArrayCollections works by reference, not by value - the latter would be much more expensive - which is why you would have to code the search yourself. If all properties have to be the same you could use the ObjectUtil.compare function)

Where is the data coming from? Any kind of SQL database is designed to quickly and efficiently fetch data as well as have limits on what kind of information can be added (including indexes that prevent duplicates). Trying to run this kind of test from the Flex client is probably not the best course of action, unless you're dealing with relatively small lists.

If it's possible, a server-side check is probably the best way to go

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top