对 Flex 数据网格中的列进行排序
数据网格从后端数据库获取数据,该数据库具有类似的记录,
RecordID Division Department Date_Report_Submitted
1. Finance Accounting 11/1/2010
2. Engineering Design 4/2/2011
3. Engineering Implementation 4/2/2011
4. Support Chat_Support 2/4/2010
单击数据网格列(部门)中的标题会导致基于记录ID的排序,
Division Department Date_Report_Submitted
Finance Accounting 11/1/2010
Engineering Design 4/2/2011
Engineering Implementation 4/2/2011
Support Chat_Support 2/4/2010
而我希望它按数据网格列(部门)的字母顺序排序
Division Department Date_Report_Submitted
Finance Accounting 11/1/2010
Support Chat_Support 2/4/2010
Engineering Design 4/2/2011
Engineering Implementation 4/2/2011
,因为按照字典顺序,会计应位于 Chat_Support 之前。
<mx:DataGrid id="myRecords" dataProvider="{myRecords_dp}" width="810" height="274"
itemClick="getRecordData(event)">
<mx:columns>
<mx:DataGridColumn id="firstCol" width="180" fontFamily="Arial" fontSize="12"
wordWrap="true" />
<mx:Button label="Click to Sort" click="mysort()" />
</mx:columns>
</mx:DataGrid>
错误,
private function mysort():void
{
var sortField:SortField = new SortField();
sortField.compareFunction = mycompare;
sortField.descending = false;
var sort:Sort = new Sort();
sort.fields = [sortField];
myRecords.sort = sort;
myRecords.refresh();
}
private function mycompare(lhs:Object, rhs:Object):int
{
var valueA:String = lhs.text();
var valueB:String = rhs.text();
return ObjectUtil.stringCompare(valueA, valueB);
}
如
1061: Call to a possible undefined methodfresh through a reference with static type mx.controls:DataGrid.
对于 myRecords.refresh(); 并
通过静态类型 mx.controls:DataGrid 的引用对可能未定义的属性进行排序。
对于 myRecords.sort
如有任何建议,我们将不胜感激。
The datagrid gets its data from a back end database which has records like
RecordID Division Department Date_Report_Submitted
1. Finance Accounting 11/1/2010
2. Engineering Design 4/2/2011
3. Engineering Implementation 4/2/2011
4. Support Chat_Support 2/4/2010
Clicking on the headers in the Datagrid column(Department) results in a sort based on recordID like
Division Department Date_Report_Submitted
Finance Accounting 11/1/2010
Engineering Design 4/2/2011
Engineering Implementation 4/2/2011
Support Chat_Support 2/4/2010
whereas I want it to be sorted alphabetically for the Datagrid column(Department) like
Division Department Date_Report_Submitted
Finance Accounting 11/1/2010
Support Chat_Support 2/4/2010
Engineering Design 4/2/2011
Engineering Implementation 4/2/2011
since Accounting should come before Chat_Support as per lexicographical order.
Looked at http://blog.flexexamples.com/2008/04/09/creating-a-custom-sort-on-a-datagrid-control-in-flex/#more-590 and have something like
<mx:DataGrid id="myRecords" dataProvider="{myRecords_dp}" width="810" height="274"
itemClick="getRecordData(event)">
<mx:columns>
<mx:DataGridColumn id="firstCol" width="180" fontFamily="Arial" fontSize="12"
wordWrap="true" />
<mx:Button label="Click to Sort" click="mysort()" />
</mx:columns>
</mx:DataGrid>
and
private function mysort():void
{
var sortField:SortField = new SortField();
sortField.compareFunction = mycompare;
sortField.descending = false;
var sort:Sort = new Sort();
sort.fields = [sortField];
myRecords.sort = sort;
myRecords.refresh();
}
private function mycompare(lhs:Object, rhs:Object):int
{
var valueA:String = lhs.text();
var valueB:String = rhs.text();
return ObjectUtil.stringCompare(valueA, valueB);
}
I get errors like
1061: Call to a possibly undefined method refresh through a reference with static type mx.controls:DataGrid.
for myRecords.refresh();
and
Access of possibly undefined property sort through a reference with static type mx.controls:DataGrid.
for myRecords.sort
Any suggestions would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你问题中的代码有点乱。例如,您不能将按钮用作 DataGrid 内的列。这会产生编译器错误。
尽管如此,我还是根据您的示例数据和您共享的代码整理了一个示例,以便向您展示 DataGrid 排序并不像您声称的那样工作。
在这里玩一下。
为了获得有关您的问题的更多帮助;你将不得不分享一些真实的代码来演示一些问题;不是你在互联网上找到的不可编译的东西的大杂烩。
The code in your question is a bit of a mess. For example, you cannot use a button as a column inside a DataGrid. That gives a compiler error.
Nevertheless, I put together a sample, based off your sample data and the code you shared, in order to show you that DataGrid sorting does not work the way you claim it does.
Play with it here.
To get more help with your issue; you're going to have to share some real code to demonstrate some issue; not a mish-mash of non-compileable stuff you found on the Internet.
对于问题:
尝试将 dataProvider 转换为 ArrayCollection 或 XMLListCollection (无论您使用哪个):
For the issues:
Try casting the dataProvider to an ArrayCollection or XMLListCollection (Whichever you're using):