2

MVC WebGrid を作成しようとしています。グリッドを機能させることはできますが、並べ替えの問題に直面しています。私のrowsPerPageセットは 5 で、合計 7 つのレコードがあります。最初のページにいるとき、最初の 5 つのレコードが表示されます。Id 列で並べ替えると、一連のデータ全体が並べ替えられ、7 番目のレコードが一番上に表示されます。

私の質問 :

  1. 1ページ目に存在する要素のみをソートし、5番目のレコードを一番上に配置する方法。
  2. 作成するデータ行にスタイルを追加する方法は?

コードは次のようなものです: CSHTML -

@model IEnumerable<Product>

@{
    ViewBag.Title = "grid";
    WebGrid grid = new WebGrid(Model, rowsPerPage: 5 );
}

@grid.GetHtml(

    tableStyle: "table",
    fillEmptyRows: true,        
    headerStyle: "main-box-header clearfix",
    footerStyle: "pagination pull-right",

    mode: WebGridPagerModes.All, //paging to grid 
    firstText: "<< First",
    previousText: "< Prev",
    nextText: "Next >",
    lastText: "Last >>",

    columns: new[]  // colums in grid
    {
        grid.Column("Id"), //the model fields to display
        grid.Column("Name"  ),
        grid.Column("Description"),
        grid.Column("Quantity"),

   })

コントローラー -

public ActionResult WebgridSample()
        {
            List<Product> inventoryList = new List<Product>();  

            inventoryList.Add(new Product
            {
                Id = "P101",
                Name = "Computer",
                Description = "All type of computers",
                Quantity = 800
            });
            inventoryList.Add(new Product
            {
                Id = "P102",
                Name = "Laptop",
                Description = "All models of Laptops",
                Quantity = 500
            });
            inventoryList.Add(new Product
            {
                Id = "P103",
                Name = "Camera",
                Description = "Hd  cameras",
                Quantity = 300
            });
            inventoryList.Add(new Product
            {
                Id = "P104",
                Name = "Mobile",
                Description = "All Smartphones",
                Quantity = 450
            });
            inventoryList.Add(new Product
            {
                Id = "P105",
                Name = "Notepad",
                Description = "All branded of notepads",
                Quantity = 670
            });
            inventoryList.Add(new Product
            {
                Id = "P106",
                Name = "Harddisk",
                Description = "All type of Harddisk",
                Quantity = 1200
            });
            inventoryList.Add(new Product
            {
                Id = "P107",
                Name = "PenDrive",
                Description = "All type of Pendrive",
                Quantity = 370
            });

            return View(inventoryList);

        }  
4

1 に答える 1

0

多分それは実際のトピックではないかもしれませんが、私は答えますこれを達成するための最も効果的な方法は、サーバー側のソートを行うことです:

1) コントローラには 4 つの新しいパラメータがあります

public ActionResult WebgridSample(int pageNum = 1, int pageSize = 5, string sortColumnName = "",   string sortOrder = "desc")

2) SQL SELECT リクエストを作成した後、エントリを取得する必要がある位置を指定し、並べ替えを行うことができます (sortOrder = "" を使用してリストを「そのまま」取得します)。

3) Bind() メソッドを autoSortAndPaging パラメーターを無効にして実行するようにします。合計エントリを指定する必要があります (コンストラクターの前に、rowsPerPage を認識させる必要があります)。

4) 必要なデータを webgrid にロードした後、ページ番号を指定することを忘れないでください

おめでとう :)

于 2015-10-19T00:02:25.177 に答える