Flex Datagrid with conditional links - datagrid

I need your help on following.
I have one datagrid which use an arraylist as a data provider. Now my requirment is in that arraylist i have one statusId as one variable or say property which i display as one of the column in datagrid. now i have another column where i have to display three links like edit, delete and view which will be based on the statusid. can you give me some idea or example

I'm no Flex expert, but usually when you want to customise the appearance of a column in a DataGrid you use an ItemRenderer, as indicated by your tag. Within the item renderer, you could use Label components and set a selection of attributes that make them look like links, and then enable/disable them depending on your condition.
Here's some example code off the top of my head, with the following caveats:
I'm using an MX DataGrid instead of the Spark DataGrid.
I'm using an inline item renderer for convenience, but it is better practice to externalise your item renderers into separate MXML files.
<mx:DataGrid id="dataGrid" dataProvider="{dataProvider}" ...>
<mx:columns>
<mx:DataGridColumn id="status_id_column" dataField="statusId" headerText="Status" />
<mx:DataGridColumn id="action_column">
<mx:itemRenderer>
<fx:Component>
<mx:Label text="View" paddingLeft="10" useHandCursor="true" buttonMode="true" mouseChildren="false" enabled="Your condition goes here" />
<mx:Label text="Edit" paddingLeft="10" useHandCursor="true" buttonMode="true" mouseChildren="false" enabled="Your condition goes here" />
<mx:Label text="Delete" paddingLeft="10" useHandCursor="true" buttonMode="true" mouseChildren="false" enabled="Your condition goes here" />
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>

thanks avik for your help it is partially correct as for me there are lot of condition i can not put that in enabled attribute. anyway i got the solution from my side :)
here is the syntax.
<mx:HBox width="100%">
<mx:DataGrid id="reportDataGrid" dataProvider="{reportDataGridArrayCollection}"
variableRowHeight="true" editable="false" rowHeight="75"
width="100%" height="400" allowDragSelection="false"
draggableColumns="false" textAlign="center">
<mx:columns>
<mx:DataGridColumn sortable="false" dataField="reportId" resizable="false" headerText="" width="0.06" editable="false" textAlign="center"/>
<mx:DataGridColumn resizable="false" headerStyleName="centered" textAlign="left" dataField="reportStatusId" headerWordWrap="true" headerText="Status" width="0.21">
</mx:DataGridColumn>
<mx:DataGridColumn resizable="false" headerStyleName="centered" textAlign="left" dataField="lockedByUser" headerWordWrap="true" headerText="Locked (Worked) By" width="0.10"/>
<mx:DataGridColumn resizable="false" headerStyleName="centered" textAlign="left" dataField="" headerWordWrap="true" headerText="Acion" width="0.20">
<mx:itemRenderer>
<fx:Component>
<mx:HBox textAlign="left" width="100%" creationComplete="init1()" >
<fx:Script>
<![CDATA[
public function init1():void {
if(data.reportStatusId==0) {
viewLnk.visible = true;
viewLnk.includeInLayout = true;
// this is my condition which you can ignore.... if((data.entityId==1 || data.entityId==2 || data.entityId==3 || data.entityId==4) ){
editLnk.visible = true;
editLnk.includeInLayout = true;
}
}
if(data.reportStatusId==1
) {
editLnk.visible = true;
editLnk.includeInLayout = true;
}
if(data.reportStatusId==2) {
reviewLnk.visible = true;
reviewLnk.includeInLayout = true;
}
if(data.reportStatusId==3) {
saveXMLLnk.visible = true;
saveXMLLnk.includeInLayout = true;
}
}
]]>
</fx:Script>
<mx:LinkButton id="editLnk" visible="false" includeInLayout="false" label="Edit" click="outerDocument.editReport(data.reportId)"/>
<mx:LinkButton id="viewLnk" visible="false" includeInLayout="false" label="View" click="outerDocument.viewReport(data.reportId)"/>
<mx:LinkButton id="reviewLnk" visible="false" includeInLayout="false" label="Review" click="outerDocument.reviewReport(data.reportId)"/>
<mx:LinkButton id="saveXMLLnk" visible="false" includeInLayout="false" label="Save XML" click="outerDocument.saveXMLReport(data.reportId)"/>
</mx:HBox>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:HBox>

