在MVC与jqGrid的滚动时自动加载数据 [英] Autoloading data when scrolling with jqGrid in MVC

查看:159
本文介绍了在MVC与jqGrid的滚动时自动加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是在试图实现自动加载功能的使用,我不能让它做GET请求我的动作在所有试图加载下一组数据。 第一个初始加载正常工作

我试着以下这里 http://trirand.com/blog/jqgrid/jqgrid的例子。 HTML

该功能项下所列在3.4版本制作新的左侧 - 滚动时自动加载

什么是错在这里?

下面是我的js格code

 <脚本类型=文/ JavaScript的'>
$(文件)。就绪(函数()
{
   $('#gvEmps)。jqGrid的(
   {
      网址:'RecordEmpGrid',
      数据类型:JSON,
      colNames:选择培训(S)]
      colModel:
      [
         {名称:'选择培训(S),索引:TrainingName',宽度:'300',对齐:'左'}
      ]
   的rowNum:15,
   滚动:真实,
   rowList:[10,20,30]
   呼叫器:#gvEmpsPager',
   sortname:TrainingName',
   viewrecords:真实,
   排序顺序:递减,
   jsonReader:
   {
      repeatitems:真实,
   },
   标题:''
});
});
< / SCRIPT>

HTML

 <表ID =gvEmps级=SGrid滚动>< /表>
< D​​IV ID =gvEmpsPager级=滚动>< / DIV>

控制器

  //永远不会滚动时,才第一次在页​​面加载打到这个动作
    [HTTPGET]
    公众的ActionResult RecordEmpGrid(字符串SIDX,串SORD,诠释页,诠释行)
    {
        变种克=新GridResult(SIDX,SORD,页,行);
        VAR跳跃=行*页面 - 行;
        DataTable的DT = NULL;
        使用(VAR MR =新MultipleRecords(Product.UtilityevaluateSql))
        {
            先生[SQL] =SELECT * FROM TT.Training WHERE CompanyId = 190 ORDER BY TrainingName ASC;
            DT = mr.GetDataTable();
        }
        gr.Total = dt.Rows.Count;
        VAR记录=新MultipleRecords(DT);
        的foreach(在records.Skip(跳过)。取(排VAR行))
        {
            gr.AddDataRow(新[] {行[TrainingTypeID]字符串,行[TrainingName]字符串。});
        }
        返回JSON(gr.Data,JsonRequestBehavior.AllowGet);
    }

我的JSON包装对象

 公共类GridResult
{
    私人列表<对象> m_rowData =新的List<对象>();    私人字符串m_order;
    私人字符串m_idx;
    私人诠释m_rows;
    私人诠释m_page;    公众诠释共{搞定;组; }    公共GridResult()
    {    }    公共GridResult(字符串SIDX,串SORD,诠释页,诠释行)
    {
        m_idx = SIDX;
        m_order = SORD;
        m_page =页;
        m_rows =行;
    }    公共无效AddDataRow(对象[] columnData)
    {
        m_rowData.Add(columnData);
    }    私有对象M_DATA;
    公共对象数据
    {
        得到
        {
            返回M_DATA? (M_DATA = BuildData());
        }
    }    保护对象BuildData()
    {
        变种的id = 1;
        返回新
        {
            总=总,
            页= m_page ++,
            记录= m_rows,
            行数=(从行m_rowData
                    新选择
                    {
                        ID = ID ++,
                        电池=行
                    }
                    ).ToArray()
        };
    }
}


解决方案

这个问题是我的JSON助手对象中。我怪可怜的命名约定这一点,但似乎我的记录和放混合;总体参数了。

  =总//页面记录数
的记录的记录= //总数可以装载
保护对象BuildData()
{
    变种的id = 1;
    返回新
    {
        总= _rows,//< - 行数总计为页数限制(15在我的情况)
        页= m_page ++,
        记录=总,//< - 可能的记录总数加载(121在我的情况)
        行数=(从行m_rowData
                新选择
                {
                    ID = ID ++,
                    电池=行
                }
                ).ToArray()
    };
}

