【问题标题】:How to sort columns in jQuery grid in MVC 3?如何在 MVC 3 中对 jQuery 网格中的列进行排序?
【发布时间】:2012-02-14 01:20:08
【问题描述】:

我有这个使用 jQuery 网格的示例 MVC 项目。我遇到的只有一个问题,那就是 jQuery 网格的排序功能。

型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MyAjaxSample.Models
{
    public class Candy
    {
        [Key]        
        public long ID { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
        public string Color { get; set; }
        public bool Expired { get; set; }
    }
}  

控制器:请看注释代码,出现错误的地方

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyAjaxSample.Models;

namespace MyAjaxSample.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult DynamicGridData(string sidx, string sord, int page, int rows)
        {
            List<Candy> candyList = new List<Candy>();
            for (int i = 0; i <= 50; i++)
            {
                Candy candy = new Candy();
                candy.ID = i;
                candy.Name = "Candy " + i.ToString();
                candy.Price = i * 0.19;
                candy.Color = "Black";
                candy.Expired = false;

                candyList.Add(candy);
            }

            var context = candyList;
            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = context.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            // This is not working
            //var candies = context.OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize); ;
            var candies = context;

            var jsonData = new
            {
                total = totalPages,
                page,
                records = totalRecords,
                rows = (
                    from item in candies
                    select new
                    {
                        i = candies,
                        cell = new string[] { item.ID.ToString(), item.Name, item.Price.ToString(), item.Color, item.Expired.ToString() }
                    }).ToArray()
            };
            return Json(jsonData);
        }
    }
}

查看:

<link href="/Content/jquery-ui-1.8.7.css" rel="stylesheet" type="text/css" />
<link href="/Content/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<link href="/Content/ui.multiselect.css" rel="stylesheet" type="text/css" />

<script src="/Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>

<script src="/Scripts/grid.locale-en.js" type="text/javascript"></script>
<script src="/Scripts/jquery.jqGrid.min.js" type="text/javascript"></script>

<script type="text/javascript">
    jQuery(document).ready(function () {
        jQuery("#list").jqGrid({
            url: '/Home/DynamicGridData/',
            datatype: 'json',
            mtype: 'POST',
            colNames: ['ID', 'Name', 'Price', 'Color', 'Expired'],
            colModel: [
        { name: 'ID', index: 'ID', width: 40, align: 'left' },
        { name: 'Name', index: 'Name', width: 40, align: 'left' },
        { name: 'Price', index: 'Price', width: 400, align: 'left' },
        { name: 'Color', index: 'Color', width: 400, align: 'left' },
        { name: 'Expired', index: 'Expired', width: 400, align: 'left' }
        ],
            pager: jQuery('#pager'),
            rowNum: 80,
            rowList: [20, 10, 20, 50],
            sortname: 'ID',
            sortorder: "desc",
            viewrecords: true,
            imgpath: '',
            caption: 'My first grid'
        });
    }); 
</script>  

<table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
<div id="pager" class="scroll" style="text-align:center;"></div>  

这里唯一的问题是排序功能。实际上我会将它用于实体框架,我使用List 的原因是因为(我认为)它们具有相同的 sortOrder 参数。无论如何,我希望有人能提供帮助。

【问题讨论】:

    标签: c# jquery asp.net-mvc-3 model-view-controller jquery-ui


    【解决方案1】:

    使用Demo 链接查看示例示例。

    【讨论】:

    • 在 jquery 网格中,header 的 td 替换为 th。现在用 tbody 限制数据行,用 thead 标签限制标题行。
    • 谢谢,但我真的不明白。我只是 jQuery 的新手。 :(
    • 你必须在你的css中做它的标题。
    • +1 分享了如此出色的插件。但我仍然无法使其适用于我的项目。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-04-07
    • 2014-03-04
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多