Related

Get all selected items - <mx:CheckBox

Here is my dataGrid:
How can I get all the selected values of check
<mx:DataGrid id="dg"
dataProvider="{listOfItems}" verticalAlign="middle" rowHeight="20" rowCount="30"
selectable="true" verticalScrollPolicy="on" >
<mx:columns >
<mx:DataGridColumn id="col1"
dataField="value"
headerText="Item Name">
<mx:itemRenderer>
<mx:Component>
<mx:CheckBox label="{data.vlaue}" paddingLeft="5" />
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
I have a button below that:
<mx:Button x="25" label="Get Selected Items" width="100" click="getSelItems()" cornerRadius="7" fontSize="12" id="itmSel" />
I could find some ways to get the individually selected row by setting change="" method, but how to get all the selected items.
One simple solution I found so far is, to iterate my dataprovider checking, if the item is selected or not.
And here you go!
var tmpList:ArrayCollection = ArrayCollection(dg.dataProvider);
var obj:Object;
for (var i:int=0; i < tmpList.length; i++)
{
if (tmpList[i].Selected == true)
{
//Added to my array collection.
}
}

Make item editor editable of the advancedatagrid in flex

i have a advance datagrid in which i have 2 columns and every row of the column is a item editor
now i want to edit the row cell on double click i tried various things to make it editable
some of properties are written in this code.
i make editable property true of colmns Grid and also i tried the rendrerIsEditor to set it true...
<mx:AdvancedDataGrid id="varGrid" width="100%" top="7" bottom="5" left="7" right="7" rowCount="15"
sortableColumns="true" editable="true">
<mx:columns>
<mx:AdvancedDataGridColumn headerText="Name" editable="true" dataField="name" sortable="true" editorDataField="text" rendererIsEditor="true">
<mx:itemEditor>
<fx:Component>
<s:GridItemEditor >
<s:TextInput id="variableName" text="#{value}" restrict="^\\{\\}" width="100%" height="100%" maxChars="250"
/>
</s:GridItemEditor>
</fx:Component>
</mx:itemEditor>
</mx:AdvancedDataGridColumn>
<mx:AdvancedDataGridColumn headerText="Value" editable="true" dataField="lastValue" sortable="true" rendererIsEditor="true">
<mx:itemEditor>
<fx:Component>
<s:GridItemEditor>
<s:TextInput text="#{value}" restrict="^\\{\\}" width="100%" height="100%" maxChars="250"/>
</s:GridItemEditor>
</fx:Component>
</mx:itemEditor>
</mx:AdvancedDataGridColumn>
</mx:columns>
<s:AsyncListView list="{data.variables}"/>
</mx:AdvancedDataGrid>
please help me is i am doing it right or is there something missing in this.
There are a couple of things wrong with your code:
You want to use a custom itemEditor, so don't set rendererIsEditor="true".
You can't use s:GridItemEditor within the AdvancedDataGrid. It's for the Spark s:DataGrid.
The id attribute is not allowed within <fx:Component>.
Use Spark components as itemEditor is not as easy as it used to be with Halo components. I'd recommend you use the mx:TextInput instead of the s:TextInput. If you need to use the Spark one take a look at MXAdvancedDataGridItemRenderer and Using a Spark item renderer with an MX control.
Below is a code snippet that corrects all those issues and uses the mx:TextInput component:
<mx:AdvancedDataGrid id="varGrid" width="100%" top="7" bottom="5" left="7" right="7" rowCount="15" sortableColumns="true"
editable="true">
<mx:columns>
<mx:AdvancedDataGridColumn headerText="Name" editable="true" dataField="name" sortable="true" editorDataField="text">
<mx:itemEditor>
<fx:Component>
<mx:TextInput restrict="^\\{\\}" width="100%" height="100%" maxChars="250"/>
</fx:Component>
</mx:itemEditor>
</mx:AdvancedDataGridColumn>
<mx:AdvancedDataGridColumn headerText="Value" editable="true" dataField="lastValue" sortable="true">
<mx:itemEditor>
<fx:Component>
<mx:TextInput restrict="^\\{\\}" width="100%" height="100%" maxChars="250"/>
</fx:Component>
</mx:itemEditor>
</mx:AdvancedDataGridColumn>
</mx:columns>
<s:AsyncListView list="{data.variables}"/>
</mx:AdvancedDataGrid>

