根据行中的数据过滤数据网格,Flex

时间:2012-03-23 22:46:45

标签: actionscript-3 flex

今天我一直在搜索试图找到如何做到这一点,并且不熟悉动作脚本开始赶上我。我想要完成的是:我在Datagrid中有一个消息列表来自另一个类中的数据提供者,而后者又从我们的Oracle数据库中获取它们。我需要所有用户在消息上设置可见状态,然后通过单击按钮将其过滤出数据网格。我有隐藏的复选框,它将该值设置到数据库中。当filter参数在行数据中时,我无法弄清楚如何让filterFunction与数组集合一起使用。

这是代码

public function filterResults():void {

            modelLocator.notification.messageList.filterFunction = filterRows;

            modelLocator.notification.messageList.refresh(); 


        }

        public function filterRows(item:Object):Boolean {
            //return true if row should stay visible
            //return false if it should go away

             var i:int;

            if(showAll == false) {//checks whether this is coming from the hide or show all button
            //Somehow need to interrogate the row data to check if messageVisible is set to true or false

             /* if (showAll == false) {
                return false;
            }else {
                return true;
            }
            return false; */ 

        }
        public var showAll:Boolean;

        public function showAllMessages():void{

            showAll = true;
            filterResults();
        }
        public function hideMessages():void{
            showAll = false;
            filterResults();
        }


    ]]>
</mx:Script>

<mx:VBox>
    <component:EditMessage id="editMessage"  width="930" height="445"/>
    <mx:Panel id="messageListPanel" title="Message History" layout="vertical" width="930" height="196" horizontalAlign="left">

        <mx:DataGrid id="messageDataGrid" dataProvider="{modelLocator.notification.messageList}" 
                     width="910" height="139" enabled="true" mouseEnabled="true" editable="false"
                     rowCount="5" itemClick="{selectMessage()}">

            <mx:columns>
                <mx:DataGridColumn headerText="Date Created" labelFunction="formatCreateDate" width="60"/>
                <mx:DataGridColumn headerText="From" dataField="senderEmail" width="100"/>
                <mx:DataGridColumn headerText="Subject" dataField="subject" width="100"/>
                <mx:DataGridColumn headerText="Start Date" labelFunction="formatStartDate" width="60"/>
                <mx:DataGridColumn headerText="End Date" labelFunction="formatEndDate" width="60" />
                <mx:DataGridColumn headerText="Date Sent" labelFunction="formatSendDate" width="60" />
                <mx:DataGridColumn headerText="Sender Netid" dataField="senderNetId" width="50" />
                <mx:DataGridColumn headerText="Sender Name" dataField="senderName" width="80" />
                <mx:DataGridColumn headerText="Message" dataField="message" width="100" />
                <mx:DataGridColumn headerText="Message Id" dataField="id" width="10" />
            </mx:columns>
        </mx:DataGrid>              
    </mx:Panel>                 
</mx:VBox>
<mx:Button id="showMessagesBtn" x="786" y="452" label="Show All Messages" click="showAllMessages()"/>
<mx:Button id="hideMessagesBtn" x="665" y="452" label="Hide Messages" click="hideMessages()" />

我在这里找到了一个关于传入文本http://franto.com/filter-results-in-datagrid-flex-tutorial/的教程,但无法弄清楚上面提到的问题,这真的不是那么难,可以吗?

谢谢,

伊恩

1 个答案:

答案 0 :(得分:2)

item是dataprovider的一个元素,为dataprovider中的每个元素调用该方法,并标记该项以包含在长度和迭代中。

       public function filterResults():void {

            modelLocator.notification.messageList.filterFunction = filterRows;

            modelLocator.notification.messageList.refresh(); 


        }

        public function filterRows(item:Object):Boolean {
            if(showAll)
                return true;
            if(item.messageVisible=="true")
                return true;
            return false;
        }
        public var showAll:Boolean;

        public function showAllMessages():void{

            showAll = true;
            filterResults();
        }
        public function hideMessages():void{
            showAll = false;
            filterResults();
        }


    ]]>
</mx:Script>

<mx:VBox>
    <component:EditMessage id="editMessage"  width="930" height="445"/>
    <mx:Panel id="messageListPanel" title="Message History" layout="vertical" width="930" height="196" horizontalAlign="left">

        <mx:DataGrid id="messageDataGrid" dataProvider="{modelLocator.notification.messageList}" 
                     width="910" height="139" enabled="true" mouseEnabled="true" editable="false"
                     rowCount="5" itemClick="{selectMessage()}">

            <mx:columns>
                <mx:DataGridColumn headerText="Date Created" labelFunction="formatCreateDate" width="60"/>
                <mx:DataGridColumn headerText="From" dataField="senderEmail" width="100"/>
                <mx:DataGridColumn headerText="Subject" dataField="subject" width="100"/>
                <mx:DataGridColumn headerText="Start Date" labelFunction="formatStartDate" width="60"/>
                <mx:DataGridColumn headerText="End Date" labelFunction="formatEndDate" width="60" />
                <mx:DataGridColumn headerText="Date Sent" labelFunction="formatSendDate" width="60" />
                <mx:DataGridColumn headerText="Sender Netid" dataField="senderNetId" width="50" />
                <mx:DataGridColumn headerText="Sender Name" dataField="senderName" width="80" />
                <mx:DataGridColumn headerText="Message" dataField="message" width="100" />
                <mx:DataGridColumn headerText="Message Id" dataField="id" width="10" />
            </mx:columns>
        </mx:DataGrid>              
    </mx:Panel>                 
</mx:VBox>
<mx:Button id="showMessagesBtn" x="786" y="452" label="Show All Messages" click="showAllMessages()"/>
<mx:Button id="hideMessagesBtn" x="665" y="452" label="Hide Messages" click="hideMessages()" />