Telerik RadGrid排序客户端JavaScript jQuery

时间:2021-11-07 15:29:28

I need a way to sort a rad grid using javascript of jQuery I want to avoid using a web service because no where else in the project makes use of a web service.

我需要一种方法来使用jQuery的javascript对rad网格进行排序我想避免使用Web服务,因为项目中的其他任何地方都没有使用Web服务。

I bind to the grid using JavaScript and don't require a OnNeedDataSource event.

我使用JavaScript绑定到网格,不需要OnNeedDataSource事件。

<ClientEvents OnRowSelected="RowSelected" OnRowDeselected="RowDeselected" OnCommand="RadGridCommand"/>

On the client RadGridCommand event I cancel default command to prevent postback/ajax request and check if the command is a sort event at this point i would like to provide a way to sort my grid.

在客户端RadGridCommand事件我取消默认命令以防止回发/ ajax请求并检查命令是否是排序事件此时我想提供一种方法来排序我的网格。

      //RadGrid Command function 
      function RadGridCommand(sender, args) {

          args.set_cancel(true); //cancel the default command to prevent postback/ajax request

          if (args.get_commandName() == "Sort") {
             var sortExpressions = sender.get_masterTableView().get_sortExpressions();

Any suggestions would be much appreciated

我们欢迎所有的建议

1 个解决方案

#1


0  

I got it working using a JavaScript sort function

我使用JavaScript排序功能工作

 LotResults.sort(function (a, b) {
    return b.LotResult - a.LotResult    //Sort DESC order
 })

I use the command name to check if the sort command is called and cancel the default command to prevent a postback.

我使用命令名来检查是否调用了sort命令,并取消默认命令以防止回发。

I then grab the sortExpressions to get the sort order to decide if the order is to be ascending or descending order. And finally bind my object to the grid to update the view.

然后,我获取sortExpressions以获取排序顺序,以确定顺序是升序还是降序。最后将我的对象绑定到网格以更新视图。

Below is how I did it needs a bit more work from my side to get it to the way I need it to be, but it does work and is sorting my grid :)

以下是我的工作方式,我需要做更多的工作才能达到我需要它的方式,但它确实有效并且正在排序我的网格:)

//RadGrid Command function 
function RadGridCommand(sender, args) {

 args.set_cancel(true); //cancel the default command to prevent postback/ajax request

 if (args.get_commandName() == "Sort") {
    var sortExpressions = sender.get_masterTableView().get_sortExpressions();
    var sortVal = sortExpressions.toString();

    if (sortVal != "") {

       var fieldName = sortExpressions.getItem(0).get_fieldName();
       var sortOrder = sortExpressions.getItem(0).get_sortOrder();

       if (sortOrder == 1) {
          LotResults.sort(function (a, b) {
                   return b.LotResult - a.LotResult  
          })
        }
        else if (sortOrder == 2) {                    
                LotResults.sort(function (a, b) {
                          return a.LotResult - b.LotResult
                })
        }
   }

   var masterTable = $find("<%= DeactivationLotResultsRadGrid.ClientID %>").get_masterTableView(); 
   masterTable.set_virtualItemCount(LotResults.length);
   masterTable.set_dataSource(LotResults);
   masterTable.dataBind();
   }}

#1


0  

I got it working using a JavaScript sort function

我使用JavaScript排序功能工作

 LotResults.sort(function (a, b) {
    return b.LotResult - a.LotResult    //Sort DESC order
 })

I use the command name to check if the sort command is called and cancel the default command to prevent a postback.

我使用命令名来检查是否调用了sort命令,并取消默认命令以防止回发。

I then grab the sortExpressions to get the sort order to decide if the order is to be ascending or descending order. And finally bind my object to the grid to update the view.

然后,我获取sortExpressions以获取排序顺序,以确定顺序是升序还是降序。最后将我的对象绑定到网格以更新视图。

Below is how I did it needs a bit more work from my side to get it to the way I need it to be, but it does work and is sorting my grid :)

以下是我的工作方式,我需要做更多的工作才能达到我需要它的方式,但它确实有效并且正在排序我的网格:)

//RadGrid Command function 
function RadGridCommand(sender, args) {

 args.set_cancel(true); //cancel the default command to prevent postback/ajax request

 if (args.get_commandName() == "Sort") {
    var sortExpressions = sender.get_masterTableView().get_sortExpressions();
    var sortVal = sortExpressions.toString();

    if (sortVal != "") {

       var fieldName = sortExpressions.getItem(0).get_fieldName();
       var sortOrder = sortExpressions.getItem(0).get_sortOrder();

       if (sortOrder == 1) {
          LotResults.sort(function (a, b) {
                   return b.LotResult - a.LotResult  
          })
        }
        else if (sortOrder == 2) {                    
                LotResults.sort(function (a, b) {
                          return a.LotResult - b.LotResult
                })
        }
   }

   var masterTable = $find("<%= DeactivationLotResultsRadGrid.ClientID %>").get_masterTableView(); 
   masterTable.set_virtualItemCount(LotResults.length);
   masterTable.set_dataSource(LotResults);
   masterTable.dataBind();
   }}