jqGrid的asp.net MVC从头开始执行3 [英] jQGrid asp.net mvc 3 implementation from scratch

查看:120
本文介绍了jqGrid的asp.net MVC从头开始执行3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的jqGrid 在我的一些基本功能,包括过滤和子网格应用程序。我看了很多文章,但很多人似乎是陈旧过时。我觉得我与实施它的基础知识挣扎。我真的AP preciate如果你能提供基本的控制器视图结构或提及任何形式的从无到有教程与 ASP。净MVC 3。

I am trying to implement jQGrid in my application with some basic features including filtering and sub grid. I looked numerous articles but many of them seem to be old and outdated. I think i am struggling with the basics of implementing it. I will really appreciate if you can provide basic controller view structure or refer to any sort of tutorial from scratch with asp. net mvc 3.

推荐答案

下面是在查看code(第一加价,那么JS):

Here is the code in the View (first mark-up, then JS):

@model SampleApp.SampleModel

@{
    ViewBag.Title = "Stackoverflow Title";
    Layout = "~/Views/Shared/_defaultLayout.cshtml";

}

<link href="@Url.Content("~/Content/themes/cupertino/jquery-ui-1.8.13.custom.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/grid/ui.jqgrid.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/ui/jquery-ui-1.8.12.custom.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/grid/grid.locale-en.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/grid/jquery.jqGrid.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/myJSHelper.js")" type="text/javascript"></script>

<script type="text/javascript" id="loadGridResultsScript1">
    // This code LOADs the grid by calling the MVC Action to get Data
    $(document).ready(function () { helper.loadSearchResults('@Model.JobCode'); });
</script>

<!-- these are the jQuery Grid controls -->
<table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
<div id="pager" class="scroll" style="text-align:center;"></div>

下面是myHelper JS文件中的JS功能:

Here is the JS function from myHelper JS file:

loadSearchResults: function (id) {

        $("#list").jqGrid({
            url: vpath + '/Sample/GetTargets/' + id,
            datatype: 'json', mtype: 'POST', colNames: cols,
            colModel: colModel, pager: $('#pager'),
            rowNum: 15, rowList: [5, 10, 15, 25, 50, 100], sortname: cols[1],
            sortorder: "asc", viewrecords: true,
            imgpath: '', caption: ' '
        });
        $("#list").setGridWidth(1000, true);
        $("#list").setGridHeight(350, true);


    }

和这里是jQuery的调用操作:

and here is the Action that jQuery calls:

        public ActionResult GetTargets(string id, string sidx, string sord, int page, int rows)
    {

        var repo = IOCContainer.Resolve<DataRepository>();
        ////////////////////////////////////////////////////////////////////
        var job = svc.GetJobByCode(id);
        // job is my 'Model', it is a System.Data.DataSet

        int pageIndex = Convert.ToInt32(page) - 1;
        int pageSize = rows;

        int totalRecords = (job.Targets == null) ? 0 : job.Tables[0].Rows.Count; 
        int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
        List<DataRow> pageSet = Enumerable.Empty<DataRow>().ToList();

        if (totalRecords > 0)
        {
            // Get rows for current page!!!
            if (sord == "asc")
            {
                if (sidx == "   ") sidx = job.Tables[0].Columns[0].ColumnName;

                pageSet = job.Tables[0].Rows.Cast<DataRow>()
                        .OrderBy(q => q[sidx])
                        .Skip(pageIndex * pageSize)
                        .Take(pageSize)
                        .ToList();
            }
            else
                pageSet = job.Targets.Tables[0].Rows.Cast<DataRow>()
                        .OrderBy(q => q[sidx])
                        .Reverse()
                        .Skip(pageIndex * pageSize)
                        .Take(pageSize)
                        .ToList();
        }

        var cols = GetColumnNames(job.Tables[0]);
        // Func to get Cells, called later on in code...
        Func<DataRow, string[], string[]> getCells = (pkg, columns) =>
        {
            var cellList = new List<string>();
            cellList.Add(pkg[0].ToString());
            foreach (var col in columns)
            {
                if (col.StartsWith("_")) continue;
                object cellContent = pkg[col];
                string cellText = string.Empty;
                if (cellContent is DateTime)
                {
                    cellText = ((DateTime)cellContent).ToString("MM/dd/yyyy");
                }
                else if (cellContent is decimal || cellContent is double)
                {
                    cellText = string.Format("{0:c}", cellContent);
                }
                else
                {
                    cellText = String.Format("{0}", cellContent);
                }
                cellList.Add(cellText);
            }

            return cellList.ToArray();
        };
        var jsonData = new
        {
            total = totalPages,
            page,
            records = totalRecords,
            rows = (
                from item in pageSet
                select new
                {
                    i = item[0].ToString(),
                    cell = getCells(item, cols)
                }).ToArray()
        };
        return Json(jsonData);
    }

我希望对您有所帮助。让我知道,如果有任何疑问

I hope you find this helpful. Let me know if there are any questions

这篇关于jqGrid的asp.net MVC从头开始执行3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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