Flex DataGrid column sort descending not working

夙愿已清 提交于 2020-01-06 18:07:15

问题


I'm having a strange problem.

In Flex 4, I have a Spark DataGrid and have both an item renderer and a sort compare function defined for one of the columns. Strangely, this column will not sort in descending order.

The data is originally not sorted. Clicking on the column header displays the "up" arrow and the data sorts in ascending order. However, clicking on the column header again does nothing. The "up" arrow stays and the data stays sorted in ascending order.

I have debugged through the code and found that column.sortDescending in the sort compare function is always false (should this be true when the a descending sort should happen?).

I have Googled this for a long while and searched on here but haven't found anything.

Anyone have any ideas?

Thanks in advance.

//Edit

Simple Example code:

CreationComplete function initializes data using some random values:

protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                arrayData = new ArrayCollection();

                var obj:Object;
                for(var i:Number=0; i < 10; i++) {
                    obj = new Object();
                    obj.value1 = i;
                    obj.value2 = Math.floor(Math.random() * (1 + 100 - 1)) + 1;
                    obj.value3 = Math.floor(Math.random() * (1 + 100 - 1)) + 1;

                    arrayData.addItem(obj);
                }
            }

Datagrid defines columns with 4th column having an item renderer and sort compare function defined.

<s:DataGrid x="122" y="142" width="391" height="223" requestedRowCount="4" dataProvider="{arrayData}">
        <s:columns>
            <s:ArrayList>
                <s:GridColumn dataField="value1" headerText="Value 1"></s:GridColumn>
                <s:GridColumn dataField="value2" headerText="Value 2"></s:GridColumn>
                <s:GridColumn dataField="value3" headerText="Value 3"></s:GridColumn>
                <s:GridColumn headerText="Value 3" sortable="true" itemRenderer="ItemRendererTest" sortCompareFunction="sortCompareFunc"></s:GridColumn>
            </s:ArrayList>
        </s:columns>
        <s:typicalItem>
            <fx:Object dataField1="Sample Data" dataField2="Sample Data" dataField3="Sample Data"></fx:Object>
        </s:typicalItem>
    </s:DataGrid>

Item renderer code:

override public function prepare(hasBeenRecycled:Boolean):void {
                var columnString:String = data["value1"] + ":" + data["value2"] + ":" + data["value3"];

                lblData.text = columnString;
            }

Sort compare function:

private function sortCompareFunc(obj1:Object, obj2:Object, col:GridColumn):int {
                var obj1String:String = obj1["value1"] + ":" + obj1["value2"] + ":" + obj1["value3"];
                var obj2String:String = obj2["value1"] + ":" + obj2["value2"] + ":" + obj2["value3"];

                var collator:SortingCollator = new SortingCollator();

                return collator.compare(obj1String, obj2String);
            }

回答1:


Just had something similar to this myself seems that the spark grid control needs a dataField defining even though you have a custom sort compare function, simply try adding dataField="value1" on your 4th column even though your not going to use it, see if that helps.



来源:https://stackoverflow.com/questions/12176784/flex-datagrid-column-sort-descending-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!