切换这两个问题解决了。

下面是使我发现有很大的联系。这家伙是个jqGrid的大师。

http://blogs.teamb.com/craigstuntz/2009/04 /三万八千二百一十二分之十五/

My problem is that while trying to implement usage of the Autoload feature, I cannot get it to make GET requests to my Action at all to attempt to load the next set of data. The first initial load works fine

I tried following the example here http://trirand.com/blog/jqgrid/jqgrid.html

The feature is listed under the item on the left hand side titled New in Version 3.4 - Autoloading when scrolling

What is the mistake here?

Here is my js grid code

<script type='text/javascript'>
$(document).ready(function()
{
   $('#gvEmps').jqGrid(
   {
      url:'RecordEmpGrid',
      datatype: 'json',
      colNames:['Select Training(s)'],
      colModel :
      [
         {name: 'Select Training(s)', index: 'TrainingName', width: '300', align: 'Left'}
      ],
   rowNum: 15,
   scroll: true,
   rowList:[10,20,30],
   pager: '#gvEmpsPager',
   sortname: 'TrainingName',
   viewrecords: true,
   sortorder: 'desc',
   jsonReader:
   {
      repeatitems : true,
   },
   caption: ''
});
});
</script>

Html

<table id="gvEmps" class="SGrid scroll"></table>
<div id="gvEmpsPager" class="scroll"></div>

Controller

    //will never hit this action when scrolling, only first time on page load
    [HttpGet]
    public ActionResult RecordEmpGrid(string sidx, string sord, int page, int rows)
    {
        var gr = new GridResult(sidx, sord, page, rows);
        var skip = rows * page - rows;
        DataTable dt = null;
        using (var mr = new MultipleRecords(Product.Utility, "evaluateSql"))
        {
            mr["sql"] = "SELECT * FROM TT.Training WHERE CompanyId = 190 ORDER BY TrainingName ASC";
            dt = mr.GetDataTable();
        }
        gr.Total = dt.Rows.Count;
        var records = new MultipleRecords(dt);
        foreach (var row in records.Skip(skip).Take(rows))
        {
            gr.AddDataRow(new []{row["TrainingTypeID"].String, row["TrainingName"].String });
        }
        return Json(gr.Data, JsonRequestBehavior.AllowGet);
    }

My json wrapper object

public class GridResult
{
    private List<object> m_rowData = new List<object>();

    private string m_order;
    private string m_idx;
    private int m_rows;
    private int m_page;

    public int Total { get; set; }

    public GridResult()
    {

    }

    public GridResult(string sidx, string sord, int page, int rows)
    {
        m_idx = sidx;
        m_order = sord;
        m_page = page;
        m_rows = rows;
    }

    public void AddDataRow(object[] columnData)
    {
        m_rowData.Add(columnData);
    }

    private object m_data;
    public object Data
    {
        get
        {
            return m_data ?? (m_data = BuildData());
        }
    }

    protected object BuildData()
    {
        var id = 1;
        return new
        {
            total = Total,
            page = m_page++,
            records = m_rows,
            rows = (from row in m_rowData
                    select new
                    {
                        id = id++,
                        cell = row
                    }
                    ).ToArray()
        };
    }
}

解决方案

The issue was inside my json helper object. I blame poor naming conventions for this but it appears that I was mixing the records & total parameters up.

total = //Number of records of the page
records = //Total number of records that can be loaded


protected object BuildData()
{
    var id = 1;
    return new
    {
        total = _rows, // <-- Number of rows total for a page limit (15 in my case)
        page = m_page++,
        records = Total, //<-- Total number of records possible to load (121 in my case)
        rows = (from row in m_rowData
                select new
                {
                    id = id++,
                    cell = row
                }
                ).ToArray()
    };
}

switching those two solved the problem.

Here is a great link that lead me to the discovery. This guy is a jqGrid guru.

http://blogs.teamb.com/craigstuntz/2009/04/15/38212/

这篇关于在MVC与jqGrid的滚动时自动加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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