5 Replies Latest reply on Aug 25, 2009 3:57 AM by Niranjan_swain

    How to prevent grid data to be sorted after applying addItemAt() function on dataprovider?

    82.bansal

      Hi,

       

      I am facing a problem that when I add a new item in datasource of grid using addItemAt() it will also sort the grid data in wiered manner. I need to only add the item to the datasource and not to sort it....... Any help is much appreciated.

       

      Thanks............

        • 1. Re: How to prevent grid data to be sorted after applying addItemAt() function on dataprovider?
          paul.williams Level 4

          Do you have any sorting already applied to your dataProvider? Has the

          grid been sorted (by clicking a column header) prior to the insert?

          • 2. Re: How to prevent grid data to be sorted after applying addItemAt() function on dataprovider?
            82.bansal Level 1

            Yes grid is sorted before adding items to the datasource............

            • 3. Re: How to prevent grid data to be sorted after applying addItemAt() function on dataprovider?
              Niranjan_swain

              Hi, do one thing. when u add data to dataprovider of grid at that time u take all ur datagrid column and make there soratable property false.

              example.

               

              1. suppose ur datagrid name is myGrid.

              2. dataProvider name is myData.

               

              when u add the data to myData u liked following example

               

               

              private var myData:ArrayCollection = new ArrayCollection();

               

              private function addDataToMyData():void

                   {

               

                   //find all column in ur grid

                   var gridColumns:Array = myGrid.columns;

                   for(var i:int=0; i<gridColumns.length; i++)

                        {

                        DataGridColumn(gridColumns[i]).sortable = false;

                        }

                   myGrid.columns = gridColumns;

               

                   var ob:Object = new Object();// set ur data in this object.

                  myData.addItemAt(ob, 3/*index*/);

                    myGrid.dataProvider = myData;

               

              }

               

              i think this will help u.

               

               

                       

              • 4. Re: How to prevent grid data to be sorted after applying addItemAt() function on dataprovider?
                82.bansal Level 1

                Thanks for the response niranjan, but it doesn't solve my problem. Actually I think grid sorting i8s not the root cause of the problem.

                 

                Let me explain in detail. Suppose I have a 1 column grid wuth data as below:

                 

                2

                2

                2

                3

                3

                3

                4

                4

                 

                No what I do On click of sum button I just sum aa the data in the column and display it aa a new row in the bottom. I am binding my grid with XMLListCollection and to add a new row at botom m using addItemAt() function. I work fine when i make sortablecolumns property of grid = false, but when this property is true then I got this as output

                 

                2

                2

                2

                3

                3

                3

                23  //sum

                4

                4

                 

                However it should give me

                 

                2

                2

                2

                3

                3

                3

                4

                4

                23 // sum

                 

                 

                I hope you understand the problem

                • 5. Re: How to prevent grid data to be sorted after applying addItemAt() function on dataprovider?
                  Niranjan_swain Level 1

                  hai , i am getting ur problem actually  when u sort the column it sort string wise. default sorting of grid column is string.

                  U have to change it to numeric . there is a property compareFunction(). it call when u click on the header for sorting.

                  U apply this column to the datagridColumn which column work with numeric data.in this function body u write ur logic to

                  compare the numeric data. i give a example below

                   

                  private function numeric_CompareFunc(itemA:Object, itemB:Object):int

                  {

                              var value1:Number= parseInt(itemA.@fieldName);

                              var  value2:Number = parseInt(itemA.@fieldName);

                              return ObjectUtil.numericCompare(value1, value2);

                  }

                   

                  <mx:datagrid  ------>

                  <mx:datagridColumn compareFunction=" numeric_CompareFunc"/> 

                   

                  here i am not write the datagrid structure detail. but mentatin how call this function. i sure this will help u.

                   

                  what is the result let me know. if working fine give me some points.

                   

                  Thanks

                  Niranjan