计算数据网格中的列

Posted

技术标签:

【中文标题】计算数据网格中的列【英文标题】:calc columns in datagrid 【发布时间】:2013-02-11 06:30:58 【问题描述】:

我真的需要你的帮助。人员遇到以下问题: 我有一个数据网格,其中有一列名为:第 2 列,它有一个固定的数字,其余的是可编辑的列 = true,以便我的客户可以输入数据。 我希望每一个,因为我可以手动插入来调用我进行比较的函数。那行我正在更改值 ¿¿ 插入该行不能大于列的值:第 2 列

这是针对每个采样线完成的

 Line 1: Fixed number ==> 85
 I can get editable columns in column 3: value = 20
 I can get editable columns in column 4: value = 50
 I can get editable columns in column 5: value = 100

当该值大于列 Column 2 时发出警报。

我正在使用 Flex 3

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:ArrayCollection id="arrColl">
    <mx:source>
        <mx:Array>
            <mx:Object label="Student A" score="85" />
            <mx:Object label="Student B" score="48" />
            <mx:Object label="Student C" score="71" />
            <mx:Object label="Student D" score="88" />
            <mx:Object label="Student E" score="24" />
            <mx:Object label="Student F" score="64" />
            <mx:Object label="Student G" score="76" />
            <mx:Object label="Student H" score="76" />
            <mx:Object label="Student I" score="93" />
            <mx:Object label="Student J" score="88" />
            <mx:Object label="Student K" score="48" />
            <mx:Object label="Student L" score="76" />
        </mx:Array>
    </mx:source>
</mx:ArrayCollection>
<mx:DataGrid x="396" y="309" dataProvider="arrColl" editable="true">
    <mx:columns>
        <mx:DataGridColumn headerText="Column 1"  dataField="label"  />
        <mx:DataGridColumn headerText="Column 2"  dataField="score"  />
        <mx:DataGridColumn headerText="Column 3"  editable="true"  dataField="col1"/>
        <mx:DataGridColumn headerText="Column 4"  editable="true"  dataField="col2"/>
        <mx:DataGridColumn headerText="Column 5"  editable="true"  dataField="col3"/>
        <mx:DataGridColumn headerText="Column 6"  editable="true"  dataField="col4"/>
        <mx:DataGridColumn headerText="Column 7"  editable="true"  dataField="col5" />
    </mx:columns>
</mx:DataGrid>

【问题讨论】:

【参考方案1】:

你需要这样做:

    为 score 列设置 editable = "false" 以防止用户更改固定值 为 col1 到 col5 列设置限制,以便仅输入数字 添加一个检查函数来做camparison

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.controls.TextInput;
        import mx.events.DataGridEvent;
        import mx.events.DataGridEventReason;

        protected function checkInput(event:DataGridEvent):void
        
            if (event.reason == DataGridEventReason.NEW_ROW || event.reason == DataGridEventReason.NEW_COLUMN)
            
                var editor:TextInput = (event.currentTarget as DataGrid).itemEditorInstance as TextInput;
                var text:String = editor.text;

                if( event.dataField == "col1" || event.dataField == "col2" || event.dataField == "col3" || event.dataField == "col4" || event.dataField == "col5")
                
                    if(int(text) > int(event.itemRenderer.data.score)) 
                                               
                        editor.text = "";

                        event.preventDefault();

                        Alert.show(text + " is too big!");

                        return;
                    
                
            
        

    ]]>
</mx:Script>
<mx:ArrayCollection id="arrColl">
    <mx:source>
        <mx:Array>
            <mx:Object label="Student A" score="85"/>
            <mx:Object label="Student B" score="48"/>
            <mx:Object label="Student C" score="71"/>
            <mx:Object label="Student D" score="88"/>
            <mx:Object label="Student E" score="24"/>
            <mx:Object label="Student F" score="64"/>
            <mx:Object label="Student G" score="76"/>
            <mx:Object label="Student H" score="76"/>
            <mx:Object label="Student I" score="93"/>
            <mx:Object label="Student J" score="88"/>
            <mx:Object label="Student K" score="48"/>
            <mx:Object label="Student L" score="76"/>
        </mx:Array>
    </mx:source>
</mx:ArrayCollection>
<mx:DataGrid x="396" y="309" dataProvider="arrColl" editable="true" itemEditEnd="checkInput(event)">
    <mx:columns>
        <mx:DataGridColumn headerText="Column 1"  dataField="label" editable="false"/>
        <mx:DataGridColumn headerText="Column 2"  dataField="score" editable="false"/>

        <mx:DataGridColumn headerText="Column 3"  editable="true" dataField="col1">
            <mx:itemEditor>
                <mx:Component>
                    <mx:TextInput restrict="0-9"/>
                </mx:Component>
            </mx:itemEditor> 
        </mx:DataGridColumn>

        <mx:DataGridColumn headerText="Column 4"  editable="true" dataField="col2">
            <mx:itemEditor>
                <mx:Component>
                    <mx:TextInput restrict="0-9"/>
                </mx:Component>
            </mx:itemEditor> 
        </mx:DataGridColumn>

        <mx:DataGridColumn headerText="Column 5"  editable="true" dataField="col3">
            <mx:itemEditor>
                <mx:Component>
                    <mx:TextInput restrict="0-9"/>
                </mx:Component>
            </mx:itemEditor> 
        </mx:DataGridColumn>

        <mx:DataGridColumn headerText="Column 6"  editable="true" dataField="col4">
            <mx:itemEditor>
                <mx:Component>
                    <mx:TextInput restrict="0-9"/>
                </mx:Component>
            </mx:itemEditor> 
        </mx:DataGridColumn>

        <mx:DataGridColumn headerText="Column 7"  editable="true" dataField="col5">
            <mx:itemEditor>
                <mx:Component>
                    <mx:TextInput restrict="0-9"/>
                </mx:Component>
            </mx:itemEditor> 
        </mx:DataGridColumn>

    </mx:columns>
