PDA

View Full Version : DataGrid + filterFunction + selectedItems


matchoo
12-30-2008, 07:20 PM
Greetings,

Ok, it's been two days of misery for me, a wife that complains I am obsessed with work, and a boss that keeps asking "where is it?"

So, this problem has a bounty. I like Pizza. Do you like Pizza? The first male, female, hermaphrodite, or genderless martian to solve my problem gets one large pizza with whatever toppings they want. Respond with your solution, and if it works, you can tell me your favorite local pizza place, and I'll have it delivered to you with a nice green salad, and some cans of soda to boot.

No, I'm serious.

Here's the information you need to proceed:

1) I have a DataGrid, well, it's actually an AdvancedDataGrid, but for your purposes, it doesn't matter.

2) I have some comboBoxes,that apply filters to the DataGrid using filterFunction. The example below just shows one comboBox, but I have several, and a slider also.

3) Ok, here's the problem: I want to give my users the ability to select multiple rows in the datagrid using selectionMode="multipleRows" - then press a button to only show the rows that are highlighted. I need this functionality to incorporate the other filters that I built - so it has to be in the filterFunction code.

4) I also want to be able to 'reset' the grid so that the formerly highlighted rows re-appear, but the other filters stay intact. Another button perhaps.

The code is too cumbersome to show here, so if you can show an example using the following snippets, that'll be great:

Simplified Grid:



<mx:HTTPService id="videoServiceSource" result="handleData(event)"
resultFormat="e4x" url="{urlString}" showBusyCursor="true" />

<mx:XMLListCollection id="videoData" source="{videoServiceSource.lastResult.*}" />

<mx:HTTPService id="videoPublisherSource" result="handleDataPub(event)" resultFormat="e4x" url="{urlString}">
<mx:request>
<a>publisher</a>
</mx:request>
</mx:HTTPService>

<mx:XMLListCollection id="videoPublisher" source="{videoPublisherSource.lastResult.*}" />

<mx:Script>
<![CDATA[
import mx.events.CollectionEvent;
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.utils.ObjectUtil;
import mx.controls.Alert;
import mx.events.ListEvent;
import mx.utils.ArrayUtil;
import mx.controls.advancedDataGridClasses.*;
import mx.events.AdvancedDataGridEvent;

[Bindable] public var urlString:String = "index.php";
[Bindable] public var publisherData:XMLListCollection;

private function init():void
{
//I do stuff here to chain http service calls - no need to see my custom class for that
var chain:ServiceChain = new ServiceChain();
chain.add(videoPublisherSource, handleDataPubl);
chain.chainFinishedHandler = chainFinished;
chain.run();
}

private function chainFinished():void{
videoServiceSource.send();
}
}

private function handleData(event:ResultEvent):void
{
videoData = new XMLListCollection();
videoData.source = event.result.video;
videoData.filterFunction = filterVids; //SETS FILTER FOR DATAGRID
videoData.refresh();
}

private function handleDataPub(event:ResultEvent):void
{
studioData = new XMLListCollection();
studioData.source = event.result.studio;
}

private function handlePubChange():void
{
//videoData.filterFunction = filterVids; //no need to redeclare
videoData.refresh();
}

private function filterByChecks():void{
//THIS SHOULD JUST REFRESH THE FILTERS... CODE SHOULD GO IN filterVids section
videoData.refresh();
}


private function unfilterByChecks():void{
//THIS SHOULD JUST REFRESH THE FILTERS... CODE SHOULD GO IN filterVids section - BUT WITH ALL ROWS SHOWING AS THEY WERE BEFORE SELECTED ROWS WERE FILTERED
videoData.refresh();
}

private function filterVids(item:Object):Boolean {
if(

(item.publisher == publisher.value || publisher.value == 'All')

&&

(
//HERE IS WHERE I WANT TO LOOK AT SELECTED ROWS AND FILTER
//UNLESS NO ROWS ARE SELECTED - APPLY NO FILTER
//HOW TO ACCESS THAT?
)

)
{
return true;
} else {
return false;
}
}

]]>
</mx:Script>


<mx:Label text="Publisher" width="49" textAlign="right"/>
<mx:ComboBox id="publisher" dataProvider="{videoPublisher}" change="handlePubChange()" width="149"/>
<mx:Button label="show selected rows only" click="filterByChecks()"/>
<mx:Button label="show all rows again" click="unfilterByChecks()"/>
<mx:AdvancedDataGrid id="grid" dataProvider="{videoData}" selectionMode="multipleRows">
<mx:groupedColumns>
<mx:AdvancedDataGridColumn headerText="Video ID"
dataField="video_id" width="0" visible="false" />
<mx:AdvancedDataGridColumn headerText="Title" textAlign="left"
dataField="title" width="110" headerStyleName="centerAligned" />
<mx:AdvancedDataGridColumn headerText="Publisher"
dataField="publisher" width="90" headerStyleName="centerAligned"/>
</mx:groupedColumns>
</mx:AdvancedDataGrid>

matchoo
12-30-2008, 11:14 PM
I think the same rules of nature that apply to tech support lines, also apply to actionscript.org...

Neglect someone long enough... they'll figure it out themselves :)

Here's the working code:

in the filterFunction I put this:

evalSelectedRows(item.campaign_id)

Then I just created this:

protected function evalSelectedRows(cid:int):Boolean{
if(filterSelected == false){
return true;
}
var i:Number;
for (i = 0; i < grid.selectedItems.length; i++){
if(int(grid.selectedItems[i].campaign_id) == cid){
return true;
}
}
return false;
}

and this:

private function filterByChecks():void{
filterSelected = true;
}

Easy sleezy.