2016-09-24 49 views
0

綁定的總行數我怎樣才能得到結果的總行,並且還與數據表搜索工作?現在,我只得到了結果的第一頁,我已經與過濾:獲取與型號的JQuery數據表

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); 
        } 
       }); 
      } 

總數紀錄,我應該將其替換爲?現在我將它與截斷分頁後的總記錄相同。

您的回答非常感謝。

謝謝。

+0

您需要編寫單獨的查詢,如'select count(*)from yourtable'來獲取總數 –

+0

@Ganesh_Devlekar:嗨,是否可以使用一個查詢並獲取總行數,而沒有使用分頁總行數?如果不能,那麼如果我使用單獨的查詢做,我如何訪問沒有分頁的總行數?由於 – Reinhardt

回答

0

你需要做的,從應用服務器到數據庫服務器的兩個要求,首先是一個你已經有了和第二應該是返回對應的過濾器記錄的計數的查詢,但沒有隻收集該子集將被可視化。原因是你不應該加載所有的行到內存中,因爲這可能是一個巨大的浪費,所以你只加載一個子集。但是因爲只加載一個子集,所以不能加載所有元素的數量,因爲您只提供了一個子集。所以你需要一個帶有數據表的列的查詢和另一個帶有計數的查詢。

+0

您好,感謝的答案,但你可以請提供您的發言一個例子嗎?我對你的發言感到困惑。 – Reinhardt