flex, AdvancedDataGrid, custom itemrenderer

I seem to be missing some key concept(s) when it comes to flex itemrenderers, particularly as it applies to an AdvancedDataGrid. I'm doing what a lot of other people are trying to do: change the bg color of a field based on data from the row. My problem seems to be in accessing data fields? Basically, when this loads, nothing appears. If I remove the parts from the renderer where I try to access field data, it works, but kind of defeats the purpose.
Here's what I've got:
<mx:AdvancedDataGrid width="100%" height="100%"
id="topAccountsGrid"
borderStyle="solid" dropShadowEnabled="true" treeColumn="{list_name}" editable="false" selectionMode="singleRow"
dragEnabled="true" dropEnabled="true" dragMoveEnabled="true" dragDrop="topAccountsGrid_dragDropHandler(event)"
doubleClickEnabled="true" itemDoubleClick="topAccountsGrid_itemDoubleClickHandler(event)"
sort="topAccountsGrid_sortHandler(event)" backgroundColor="#ffffff">
<mx:dataProvider>
<mx:HierarchicalData source="{filteredList}"
childrenField="children" />
</mx:dataProvider>
<mx:columns>
<mx:AdvancedDataGridColumn id="colRank" headerText="Rank" dataField="Rank__c" width="60">
<mx:itemRenderer>
<fx:Component>
<mx:HBox paddingLeft="2">
<s:Label id="tempLabel" text="{data.Rank__c}" />
<fx:Script>
<![CDATA[
override public function set data(value:Object) : void{
super.data = value;
if(data.Health__c == 0){
setStyle("backgroundColor",0xFF5050);
} else if(data.Health__c == 50){
setStyle("backgroundColor",0xFFFF99);
} else if(data.Health__c == 100){
setStyle("backgroundColor",0x66FF66);
}
}
]]>
</fx:Script>
</mx:HBox>
</fx:Component>
</mx:itemRenderer>
</mx:AdvancedDataGridColumn>
<mx:AdvancedDataGridColumn id="list_name" headerText="Name" dataField="Name" />
<mx:AdvancedDataGridColumn id="colPrevRank" headerText="Previous Rank" dataField="Previous_Rank__c" />
<mx:AdvancedDataGridColumn id="colType" headerText="Type" dataField="Type" />
<mx:AdvancedDataGridColumn id="colContacts" headerText="# Contacts" dataField="Contacts__c" />
<mx:AdvancedDataGridColumn id="colDeals" headerText="# Deals" dataField="Deals__c" />
</mx:columns>
</mx:AdvancedDataGrid>
I've had a similar issue in the past when using HierarchicalData. The way I got around it was to use an AdvancedDataGridRendererProvider.
Here's some sample code:
<mx:AdvancedDataGrid width="100%" height="100%"
id="topAccountsGrid"
backgroundColor="#ffffff">
<mx:dataProvider>
<mx:HierarchicalData source="{filteredList}"
childrenField="children" />
</mx:dataProvider>
<mx:columns>
<mx:AdvancedDataGridColumn id="colRank" headerText="Rank" dataField="Rank__c" width="60" />
<mx:AdvancedDataGridColumn id="list_name" headerText="Name" dataField="Name" />
<mx:AdvancedDataGridColumn id="colPrevRank" headerText="Previous Rank" dataField="Previous_Rank__c" />
<mx:AdvancedDataGridColumn id="colType" headerText="Type" dataField="Type" />
<mx:AdvancedDataGridColumn id="colContacts" headerText="# Contacts" dataField="Contacts__c" />
<mx:AdvancedDataGridColumn id="colDeals" headerText="# Deals" dataField="Deals__c" />
</mx:columns>
<mx:rendererProviders>
<mx:AdvancedDataGridRendererProvider column="{colRank}" depth="1" dataField="Rank__c" renderer="AdvancedDataGridRankCRenderer" />
</mx:rendererProviders>
</mx:AdvancedDataGrid>
And the AdvancedDataGridRendererProvider:
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
backgroundColor="{SetBackgroundColor(data)}"
paddingLeft="2" paddingRight="2" paddingTop="2"
horizontalScrollPolicy="off"
verticalScrollPolicy="off"
height="22">
<fx:Script>
<![CDATA[
[Bindable] private var bgColor:uint = 0xD6E5FF;
private function SetBackgroundColor(obj:Object):uint
{
var returnColor:uint = 0xFF5050;
if (obj["Rank__c"] != null)
{
switch (obj["Rank__c"].toString().toUpperCase())
{
case "0":
returnColor = 0xFF5050;
break;
case "50":
returnColor = 0xFFFF99;
break;
case "100":
returnColor = 0x66FF66;
break;
default:
returnColor = 0xFF5050;
break;
}
}
return returnColor;
}
override public function set data(value:Object):void
{
super.data = value;
rankLabel.text = value["Rank__c"].toString();
validateDisplayList();
}
]]>
</fx:Script>
<mx:Label id="rankLabel" />
</mx:HBox>

