0

我正在将数据显示到 jqgrid 中,但我的 jqgrid 在第一页中仅显示 100 条记录,当我单击下一页按钮图标时,它再次获取相同的记录。而 jqgrid 正在正确计算总页数和总记录。 ..下面是我的代码...

索引.aspx

<script type="text/javascript">
    $(function () {
        $("#UsersGrid").jqGrid({
            url: 'ModCust.ashx',
            datatype: 'json',
            height: 500,
            colNames: ['Duration','username', 'ordinal', 'authcode'],
            colModel: [
                    { name: 'duration', index: 'duration', width: 100, sortable: true,hidden:true },
                    { name: 'username', width: 100, sortable: true },
                    { name: 'ordinal', width: 100, sortable: true },
                    { name: 'authcode', width: 100, sortable: true },

                ],

            cmTemplate: { title: false },
            rowNum: 100,
            rowList: [100, 200, 300],
            pager: '#UsersGridPager',
            sortname: 'duration',
            viewrecords: true,
            autowidth:true,
            sortorder: 'asc'
          });

        $("#UsersGrid").jqGrid('navGrid', '#UsersGridPager', { edit: false, add: false, del: false });
    });

</script>

这是我的处理程序代码..

ModCust.ashx

public struct JQGridResults
{
    public int page;
    public int total;
    public int records;
    public JQGridRow[] rows;
}
public struct JQGridRow
{
    public string duration;
    public string[] cell;
}
[Serializable]
public class User
{
    public string duration { get; set; }
    public string username { get; set; }
    public string ordinal { get; set; }
    public string authcode { get; set; }
}

public class ModCust : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        HttpRequest request = context.Request;
        HttpResponse response = context.Response;

        string _search = request["_search"];
        string numberOfRows = request["rows"];
        string pageIndex = request["page"];
        string sortColumnName = request["sidx"];
        string sortOrderBy = request["sord"];


        int totalRecords;
        Collection<User> users = GetUsers(numberOfRows, pageIndex, sortColumnName, sortOrderBy, out totalRecords);
        string output = BuildJQGridResults(users, Convert.ToInt32(numberOfRows), Convert.ToInt32(pageIndex), Convert.ToInt32(totalRecords));
        response.Write(output);
    }
    private string BuildJQGridResults(Collection<User> users, int numberOfRows, int pageIndex, int totalRecords)
    {

        JQGridResults result = new JQGridResults();
        List<JQGridRow> rows = new List<JQGridRow>();
        foreach (User user in users)
        {
            JQGridRow row = new JQGridRow();
            row.duration = user.duration;
            row.cell = new string[4];
            row.cell[0] = user.duration;
            row.cell[1] = user.username;
            row.cell[2] = user.ordinal;
            row.cell[3] = user.authcode;
            rows.Add(row);
        }
        result.rows = rows.ToArray();
        result.page = pageIndex;
        result.total = (totalRecords + numberOfRows - 1) / numberOfRows;
        result.records = totalRecords;
        JavaScriptSerializer serializer = new JavaScriptSerializer() { MaxJsonLength = Int32.MaxValue, RecursionLimit = 100 };
       // return new JavaScriptSerializer().Serialize(result);
        return serializer.Serialize(result);
    }


    private Collection<User> GetUsers(string numberOfRows, string pageIndex, string sortColumnName, string sortOrderBy, out int totalRecords)
    {
        Collection<User> users = new Collection<User>();
        string connectionString = "Server=localhost;Port=3306;Database=projecttt;UID=root;Pwd=techsoft;pooling=false";

        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {
            using (MySqlCommand command = new MySqlCommand())
            {
                command.Connection = connection;
                command.CommandText = "select duration,username,ordinal,authcode from processeddata_table ";
                command.CommandType = CommandType.Text; // StoredProcedure;

                MySqlParameter paramPageIndex = new MySqlParameter("@PageIndex", MySqlDbType.Int32);
                paramPageIndex.Value = Convert.ToInt32(pageIndex);
                command.Parameters.Add(paramPageIndex);

                MySqlParameter paramColumnName = new MySqlParameter("@SortColumnName", MySqlDbType.VarChar, 50);
                paramColumnName.Value = sortColumnName;
                command.Parameters.Add(paramColumnName);

                MySqlParameter paramSortorderBy = new MySqlParameter("@SortOrderBy", MySqlDbType.VarChar, 4);
                paramSortorderBy.Value = sortOrderBy;
                command.Parameters.Add(paramSortorderBy);

                MySqlParameter paramNumberOfRows = new MySqlParameter("@NumberOfRows", MySqlDbType.Int32);
                paramNumberOfRows.Value = Convert.ToInt32(numberOfRows);
                command.Parameters.Add(paramNumberOfRows);

                MySqlParameter paramTotalRecords = new MySqlParameter("@TotalRecords", MySqlDbType.Int32);
                totalRecords = 0;
                paramTotalRecords.Value = totalRecords;
                paramTotalRecords.Direction = ParameterDirection.Output;
                command.Parameters.Add(paramTotalRecords);


                connection.Open();

                using (MySqlDataReader dataReader = command.ExecuteReader())
                {
                    User user;
                    while (dataReader.Read())
                    {
                        user = new User();
                        user.duration = Convert.ToString(dataReader["duration"]);
                        user.username = Convert.ToString(dataReader["username"]);
                        user.ordinal = Convert.ToString(dataReader["ordinal"]);
                        user.authcode = Convert.ToString(dataReader["authcode"]);
                        users.Add(user);
                    }
                }
                totalRecords = users.Count;

            }

            return users;
        }

    }



    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

请先生帮助我。提前谢谢。

4

2 回答 2

1

由于您在 grid 中将数据类型定义为 json 。您应该将 json reader 放入您的代码中。把它贴在你的网格下面。

jsonReader: {
                    repeatitems: false,
                    root: "rows",
                    page: "page",
                    total: "total",
                    records: "records"
                },
于 2013-12-23T05:31:16.360 回答
0

我认为问题出在您的totalRecords = users.Count;,看起来像users.count根据您的搜索条件返回列表中的总记录而不是数据库中的总记录。您必须进行单独的查询以仅使用搜索条件获取总记录,以便获得总记录。

更新 :

尝试pageIndex = (pageIndex*numberOfRows)-numberOfRowsgetUsers 方法

于 2013-02-04T06:22:50.743 回答