</mx:DataGrid>
</mx:Application>

【讨论】:

哇!!很不错!!非常非常感谢您的帮助。正是我需要的帮助。我很感激。如果有人需要未来,我做了一点修改,让脚本。但它给了我一个完美的基础来改进它。我要感谢您的帮助!【参考方案2】:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
    import mx.controls.Alert;
    import mx.controls.TextInput;
    import mx.events.DataGridEvent;
    import mx.events.DataGridEventReason;

    [Bindable]
    var Acumula:int = 0;

    protected function checkInput(event:DataGridEvent):void
    


        if (event.reason == DataGridEventReason.NEW_ROW || event.reason == DataGridEventReason.NEW_COLUMN)
        
            var editor:TextInput = (event.currentTarget as DataGrid).itemEditorInstance as TextInput;
            var text:String = editor.text;
            var myEditor:TextInput =  TextInput(event.currentTarget.itemEditorInstance);


            Acumula += int(myEditor.text);
            if(int(Acumula) > int(event.itemRenderer.data.score)) 
                                           
                    Acumula = 0;

                    event.preventDefault();

                    Alert.show(text + "ULTRAPASSOU!");

                    return;
                

        
    

]]>
</mx:Script>
<mx:ArrayCollection id="arrColl">
<mx:source>
    <mx:Array>
        <mx:Object label="Student A" score="85"/>
        <mx:Object label="Student B" score="48"/>
        <mx:Object label="Student C" score="71"/>
        <mx:Object label="Student D" score="88"/>
        <mx:Object label="Student E" score="24"/>
        <mx:Object label="Student F" score="64"/>
        <mx:Object label="Student G" score="76"/>
        <mx:Object label="Student H" score="76"/>
        <mx:Object label="Student I" score="93"/>
        <mx:Object label="Student J" score="88"/>
        <mx:Object label="Student K" score="48"/>
        <mx:Object label="Student L" score="76"/>
    </mx:Array>
</mx:source>
</mx:ArrayCollection>
<mx:DataGrid x="10" y="28" dataProvider="arrColl" editable="true" itemEditEnd="checkInput(event)">
<mx:columns>
    <mx:DataGridColumn headerText="Column 1"  dataField="label" editable="false"/>
    <mx:DataGridColumn headerText="Column 2"  dataField="score" editable="false"/>

    <mx:DataGridColumn headerText="Column 3"  editable="true" dataField="col1">
        <mx:itemEditor>
            <mx:Component>
                <mx:TextInput restrict="0-9"/>
            </mx:Component>
        </mx:itemEditor> 
    </mx:DataGridColumn>

    <mx:DataGridColumn headerText="Column 4"  editable="true" dataField="col2">
        <mx:itemEditor>
            <mx:Component>
                <mx:TextInput restrict="0-9"/>
            </mx:Component>
        </mx:itemEditor> 
    </mx:DataGridColumn>

    <mx:DataGridColumn headerText="Column 5"  editable="true" dataField="col3">
        <mx:itemEditor>
            <mx:Component>
                <mx:TextInput restrict="0-9"/>
            </mx:Component>
        </mx:itemEditor> 
    </mx:DataGridColumn>

    <mx:DataGridColumn headerText="Column 6"  editable="true" dataField="col4">
        <mx:itemEditor>
            <mx:Component>
                <mx:TextInput restrict="0-9"/>
            </mx:Component>
        </mx:itemEditor> 
    </mx:DataGridColumn>

    <mx:DataGridColumn headerText="Column 7"  editable="true" dataField="col5">
        <mx:itemEditor>
            <mx:Component>
                <mx:TextInput restrict="0-9"/>
            </mx:Component>
        </mx:itemEditor> 
    </mx:DataGridColumn>

</mx:columns>
</mx:DataGrid>
<mx:Label x="164" y="211" text="Acumula"/>
</mx:Application>

【讨论】:

以上是关于计算数据网格中的列的主要内容,如果未能解决你的问题,请参考以下文章

在网格视图和数据上添加额外的列

是否有某种方法可以计算角度网格中的列数

如何有条件地隐藏或显示剑道网格​​中的列

如何设置数据网格列以填充数据网格集以填充和布局自动调整大小的网格?

更新 datagridview 时导出日志文件

如何在重新排序数据网格的列后获取新的列顺序?