Telerik RadGrid CustomSorting 无需访问数据库?

发布于 2024-08-24 20:31:15 字数 985 浏览 4 评论 0原文

我尝试在 Telerik 论坛上发帖,但现在每次我尝试打开我的帖子时,我都会得到 “哎呀... 我们的服务器似乎有问题。” 所以我在这里发布了这个问题。

我对 Telerik 和 RadGrid 还很陌生。我正在尝试修改现有项目,因为客户需要自定义排序。有一个数据字段可能包含数字或文本,因此它是字符串类型,但有时必须将其排序为数字。所以我去了这个链接:

http://demos .telerik.com/aspnet-ajax/grid/examples/programming/sort/defaultcs.aspx

http://www.telerik.com/help/aspnet-ajax/grdapplycustomsortcriteria。 html

示例显示:

“打开自定义排序后,RadGrid 将显示排序图标,但实际上不会对数据进行排序。” 但添加AllowCustomSorting来禁用默认排序似乎还不够。

在实现 SortCommand 时,我注意到我必须做  e.取消= true;

因为否则会发生默认排序。为什么文档和示例中都没有提到这一点?

但主要问题是 - 在 SortCommand 内部,我的 RadGrid 已经加载了所有项目。那么有没有办法对它们进行排序以避免访问数据库呢?我尝试访问“对象源,GridSortCommandEventArgs e”的各种 Items 属性,但所有 Items 都是只读的,因此我无法对它们进行排序并附加回 RadGrid。

感谢您的任何想法。

I tried to post on Telerik forum, but now each time I try to open my thread, I get
"Oops...
It seems there was a problem with our server."
So I posted this question here.

I am pretty new to Telerik and RadGrid. I am trying to modify existing project because client needs custom sorting. There is a data field which may contain numbers or text so it is a string type but sometimes it has to be sorted as numbers. So I went to this link:

http://demos.telerik.com/aspnet-ajax/grid/examples/programming/sort/defaultcs.aspx

and
http://www.telerik.com/help/aspnet-ajax/grdapplycustomsortcriteria.html

The example says:

"With custom sorting turned on, RadGrid will display the sorting icons but it will not actually sort the data."
but it seems it is not enough to add AllowCustomSorting to disable default sorting.

When implementing SortCommand, I noticed that I have to do
 e.Canceled = true;

because else default sorting occurs. Why this is not mentioned in the documentation nor example?

But the main question is - inside of SortCommand my RadGrid already has all items loaded. So is there any way to sort them to avoid hitting database? I tried accessing various Items properties of both "object source, GridSortCommandEventArgs e", but all Items are read-only, so I cannot sort them and attach back to the RadGrid.

Thanks for any ideas.

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

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

发布评论

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

评论(2

白馒头 2024-08-31 20:31:15

您可以在objectDatasource的OnSelecting事件中设置sortExpression并在SelectMethod中使用它。

protected void odsGridData_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
   e.InputParameters["filterExpression"] = grdMyTasks.MasterTableView.FilterExpression;
   //Set the Sort Expression and use this in the Get method
   e.InputParameters["sortExpression"] = grdMyTasks.MasterTableView.SortExpressions.GetSortString();
   e.Arguments.StartRowIndex = grdMyTasks.CurrentPageIndex;
   e.Arguments.MaximumRows = grdMyTasks.PageSize;
}

这样您就可以执行自定义排序并将数据传递到 RadGrid。

希望这有帮助。

You can set the sortExpression in the OnSelecting event of the objectDatasource and use it in the SelectMethod.

protected void odsGridData_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
   e.InputParameters["filterExpression"] = grdMyTasks.MasterTableView.FilterExpression;
   //Set the Sort Expression and use this in the Get method
   e.InputParameters["sortExpression"] = grdMyTasks.MasterTableView.SortExpressions.GetSortString();
   e.Arguments.StartRowIndex = grdMyTasks.CurrentPageIndex;
   e.Arguments.MaximumRows = grdMyTasks.PageSize;
}

This way you can perform custom sort and pass on the data to the RadGrid.

Hope this helps.

梦在深巷 2024-08-31 20:31:15

