客户端上的ASP.NET MVC的WebGrid排序 [英] Client side sorting on ASP.NET MVC WebGrid

查看:141
本文介绍了客户端上的ASP.NET MVC的WebGrid排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含一个MVC的WebGrid如下

I have a partial view which contain an MVC WebGrid as below

 <div id="grid">
            @{
                var grid = new WebGrid(source: Model.Items,
                                       defaultSort: "Name", 
                                       rowsPerPage: 100);


            }
            @grid.GetHtml(columns: grid.Columns(
                        grid.Column(columnName: "Name", header: "Name", canSort:true),
                        grid.Column(columnName: "Code", header: "Code")
                    ))
            </div>

这部分视图使用Jquery Ajax调用加载,结果被插入到主网页的DIV。

This partial view is loaded using Jquery ajax call and result is inserted into a DIV in the main page.

表渲染不错,但我的问题是,排序总是生成一个回调到服务器。我想排序时只在客户端发生。是否有可能使用不使用外部数据表像jQuery的数据表中的WebGrid?

The table render fine but my problem is that the sorting always generates a callback to the server. I want the sorting to happen at the client side only. Is it possible using WebGrid without using external datatables like jQuery datatable?

在此先感谢

推荐答案

您或许应该实行克莱因端自行根据装载的表看看的这里...

You should probably implement Cline-Side Sorting by yourself according to the loaded table take a look here...

请注意!:你总是可以让使用HTML属性标记您的WebGrid它更通用。
标签中的表数据clineSideSort =真正的'再添加一个jQuery事件,将附加JS功能,所有这些表持此属性...

NOTE!: you could always make it more generic by using html attributes to tag your WebGrid. Tag the table with 'data-clineSideSort=true' then add a jquery event that will attach the JS functionality to all such tables holding this property...

 function SortTable(sortOn)
 {
     var table = document.getElementById('results');
     var tbody = table.getElementsByTagName('tbody')[0];
     var rows = tbody.getElementsByTagName('tr');

     var rowArray = new Array();
     for (var i = 0, length = rows.length; i < length; i++)
     {
         rowArray[i] = new Object;
         rowArray[i].oldIndex = i;
         rowArray[i].value = rows[i].getElementsByTagName('td')[sortOn].firstChild.nodeValue;
     }

     if (sortOn == sortedOn)         {
         rowArray.reverse();
     }
     else         {
         sortedOn = sortOn;
         /*
    Decide which function to use from the three:RowCompareNumbers,
    RowCompareDollars or RowCompare (default).
    For first column, I needed numeric comparison.
    */
         if (sortedOn == 0)             {
             rowArray.sort(RowCompareNumbers);
         }
         else             {
             rowArray.sort(RowCompare);
         }
     }

     var newTbody = document.createElement('tbody');
     for (var i = 0, length = rowArray.length; i < length; i++)
     {
         newTbody.appendChild(rows[rowArray[i].oldIndex].cloneNode(true));
     }

     table.replaceChild(newTbody, tbody);
 }

 function RowCompare(a, b)
 {
     var aVal = a.value;
     var bVal = b.value;
     return (aVal == bVal ? 0 : (aVal > bVal ? 1 : -1));
 }

 // Compare number
 function RowCompareNumbers(a, b)
 {
     var aVal = parseInt(a.value);
     var bVal = parseInt(b.value);
     return (aVal - bVal);
 }

 // compare currency
 function RowCompareDollars(a, b)
 {
     var aVal = parseFloat(a.value.substr(1));
     var bVal = parseFloat(b.value.substr(1));
     return (aVal - bVal);
 }

这篇关于客户端上的ASP.NET MVC的WebGrid排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