idonaldson
idonaldson

Reputation: 475

Filtering datagrid based on data in rows, Flex

I've search all over today trying to find how to do this, and not being familiar with actionscript is starting to catch up to me. What I would like to accomplish is: I have a list of messages in a Datagrid coming from a dataprovider in another class, which in turn gets them from our Oracle DB. I need to all the user to set a visible state on the message, and then filter that out of the datagrid with the click of a button. I have the check box for hide, and it sets that value into the database. I can't figure out how to get the filterFunction to work with an array collection when the filter parameter is within the row data.

Here is the code

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()" />

I found a tutorial on doing this with incoming text here http://franto.com/filter-results-in-datagrid-flex-tutorial/, but cannot figure out the above mentioned problem, this really can't be that difficult, can it?

Thanks,

Ian

Upvotes: 1

Views: 6566

Answers (1)

shaunhusain
shaunhusain

Reputation: 19748

item is an element of the dataprovider the method is called for each element in the dataprovider and flags the item for inclusion in length and iteration.

       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()" />

Upvotes: 2

Related Questions