这是我使用的一些代码的示例,但未访问数据库。我将 MVC 3 与 Razor 视图引擎一起使用。请注意 Ajax 绑定。不要忘记使用 Telerik.Web.Mvc.UI 添加并使用 [GridResult] 注释控制器中的“Post”方法,并返回 GridModel 以获取 Json 结果集。

    using Telerik.Web.Mvc;

    [GridAction]
    public ActionResult AjaxGridSelect()
    {
        return View(new GridModel(db.lm_m_category));
    }

这里是index.cshtml(razor引擎),关键是Ajax绑定。

 @model IEnumerable<LinkManagerAdmin.Dal.lm_r_category>  
 @using Telerik.Web.Mvc.UI

 @(Html.Telerik().Grid(Model)
    .Name("Grid")
    .DataKeys(keys => keys.Add(c => c.category_id ))
    .DataBinding(dataBinding => dataBinding.Ajax()
    .Select("AjaxGridSelect", "CategoryTree")
    .Insert("GridInsert", "CategoryTree", new { GridEditMode.PopUp, GridButtonType.ImageAndText })
    .Update("GridUpdate", "CategoryTree", new { GridEditMode.InLine, GridButtonType.ImageAndText })
    .Delete("GridDelete", "CategoryTree", new { GridEditMode.InLine, GridButtonType.ImageAndText }))
    .Columns(columns =>
    {
      columns.Bound(p => p.category_name).Width(150);
      columns.Bound(p => p.status_cd).Width(100);
      columns.Command(commands =>
      {
        commands.Edit().ButtonType(GridButtonType.ImageAndText);
        commands.Delete().ButtonType(GridButtonType.ImageAndText);
      }).Width(180).Title("Commands");
    })
   .Editable(editing => editing.Mode(GridEditMode.InLine))
   .Pageable(paging => paging.PageSize(50)
   .Style(GridPagerStyles.NextPreviousAndNumeric)
   .Position(GridPagerPosition.Bottom))
   .Sortable(o => o.OrderBy(sortcol => 
   {
                sortcol.Add(a => a.category_name);
                sortcol.Add(a => a.add_date);
   })
   .Filterable()
   .Groupable()
   .Selectable())

Here is an example of some code I use that does not hit the database. I'm using MVC 3 with the Razor view engine. Notice the Ajax binding. Don't forget to add using Telerik.Web.Mvc.UI and annotate the "Post" methods in your controller with [GridResult] and to return GridModel to get the Json resultset.

    using Telerik.Web.Mvc;

    [GridAction]
    public ActionResult AjaxGridSelect()
    {
        return View(new GridModel(db.lm_m_category));
    }

Here is the index.cshtml (razor engine), the key is the Ajax binding.

 @model IEnumerable<LinkManagerAdmin.Dal.lm_r_category>  
 @using Telerik.Web.Mvc.UI

 @(Html.Telerik().Grid(Model)
    .Name("Grid")
    .DataKeys(keys => keys.Add(c => c.category_id ))
    .DataBinding(dataBinding => dataBinding.Ajax()
    .Select("AjaxGridSelect", "CategoryTree")
    .Insert("GridInsert", "CategoryTree", new { GridEditMode.PopUp, GridButtonType.ImageAndText })
    .Update("GridUpdate", "CategoryTree", new { GridEditMode.InLine, GridButtonType.ImageAndText })
    .Delete("GridDelete", "CategoryTree", new { GridEditMode.InLine, GridButtonType.ImageAndText }))
    .Columns(columns =>
    {
      columns.Bound(p => p.category_name).Width(150);
      columns.Bound(p => p.status_cd).Width(100);
      columns.Command(commands =>
      {
        commands.Edit().ButtonType(GridButtonType.ImageAndText);
        commands.Delete().ButtonType(GridButtonType.ImageAndText);
      }).Width(180).Title("Commands");
    })
   .Editable(editing => editing.Mode(GridEditMode.InLine))
   .Pageable(paging => paging.PageSize(50)
   .Style(GridPagerStyles.NextPreviousAndNumeric)
   .Position(GridPagerPosition.Bottom))
   .Sortable(o => o.OrderBy(sortcol => 
   {
                sortcol.Add(a => a.category_name);
                sortcol.Add(a => a.add_date);
   })
   .Filterable()
   .Groupable()
   .Selectable())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文