0

情報の配列を設定するデータグリッドがあります。データグリッドはユーザーが使用するため、ユーザーはデータグリッドでレコードを追加または削除できます。例: ユーザーが次の情報を追加した場合: 「名前: りんご / 説明: 果物」 この情報がデータグリッドに既に存在する場合、どうすれば再度追加できないようにできますか? 「このアイテムは既にリストされています。もう一度お試しください」というプロンプトが表示される場合があります。コードを有利に機能させる方法について、誰かからの考えはありますか?

機能性:

        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>
4

2 に答える 2

0

この質問に対する答えは、いくつかのことに依存します。リストが大きすぎない場合は、単純に 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 クライアントから実行しようとするのは、比較的小さなリストを扱っている場合を除き、おそらく最善の方法ではありません。

可能であれば、サーバー側のチェックがおそらく最善の方法です

于 2013-11-09T01:02:18.330 に答える
0

私は自分の質問に答えました: これは、この問題に遭遇する可能性のある他の誰かに使用されたものです.

        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;
    }
于 2013-11-12T15:09:24.310 に答える