Flex: Error on Scrolling Vertically in a DataGrid

i have this code
<mx:DataGrid id="tempListDG" itemDoubleClick="doubleClickHandler(event)" width="100%" height="100%" rowHeight="110"
draggableColumns="false" sortableColumns="false" allowMultipleSelection="false">
<mx:columns>
<mx:DataGridColumn id="chkSel" headerText=" " width="15" sortable="false">
<mx:itemRenderer>
<mx:Component>
<mx:HBox horizontalScrollPolicy="off" verticalScrollPolicy="off" paddingLeft="3">
<mx:Script>
<![CDATA[
]]>
</mx:Script>
<mx:CheckBox name="chkSel" selected="false" />
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn id="sum" dataField="#summary" headerText="Summary Description" width="280" >
<mx:itemRenderer>
<mx:Component>
<mx:HBox name="thumbs" creationComplete="setThumbnailImage(event)" verticalAlign="top" verticalScrollPolicy="off">
<mx:Script>
<![CDATA[
import mx.controls.Text;
import com.azaaza.containers.HBox;
import com.azaaza.controls.Image;
import com.hwakin.tavi.model.ModelLocator;
import mx.controls.DataGrid;
private function setThumbnailImage(e:Event):void{
var dg:DataGrid = DataGrid(e.target.parent.parent);
var dCounter:int = TemplateOpenPanel(dg.parent.parent).dCount;
if (dCounter+1 > XMLList(dg.dataProvider).length()){
dg.validateDisplayList();
return;
}
img.load(ModelLocator.getInstance().StringToBitmap(XMLList(dg.dataProvider)[dCounter].#thumbStr));
img.width = 80;
img.height = 110;
txt.htmlText = XMLList(dg.dataProvider)[dCounter].#summary;
txt.maxHeight = 110;
dCounter++;
TemplateOpenPanel(dg.parent.parent).dCount = dCounter;
}
]]>
</mx:Script>
<mx:Image id="img">
</mx:Image>
<mx:Text id="txt">
</mx:Text>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn dataField="#dateCreated" headerText="Date Created" width="100" />
<mx:DataGridColumn dataField="#dateModified" headerText="Date Modified" width="100"/>
<mx:DataGridColumn dataField="#guid" headerText="guid" visible="false"/>
<mx:DataGridColumn dataField="#fileName" headerText="File Name" visible="false"/>
<mx:DataGridColumn dataField="#tempXml" headerText="tempXml" visible="false"/>
</mx:columns>
</mx:DataGrid>
the datagridcolumn id named "sum" creates images and text given by the XML i loaded
but i got error when i use the scroll of the datagrid. and the images get disaligned and all of the data like the dateCreated and dateModified are shuffled or something.
please help me with this one.. thanks
If you are still looking for answer add this into your code.
1)
protected function dgtempListDG_scrollHandler(event:ScrollEvent):void
{
// TODO Auto-generated method stub
tempListDG.invalidateDisplayList();
}
2)
scroll = "dgtempListDG_scrollHandler(event)"
Add this in mx:datagrid.
remember that item renderers are recycled and reused, so you should not use creationCompelte events, (if only 5 item renderers are visible, only 7 are created and then they are reused, but they are created only once so creation complete only fires once)
I like to use dataChange events, they work upon creation and each time the data of the itemRenderer changes.

