Flex 自定义排序功能与服务器端支持

发布于 2024-08-22 16:15:18 字数 206 浏览 13 评论 0原文

Flex 中可用的排序功能假设您可以访问所有数据,但我使用分页数据网格(带有自定义代码),数据网格绑定到 ArrayCollection 实例,在下一页调用我更改数据dataprovider 的数据和一切工作正常,但为了排序,我需要重写单击或事件,更好地重写 arraycollection 的排序方法

所有这些都是为了能够进行服务器端排序。

有人遇到过这种问题吗?

The sorting capabilities that are available in Flex assume that you have access to all the data, but I'm using a paginated datagrid (with custom code), the datagrid is binded to an ArrayCollection instance, on the next page call I change the data of the dataprovider and everything works ok, but for sorting I need to override the click or event better override the sort method of the arraycollection

All this is to be able to do a server-side sorting.

Has anyone faced this kind of issue?

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

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

发布评论

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

评论(4

躲猫猫 2024-08-29 16:15:18

以下是我用来解决这个问题的步骤...这个解决方案的好处是我可以允许 Flex 与我一起“排序”,这使得排序方向图标在 DataGrid 上可见。

步骤:

  1. 使用DataGrid的headerRelease事件拦截“排序”请求。

  2. 保留列及其当前排序方向的本地映射...这模仿了 flex 对其排序所做的操作...因此所有列都以“升序”开始,然后仅在以下情况下切换方向连续单击给定列两次。这可能可以通过观察 flex 的内部结构来完成,但我不想尝试。

  3. headerRelease 事件使用其事件中请求的列,以及先前请求的列和当前列排序方向的映射来决定是否更新本地映射中的排序方向。

  4. 调用服务器以获取适当排序的数据页。

此时,Flex 也想对数据进行排序...(除非您阻止了 headerRelease 事件的 Default() )...以允许 Flex 对数据进行“排序”而不弄乱任何内容,以便方向图标继续如果功能正常,您可以:

  1. 在服务器上的 SQL 结果集中添加一个“行 id”字段,它只是结果集中每个连续行的计数器,在排序和分页之后...这个 id无论应用哪种排序方向,都将始终为升序。

  2. 将 DataGridColumns 上的 sortCompareFunction 设置为使用此行 id 的“虚拟”排序...如下所示:

public function doNothingSort( a:Object, b:Object ):int {
    if( 我们当前处于升序方向 )
        返回 ObjectUtil.numericCompare( a.new, b.num );
    别的
        返回 ObjectUtil.numericCompare( b.num, a.num );
}

这将允许 Flex 运行整个“页面”并保持一切不变......所以它很高兴,你也很高兴,因为你有图标......

Here are the steps I used to solve this issue... the nice thing about this solution is that I can allow Flex to "sort" along with me, which keeps the sort direction icons visible on the DataGrid.

Steps:

  1. use the headerRelease event of the DataGrid to intercept the "sort" request.

  2. keep a local map of columns and their current sort directions... this is a mimic of what flex is doing on its sort... so all columns start out with "ascending", and then the direction is toggled only when a given column is clicked twice in a row. this could probably be done by watching the flex internal structures, but i didn't care to try that.

  3. the headerRelease event uses the requested column from its event, along with the previous requested column and the map of current column sort directions to decide whether to update the sort direction in the local map.

  4. make the call to the server to get the appropriately sorted page of data.

at this point, Flex wants to sort the data as well... ( unless you preventDefault() the headerRelease event )... to allow Flex to "sort" the data without messing anything up, and so that the direction icons continue to function properly, you can:

  1. add a "row id" field to your SQL result set on the server, which is simply a counter for each sequential row in the result set, after it's been sorted and paged... this id will always be ascending, no matter what sort direction is applied.

  2. set the sortCompareFunction on the DataGridColumns to a "dummy" sort that uses this row id... as such:

public function doNothingSort( a:Object, b:Object ):int {
    if( weAreCurrentlyInAscendingDirection )
        return ObjectUtil.numericCompare( a.new, b.num );
    else
        return ObjectUtil.numericCompare( b.num, a.num );
}

This will allow flex to run through the "page" and leave everything as is... so it's happy and you're happy because you have the icons...

无名指的心愿 2024-08-29 16:15:18

我不确定我是否真正理解这个问题,但听起来您需要在服务器端进行排序。如果您没有将所有数据加载到 Flex 应用程序中,则无法对其进行排序。

I am not certain that I really understand the question, but it sounds like you need to do your sorting on the server-side. If you do not have all of the data loaded into the flex application, there is no way you could sort it.

明月松间行 2024-08-29 16:15:18

我是这样理解这个问题的:

他在flex客户端中有一部分总数据。由于客户端不知道所有数据,因此无法在客户端完成排序。
他已经让服务器端排序工作起来了。

他现在需要做的是:当用户单击数据网格的标题时,他想要进行服务器调用并取回排序后的数据。
单击网格标题时的默认行为是数据在客户端排序。
这就是为什么他需要在客户端做一些事情。

我唯一发现的是:

<mx:DataGrid xmlns:mx="http://www.adobe.com/2006/mxml"
headerRelease="onHeaderRelease(event)">

一旦单击标题上的鼠标按钮(再次释放鼠标按钮),就会调用指定的 onHeaderRelease 函数。

示例函数。也许你可以从这里拿起

        public function onHeaderRelease(evt:DataGridEvent):void
        {
            var grd:DataGrid = DataGrid(evt.currentTarget);
            Alert.show(evt.columnIndex + " : " + (DataGridColumn)(grd.columns[evt.columnIndex]).sortDescending, "ColumnIndex : Sorted Descending?");
            // do the server called and get the sorted array back
        }

我希望这对你有帮助!

I understood the problem like this:

He has a part of the total data in the flex client. Because the client does not know all of the data, sorting can't be done client side.
He already has the serverside sorting working.

What he needs to do now is: When the user clicks on the header of the datagrid he wants to make a server call and get the sorted data back.
The default behaviour when the header of a grid is clicked is that the data is sorted client side.
So thats why he needs to to something client side.

The only thing I found was this:

<mx:DataGrid xmlns:mx="http://www.adobe.com/2006/mxml"
headerRelease="onHeaderRelease(event)">

The specified function onHeaderRelease is called as soon as the mousebutton on a header was clicked (the mousebutton was released again).

Example function. Maybe you can pick up from here

        public function onHeaderRelease(evt:DataGridEvent):void
        {
            var grd:DataGrid = DataGrid(evt.currentTarget);
            Alert.show(evt.columnIndex + " : " + (DataGridColumn)(grd.columns[evt.columnIndex]).sortDescending, "ColumnIndex : Sorted Descending?");
            // do the server called and get the sorted array back
        }

I hope this will help you!

二手情话 2024-08-29 16:15:18

我实现了以下解决方案,目前该解决方案运行良好,但可能还可以进行改进。

我扩展了 ArrayCollection 类并重写了排序 set/get 和刷新方法

package custom
{
import mx.collections.ArrayCollection;
import mx.collections.Sort;

/**
 *  Dispatched when a sort is required
 *
 *  @eventType custom.PaginatedCollectionEvent.SORT
 */
[Event(name="sort" , type="custom.PaginatedCollectionEvent")]

public class PaginatedCollection extends ArrayCollection
{
    private var _sort:Sort;

    public function PaginatedCollection(source:Array=null)
    {
        super(source);
    }

    public function setDefaultSort (s:Sort):void
    {
        _sort = s;
    }


    override public function set sort(s:Sort):void
    {
        _sort = s;
        if (!s)
            return;

        var event:PaginatedCollectionEvent = new PaginatedCollectionEvent(PaginatedCollectionEvent.SORT);
        event.fields = s.fields;
        event.s = s;
        this.dispatchEvent(event);
    }

    override public function get sort():Sort
    {
        return _sort;
    }

    /**
     * Avoid the internal sorting implementation, with this it's possible
     * to do a server side sort.
     *
     * @return true
     */
    override public function refresh():Boolean
    {
        return true;
    }

    /**
     * Wrapper for ArrayCollection refesh implementation
     */
    public function superRefresh():Boolean
    {
        return super.refresh();
    }
}

}

I implemented the following solution, which at the moment is working pretty well, but probably there are improvements that can be made.

I extended ArrayCollection class and override the sort set/get and refresh method

package custom
{
import mx.collections.ArrayCollection;
import mx.collections.Sort;

/**
 *  Dispatched when a sort is required
 *
 *  @eventType custom.PaginatedCollectionEvent.SORT
 */
[Event(name="sort" , type="custom.PaginatedCollectionEvent")]

public class PaginatedCollection extends ArrayCollection
{
    private var _sort:Sort;

    public function PaginatedCollection(source:Array=null)
    {
        super(source);
    }

    public function setDefaultSort (s:Sort):void
    {
        _sort = s;
    }


    override public function set sort(s:Sort):void
    {
        _sort = s;
        if (!s)
            return;

        var event:PaginatedCollectionEvent = new PaginatedCollectionEvent(PaginatedCollectionEvent.SORT);
        event.fields = s.fields;
        event.s = s;
        this.dispatchEvent(event);
    }

    override public function get sort():Sort
    {
        return _sort;
    }

    /**
     * Avoid the internal sorting implementation, with this it's possible
     * to do a server side sort.
     *
     * @return true
     */
    override public function refresh():Boolean
    {
        return true;
    }

    /**
     * Wrapper for ArrayCollection refesh implementation
     */
    public function superRefresh():Boolean
    {
        return super.refresh();
    }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文