获取使用模型JQuery数据表绑定的总行数

时间:2016-09-24 09:34:21

标签: c# jquery asp.net datatable dapper

如何获取结果的总行数以及数据表搜索的工作情况?现在,我只得到了结果的第一页,我已经过滤了:

OFFSET AND FETCH

它将绑定到模型,我可以得到第一页的结果:

MyModel.Count()

以下是我正在使用的代码:

DataAccess.cs:

public IEnumerable<MemberRepository> RetrieveData(MemberRepository repository)
    {
            string query = "SELECT * FROM [Member] AS m"
                + " WITH (NOLOCK) INNER JOIN [MemberStatus] AS ms WITH (NOLOCK) ON m.[Status] = ms.[StatusIdentity] INNER JOIN [MemberAccount] AS ma WITH (NOLOCK) ON m.[Account] = ma.[AccountIdentity]"
                + " WHERE m.[GroupAccount] = 'Group' ORDER BY m.[Name] ASC OFFSET (0) * 10 ROWS FETCH NEXT 10 ROWS ONLY";

            return Find<MemberRepository>(query, repository);
    }

private IEnumerable<T> Find<T>(string query, T models)
    {
        using (IDbConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();

            return conn.Query<T>(query, models);
        }
    }

我可以像下面这样访问它:

Retrieve.ashx

public int sEcho { get; set; }
            public string sSearch { get; set; }
            public int recordsTotal { get; set; }
            public int recordsFiltered { get; set; }
            public int iTotalRecords { get; set; }
            public int iTotalDisplayRecords { get; set; }
            public IList<string[]> aaData;

            public void SetResponse(int echo, string search, int records, int totalRecords, List<string[]> aaData)
            {
                this.sEcho = echo;
                this.sSearch = search;
                this.recordsTotal = records;
                this.recordsFiltered = records;
                this.iTotalRecords = totalRecords;
                this.iTotalDisplayRecords = totalRecords;
                this.aaData = aaData;
            }

public override void ProcessRequest(HttpContext context)
   { 
        base.ProcessRequest(context);

        MemberRepository models = new MemberRepository();

        DataAccess access = new DataAccess();

        IEnumerable<MemberRepository> MemberLists = access.RetrieveData(models);

        List<string[]> aaData = repository.Select(r => new[]
            {
                r.Name
            }).ToList();

        SetResponse(echo, string.Empty, models.Count(), models.Count(), aaData);
   }

Retrieve.aspx:

$("#myTable").DataTable({
            bProcessing: true,
            bServerSide: true,
            iDisplayLength: 10,
            sAjaxSource: "../Retrieve.ashx",
            fnServerData: function (sSource, aoData, fnCallback) {

                $.ajax({
                    type: "POST",
                    data: aoData,
                    url: sSource,
                    dataType: "json",
                    success: function (msg) {
                        fnCallback(msg);
                    }
                });
            }

对于总记录,我应该用什么来替换它?现在,我在切断分页后的总记录中也是如此。

你的回答非常感谢。

谢谢。

1 个答案:

答案 0 :(得分:0)

您需要从应用程序服务器向数据库服务器发出两个请求,第一个是您已经拥有的请求,第二个应该是一个查询,它返回与您的过滤器对应的记录计数,但不收集只有子集可视化。原因是您不应该将所有行加载到内存中,因为这可能是一个巨大的浪费,因此您只加载一个子集。但由于您只加载了一个子集,因此您无法加载所有元素的数量,因为您只拥有一个子集。因此,您需要一个包含数据表列的查询和另一个包含计数计数的查询。