how to have a link in a datagrid and to pop up a window on clicking the link in flex?

I have a datagrid with different types of columns, like I have checkboxes, combo boxes and text Inputs as the column types.
Now I want one of the column type to a link, with the label "view". All the rows in that column are link with the same label "View" and on clicking it, I want a Pop up window to be opened?
This is my code:
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%">
<mx:Script>
<![CDATA[
[Bindable]
private var defectDetails:ArrayCollection = new ArrayCollection([ ]);
private function addDefect():void{
defectDG.dataProvider.addItem(
{CommentHistory:"View"}
);
defectDG.height += 30;
}
private function defectCommentsPopUp():void{
var helpWindow:defectCommentsLookUp=defectCommentsLookUp(PopUpManager.createPopUp(this, defectCommentsLookUp, true));
}
]]>
</mx:Script>
<mx:DataGrid id="defectDG" dataProvider="{defectDetails}" variableRowHeight="true" width="100%" height="75" >
<mx:columns>
<mx:DataGridColumn headerText="Select" dataField="Select" itemRenderer="mx.controls.CheckBox" width="50" textAlign="center" />
<mx:DataGridColumn headerText="Defect Id" dataField="DefectId" itemRenderer="mx.controls.TextInput" textAlign="center"/>
<mx:DataGridColumn headerText="Status" dataField="Status" itemRenderer="mx.controls.ComboBox" textAlign="center"/>
<mx:DataGridColumn headerText="Severity" dataField="Severity" itemRenderer="mx.controls.ComboBox" textAlign="center" />
<mx:DataGridColumn headerText="Comment History" dataField="CommentHistory" itemRenderer="mx.controls.Text" textAlign="center" headerWordWrap="true" />
</mx:columns>
</mx:DataGrid>
<mx:Button styleName="buttonStyle" label="Add New Defect" click="addDefect()"/>
</mx:VBox>
I didn't know how to bring a link in the datagrid. So used the Text control to display the "View" label. Now If I click this item, "View" in the datagrid, I want the Pop up function, i.e.,defectCommentsPopUp() to be called.
How to do this?
Assign values to commentHistory that would help to identify the row.
<mx:DataGridColumn dataField="commentHistory">
<mx:itemRenderer>
<mx:Component>
<mx:Label text="View" click="outerDocument.onViewClick(data)"/>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
in the script:
private function onViewClick(item:Object):void
{
//item contains the commentHistory value of the clicked row.
showPopUp(item);
}

Resources