Asp.net MVC 中 JqGrid 使用

本地查询(Local):

@using Newtonsoft.Json
@using System.Net.Http

@{
    var result = from u in 查询语句略
                 select new
                 {
                     user_id = u.user_id,
                     user_email = u.user_email,
                     user_nicename = u.user_nicename,
                     user_status = u.user_status == 1 ? "已启用" : "未启用",
                     meta_key = m.meta_key,
                     meta_value = m.meta_value,
                     user_registered = u.user_registered,
                     option_description = o.option_description
                 };

    string strJsonRslt = JsonConvert.SerializeObject(result, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
}

<script type="text/javascript">
    $("#gridTable").jqGrid({
            datatype: "local",
            width: '100%',
            height: '100%',
            colNames: ['编号', '用户名', '昵称', '状态', '权限', '出生日期'],
            colModel: [
                    { name: 'user_id', index: 'user_id', width: 60, sorttype: "int", editable: false },
                    { name: 'user_email', index: 'user_email', width: 180, editable: false },
                    { name: 'user_nicename', index: 'user_nicename', width: 80, editable: false },
                    { name: 'user_status', index: 'user_status', width: 60, sorttype: "string", editable: true, edittype: "select", editoptions: { value: "1:已启用;2:未启用" } },
                    { name: 'option_description', index: 'option_description', width: 100, editable: true, edittype: "select", editrules: { required: true }, editoptions: { value: '@strOptions' } },
                    { name: 'user_registered', index: 'user_registered', width: 180, sorttype: "date", editable: false }
            ],
            multiselect: true,
            multiselectWidth: "30px",
            sortname: 'user_id',
            sortorder: 'asc',
            viewrecords: true,
            rowNum: 10,
            rowList: [10, 20, 30],
            pager: "#gridPager",
            caption: "用户信息列表"
        });
        
        var mydata = eval(@MvcHtmlString.Create(strJsonRslt));

        if (typeof (mydata) != "undefined") {
            for (var i = 0; i <= mydata.length; i++)
                jQuery("#gridTable").jqGrid('addRowData', i + 1, mydata[i]);
        }

        jQuery("#gridTable").trigger("reloadGrid");
</script>

<table id="gridTable"></table>
<div id="gridPager"></div>

联网查询(View):

<script type="text/javascript">
$("#gridTable").jqGrid({
        url: "/DSource/Load",
        mtype:"post",
        datatype: "json",
        width: '100%',
        height: '100%',
        colNames: ['编号', '报警', '名称', '监控项', '状态', '添加时间', '描述'],
        colModel: [
                { name: 'dsource_id', index: 'dsource_id', width: 30, sorttype: "int", editable: false },
                { name: 'dsource_alarm', index: 'dsource_alarm', width: 30, align: "center", sortable: false, editable: false, formatter: alarmFormatter },
                { name: 'dsource_name', index: 'dsource_name', width: 150, editable: false },
                { name: 'dsource_item', index: 'dsource_item', width: 70, editable: false },
                { name: 'dsource_status', index: 'dsource_status', width: 60, editable: false },
                { name: 'dsource_updatetime', index: 'dsource_updatetime', width: 150, sorttype: "date", editable: false },
                { name: 'dsource_description', index: 'dsource_description', width: 250, sorttype: "string", editable: false },
        ],
        ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
        toolbar:[true,"top"],
        multiselect: true,
        multiselectWidth: "30px",
        sortname: 'dsource_id',
        sortorder: 'asc',
        viewrecords: true,
        rowNum: 10,
        rowList: [10,20,30],
        pager: "#gridPager",
        caption: "信息元列表",
    });
</script>

<table id="gridTable"></table>
<div id="gridPager"></div>

联网查询(Controller):

public JsonResult Load(bool? _search, string nd, int page, int rows, string sidx, string sord, string searchField, string searchString, string searchOper)
{
    var query = (from d in 查询语句略
                 select new
                 {
                     d.dsource_id,
                     dsource_alarm = new DSourceLogService().GetDSourceAlarmCnt(d.dsource_id, "", newEntity).ToString(),
                     d.dsource_name,
                     dsource_item = p.prop_description,
                     d.dsource_description,
                     dsource_status = d.dsource_status == "1" ? "启用" : "禁用",
                     d.dsource_expiredday,
                     dsource_alarmhigh = (d.dsource_alarmhigh == -65535 || d.dsource_alarmhigh == 65535) ? "未禁用" : d.dsource_alarmhigh.ToString(),
                     dsource_alarmlow = (d.dsource_alarmlow == -65535 || d.dsource_alarmhigh == 65535) ? "未禁用" : d.dsource_alarmhigh.ToString(),
                     dsource_alarmdeltadata = (d.dsource_alarmdeltadata == -65535 || d.dsource_alarmhigh == 65535) ? "未禁用" : d.dsource_alarmhigh.ToString(),
                     dsource_alarmidle = (d.dsource_alarmidle == -65535 || d.dsource_alarmhigh == 65535) ? "未禁用" : d.dsource_alarmhigh.ToString(),
                     d.dsource_formula,
                     dsource_updatetime = d.dsource_updatetime.ToString("yyyy-MM-dd HH:mm:ss")
                 }).AsQueryable();

    page = page <= query.Count() / rows + 1 ? page : 1;

    var filter = query.OrderUsingSortExpression(string.Format("{0} {1}", sidx, sord)).Skip(rows * (page - 1)).Take(rows);

    return this.Json(new { rows = filter, page = page, total = query.Count() / rows + 1, records = query.Count() }, JsonRequestBehavior.AllowGet);
}

 

posted on 2013-04-17 12:19  一路前行  阅读(5373)  评论(1编辑  收藏  举报