1

在我的 CS 页面中,我有以下代码

   public JsonResult DynamicGridData(string sidx, string sord, int page, int rows)
    {

        Employee _emp = new Employee();

        List<Employee> _lstemp = _emp.GetallEmp();
        int pageIndex = Convert.ToInt32(page) - 1;
        int pageSize = rows;
        int totalRecords = _lstemp.Count();
        int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);


        var jsonData = new
        {
            total = totalPages,
            page,
            records = totalRecords,
            rows = (
                from emp in _lstemp
                select new
                {
                    i = emp.ID,
                    cell = new string[] { emp.ID.ToString(), emp.FirstName.ToString(), emp.LastName.ToString(),emp.Age.ToString(),emp.State.ToString(),emp.Country.ToString() }
                }).ToArray()
        };
        return Json(jsonData);
    }

我的模型是

     public class Employee
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public string State { get; set; }
    public string Country { get; set; }

    public List<Employee> GetallEmp()
    {
        List<Employee> list = new List<Employee>()
        {
            new Employee{ID=1,FirstName="Asish",LastName="Nehra",Age=25,State="A",Country="India"},
            new Employee{ID=2,FirstName="Nsish",LastName="Oehra",Age=35,State="B",Country="Sri Lanka"},
            new Employee{ID=3,FirstName="Psish",LastName="Lehra",Age=26,State="C",Country="Bangladesh"},
            new Employee{ID=4,FirstName="Jsish",LastName="Hehra",Age=25,State="D",Country="Australia"},
            new Employee{ID=5,FirstName="Usish",LastName="Tehra",Age=85,State="E",Country="Kenya"},
            new Employee{ID=6,FirstName="Rsish",LastName="Lehra",Age=15,State="F",Country="India"},
            new Employee{ID=7,FirstName="Isish",LastName="Eehra",Age=5,State="G",Country="Pakistan"},
             new Employee{ID=8,FirstName="Asish",LastName="Nehra",Age=25,State="A",Country="India"},
            new Employee{ID=9,FirstName="Nsish",LastName="Oehra",Age=35,State="B",Country="Sri Lanka"},
            new Employee{ID=10,FirstName="Psish",LastName="Lehra",Age=26,State="C",Country="Bangladesh"},
            new Employee{ID=11,FirstName="Jsish",LastName="Hehra",Age=25,State="D",Country="Australia"},
            new Employee{ID=12,FirstName="Usish",LastName="Tehra",Age=85,State="E",Country="Kenya"},
            new Employee{ID=13,FirstName="Rsish",LastName="Lehra",Age=15,State="F",Country="India"},
            new Employee{ID=14,FirstName="Isish",LastName="Eehra",Age=5,State="G",Country="Pakistan"},
             new Employee{ID=15,FirstName="Asish",LastName="Nehra",Age=25,State="A",Country="India"},
            new Employee{ID=16,FirstName="Nsish",LastName="Oehra",Age=35,State="B",Country="Sri Lanka"},
            new Employee{ID=17,FirstName="Psish",LastName="Lehra",Age=26,State="C",Country="Bangladesh"},
            new Employee{ID=18,FirstName="Jsish",LastName="Hehra",Age=25,State="D",Country="Australia"},
            new Employee{ID=19,FirstName="Usish",LastName="Tehra",Age=85,State="E",Country="Kenya"},
            new Employee{ID=20,FirstName="Rsish",LastName="Lehra",Age=15,State="F",Country="India"},
            new Employee{ID=21,FirstName="Isish",LastName="Eehra",Age=5,State="G",Country="Pakistan"},

        };
        return list;
    }
}

在我的 Cshtml 页面中

<script type="text/javascript">
jQuery(document).ready(function () {

    jQuery("#list").jqGrid({
        url: '/Home/DynamicGridData/',
        datatype: 'json',
        mtype: 'POST',
        colNames: ['ID', 'FirstName', 'LastName', 'Age', 'State', 'Country'],
        colModel: [
  { name: 'ID', index: 'ID', width: 40, align: 'left' },
  { name: 'FirstName', index: 'FirstName', width: 80, align: 'left' },
  { name: 'LastName', index: 'LastName', width: 80, align: 'left' },
  { name: 'Age', index: 'Age', width: 80, align: 'left' },
  { name: 'State', index: 'State', width: 80, align: 'left' },
  { name: 'Country', index: 'Country', width: 80, align: 'left' }],
        pager: jQuery('#pager'), 
        rowNum: 10,
        rowList: [10, 20, 30],
        sortname: 'ID, FirstName, LastName, Age, State, Country',
        sortorder: "Asc",
        viewrecords: true,
        imgpath: '/content/images',
        autowidth: true,
        width: '100%',
        height: '100%',
        multiselect: false,
        caption: "Grid example",
        loadComplete: function() {
        //jQuery("#myGridID").trigger("reloadGrid"); // Call to fix client-side sorting
    }
    });

    //jQuery("#list").jqGrid('navGrid', '#pager', { add: true, edit: true, del: true });
    jQuery("#list").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false }, {}, {}, {}, { multipleSearch: true, multipleGroup: true, showQuery: true });


}); 
</script>

我如何对所有列进行排序..我只想通过将所有字段设置为 sortable:true 来对所有字段进行排序

编辑格式<script>不正确

4

1 回答 1

1

在您的控制器中,您将需要使用 anOrderBy然后使用sidx, sord网格传递给您的控制器的值。我还在下面的示例中包括了分页,因此您的网格只能显示数据集中的一页行。

List pagedQuery =  _lstemp.AsQueryable().OrderBy(sidx + " " + sord).Skip((page - 1) * rows).Take(rows);

在您的colModel(jqGrid 设置)中,您将使用属性 par 设置要排序的每一列

sortable: false,或者sortable: true

如果您希望所有内容都可以排序,则可以使用

$.extend($.jgrid.defaults, {sortable: true});

您将能够检查您的 POST 以查看 jqGrid 告诉控制器要排序的列以及方向。

注意:OrderBy对于这种特定的语法,您将需要 System.Linq.Dynamic dll 。此 dll 可用:

于 2013-02-21T13:18:30.233 回答