4 Replies Latest reply on May 9, 2007 6:46 AM by michaeljbaird

    Sort DataGrid with dataprovider XMLisCollection

    michaeljbaird
      I can't get my datagrid to sort when I have a XMLListcollection as the dataprovider and I have to use a labelfunction to get the records to display because of the namespace in the XML (Replicating a .NET web service). Can someone help, Please?

      I have the following code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="initApp()" >

      <mx:Script>
      <![CDATA[
      import mx.controls.dataGridClasses.DataGridColumn;
      import mx.controls.DataGrid;
      import mx.events.DataGridEvent;
      import mx.collections.*;
      import mx.controls.Alert;
      import mx.formatters.DateFormatter;

      //the dataProvider for the DG
      [Bindable]
      private var _xlc:XMLListCollection;
      [Bindable]
      private var sortA:Sort;
      // The sort fields used to determine the sort.
      private var sortBySeverity:SortField;
      private var sortByRequestor:SortField;
      //**************************************************************************************** *******************************************************
      public function initApp():void
      {
      namespace myNameS = "myNamespace.Test";
      use namespace myNameS;
      _xlc = new XMLListCollection(GetRequestList.*..SafRequestListVO);

      //testing element values
      trace(GetRequestList.*..SafRequestListVO.SeverityLevel);

      sortA = new Sort();
      sortBySeverity = new SortField("SeverityLevel", true, true);
      sortByRequestor = new SortField("Requestor", true);

      sortA.fields=[sortBySeverity, sortByRequestor];
      _xlc.sort=sortA;
      _xlc.refresh();

      dg.dataProvider = _xlc;
      dg.rowCount=_xlc.length +1;
      }
      //**************************************************************************************** *******************************************************
      /**labelFunction to return data to DG by reference of dataField*/
      private function LF(item:Object, column:DataGridColumn):Object
      {
      namespace myNameS = "myNamespace.Test";
      use namespace myNameS;

      var colDataField:Object = column.dataField;
      switch (colDataField) //logic to determine which node to get the data from
      {
      case "SAFNumber":
      return item.SAFNumber;
      break;
      case "RequestDate":
      var date:String = new String(item.RequestDate).toString();
      var df:DateFormatter = new DateFormatter();
      df.formatString = "YYYY-MM-DD";
      return df.format(date);
      break;
      case "SeverityLevel":
      return item.SeverityLevel;
      break;
      case "Requestor":
      return item.Requestor;
      break;
      case "IssuedToDepartment":
      return item.IssuedToDepartment;
      break;
      default:
      return item.dataField;
      break;
      }
      }
      ]]></mx:Script>
      <mx:Canvas id="datagridParent" borderStyle="solid" height="450" width="100%" >
      <mx:DataGrid id="dg" width="100%" height="100%" rowCount="5" labelFunction="LF">
      <mx:columns>
      <mx:DataGridColumn dataField="SAFNumber" headerText="SAFNumber"/>
      <mx:DataGridColumn dataField="Requestor" headerText="Requestor"/>
      <mx:DataGridColumn dataField="RequestDate" headerText="RequestDate"/>
      <mx:DataGridColumn dataField="IssuedToDepartment" headerText="IssuedToDepartment"/>
      <mx:DataGridColumn dataField="SeverityLevel" headerText="SeverityLevel"/>
      </mx:columns>
      </mx:DataGrid>
      </mx:Canvas>
      <mx:XML id="GetRequestList" format="e4x">
      <GetRequestListResponse xmlns="myNamespace.Test">
      <GetRequestListResult>
      <SafRequestListVO>
      <RequestGuid>3F2504E0-4F89-11D3-9A0C-0305E82C3301</RequestGuid>
      <SAFNumber>1</SAFNumber>
      <RequestDate>03/24/2007</RequestDate>
      <Requestor>Michael</Requestor>
      <IssuedToDepartment>1</IssuedToDepartment>
      <IssuedToArea>3</IssuedToArea>
      <IssuedByDepartment>string</IssuedByDepartment>
      <Problem>Problem Details........Problem Details........</Problem>
      <SeverityLevel>3</SeverityLevel>
      <CurrentState>string</CurrentState>
      </SafRequestListVO>
      <SafRequestListVO>
      <RequestGuid>3F2504E0-4F89-11D3-9A0C-0305E82C3302</RequestGuid>
      <SAFNumber>2</SAFNumber>
      <RequestDate>02/21/2006</RequestDate>
      <Requestor>Bill</Requestor>
      <IssuedToDepartment>1</IssuedToDepartment>
      <IssuedToArea>2</IssuedToArea>
      <IssuedByDepartment>string</IssuedByDepartment>
      <Problem>Problem Details........Problem Details........Problem Details........Problem Details........Problem Details........</Problem>
      <SeverityLevel>1</SeverityLevel>
      <CurrentState>string</CurrentState>
      </SafRequestListVO>
      <SafRequestListVO>
      <RequestGuid>3F2504E0-4F89-11D3-9A0C-0305E82C3303</RequestGuid>
      <SAFNumber>3</SAFNumber>
      <RequestDate>01/06/2007</RequestDate>
      <Requestor>Jonathan</Requestor>
      <IssuedToDepartment>8</IssuedToDepartment>
      <IssuedToArea>2</IssuedToArea>
      <IssuedByDepartment>string</IssuedByDepartment>
      <Problem>string</Problem>
      <SeverityLevel>1</SeverityLevel>
      <CurrentState>string</CurrentState>
      </SafRequestListVO>
      <SafRequestListVO>
      <RequestGuid>3F2504E0-4F89-11D3-9A0C-0305E82C3304</RequestGuid>
      <SAFNumber>4</SAFNumber>
      <RequestDate>03/27/2007</RequestDate>
      <Requestor>Bill</Requestor>
      <IssuedToDepartment>7</IssuedToDepartment>
      <IssuedToArea>2</IssuedToArea>
      <IssuedByDepartment>string</IssuedByDepartment>
      <Problem>Problem Details........Problem Details........Problem Details........Problem Details........Problem Details........</Problem>
      <SeverityLevel>3</SeverityLevel>
      <CurrentState>string</CurrentState>
      </SafRequestListVO>
      <SafRequestListVO>
      <RequestGuid>3F2504E0-4F89-11D3-9A0C-0305E82C3305</RequestGuid>
      <SAFNumber>5</SAFNumber>
      <RequestDate>12/21/2006</RequestDate>
      <Requestor>Mike</Requestor>
      <IssuedToDepartment>4</IssuedToDepartment>
      <IssuedToArea>2</IssuedToArea>
      <IssuedByDepartment>string</IssuedByDepartment>
      <Problem>string</Problem>
      <SeverityLevel>2</SeverityLevel>
      <CurrentState>string</CurrentState>
      </SafRequestListVO>
      <SafRequestListVO>
      <RequestGuid>3F2504E0-4F89-11D3-9A0C-0305E82C3306</RequestGuid>
      <SAFNumber>6</SAFNumber>
      <RequestDate>04/02/2007</RequestDate>
      <Requestor>Nick</Requestor>
      <IssuedToDepartment>2</IssuedToDepartment>
      <IssuedToArea>2</IssuedToArea>
      <IssuedByDepartment>string</IssuedByDepartment>
      <Problem>Problem Details........Problem Details........Problem Details........Problem Details........Problem Details........</Problem>
      <SeverityLevel>3</SeverityLevel>
      <CurrentState>string</CurrentState>
      </SafRequestListVO>
      <SafRequestListVO>
      <RequestGuid>3F2504E0-4F89-11D3-9A0C-0305E82C3307</RequestGuid>
      <SAFNumber>7</SAFNumber>
      <RequestDate>03/27/2007</RequestDate>
      <Requestor>Sandeep</Requestor>
      <IssuedToDepartment>4</IssuedToDepartment>
      <IssuedToArea>2</IssuedToArea>
      <IssuedByDepartment>string</IssuedByDepartment>
      <Problem>Problem Details........Problem Details........Problem Details........Problem Details........</Problem>
      <SeverityLevel>2</SeverityLevel>
      <CurrentState>string</CurrentState>
      </SafRequestListVO>
      </GetRequestListResult>
      </GetRequestListResponse>
      </mx:XML>
      </mx:Application>