Flex:如何在数据网格中排序列几天(周一、周二、周三)?

发布于 2024-08-20 19:15:19 字数 155 浏览 4 评论 0 原文

我在数据网格中有一个列,其中包含“星期一”、“星期二”等 varchar 值,我想知道如何按正确的顺序对列进行排序。进行正常排序显然不起作用,因为日期不是按字母顺序自然排序的(周三之后周四)。我假设有某种 itemrenderer,但是有人有一段代码来说明它是如何工作的吗?

谢谢

I have a column in a data grid that has the values of "Monday", "Tuesday" etc, as varchars, and I wanted to know, how do i sort the column to be in the correct order. Doing a normal sort doesnt work obviously, as the days are not alphabetically ordered naturally (Wed after Thurs). I am assuming some sort of itemrenderer, but anyone got a snippet of code to illustrate how this would work?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路终见情 2024-08-27 19:15:19

据我所知,没有任何内置功能可以对一周中的几天进行排序或从一周中的某一天获取天数,因此您需要构建两者。不过,您不需要自定义 itemRenderer 来进行排序,只需一个排序比较函数即可。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;

            [Bindable]
            private var days:ArrayCollection = 
                                new ArrayCollection([{day: "Wednesday"},
                                                    {day: "Thursday"},
                                                    {day: "Monday"},
                                                    {day: "Tuesday"},
                                                    {day: "Sunday"},
                                                    {day: "Saturday"},
                                                    {day: "Friday"}
                                                    ]);

            private static var dayMap:Object = {
                                            Sunday: 0,
                                            Monday: 1,
                                            Tuesday: 2,
                                            Wednesday: 3,
                                            Thursday: 4,
                                            Friday: 5,
                                            Saturday: 6};

            private function daySorter(day1:Object, day2:Object):int {
                var i1:int = dayMap[day1.day];
                var i2:int = dayMap[day2.day];

                return i1 < i2 ? -1 : i1 == i2 ? 0 : 1;
            }
        ]]>
    </mx:Script>

    <mx:DataGrid dataProvider="{days}">
        <mx:columns>
            <mx:DataGridColumn dataField="day" sortCompareFunction="{daySorter}" />
        </mx:columns>
    </mx:DataGrid>
</mx:Application>

As far as I know there is nothing built-in either to sort days of the week or get a day number from a day of the week, so you need to build both. You don't need a custom itemRenderer to do sorting though, just a sort compare function.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;

            [Bindable]
            private var days:ArrayCollection = 
                                new ArrayCollection([{day: "Wednesday"},
                                                    {day: "Thursday"},
                                                    {day: "Monday"},
                                                    {day: "Tuesday"},
                                                    {day: "Sunday"},
                                                    {day: "Saturday"},
                                                    {day: "Friday"}
                                                    ]);

            private static var dayMap:Object = {
                                            Sunday: 0,
                                            Monday: 1,
                                            Tuesday: 2,
                                            Wednesday: 3,
                                            Thursday: 4,
                                            Friday: 5,
                                            Saturday: 6};

            private function daySorter(day1:Object, day2:Object):int {
                var i1:int = dayMap[day1.day];
                var i2:int = dayMap[day2.day];

                return i1 < i2 ? -1 : i1 == i2 ? 0 : 1;
            }
        ]]>
    </mx:Script>

    <mx:DataGrid dataProvider="{days}">
        <mx:columns>
            <mx:DataGridColumn dataField="day" sortCompareFunction="{daySorter}" />
        </mx:columns>
    </mx:DataGrid>
</mx:Application>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文