4 Replies Latest reply on May 17, 2010 11:37 PM by udara liyanage

    datagrid sorting

    udara liyanage

      when a user click on a datagrid colum header it gets sorted.How can I add a custom sorting instead of built in method.

        • 1. Re: datagrid sorting
          Vikash.kumar29 Level 3

          HI ,

           

          You can define custom sorting function by assing assigning function reference to the dataGrid Column by saying

           

              <mx:DataGridColumn sortCompareFunction="numericSorting" headerText="Total" />

           

          and define the numericSorting like :

           

                  private function numericSorting(inData1:Object,inData2:Object):int
                  {
                      if(inData1.hasOwnProperty(dataField) == false)
                          return -1;
                      if(inData2.hasOwnProperty(dataField)== false)
                          return 1;
                      var num1:Number = inData1[dataField];
                      var num2:Number = inData2[dataField];
                      return ObjectUtil.numericCompare(num1,num2);
                  }

           

          Hope this helps you

           

          Cheers

          -Vikash

          1 person found this helpful
          • 2. Re: datagrid sorting
            udara liyanage Level 1

            but my dataGridColumn are set dynamicaly,so I can not use "sortCompareFunction" way.I want to get the column name when user click column header and send it to server,then server will send another data set

            • 3. Re: datagrid sorting
              Vikash.kumar29 Level 3

              Though you are adding column dynamically then also you can use sortCompare function by saying

               

              var dataGridColumn:DataGridColumn = new DataGridColumn();

              dataGridColumn.sortCompareFunction = numericSort;

               

               

               

              However if you want to access the column name you can create a custom HeaderRenderer and assign this renderer to the Datagrid then on click you can override mouseUpHandler and  find the index of column. See the code of DataGridHeader mouseUpHandler. From here you will get the column index and then using column index you can get dataGrid Column from visibleColumns Array which nameSpace is mx_internal.

               

               

               

              Hope this helps you and let me know if you have any problem

               

              -Vikash

              • 4. Re: datagrid sorting
                udara liyanage Level 1

                thankz.Anyway I used datagrid "headerRelease" event to get column name and got name as event.dataField.