简体   繁体   English

将分页添加到剃刀视图

[英]Adding the pagination to a razor view

I have an asp.Net Mvc4 web applcation in which i have a list as a model: 我有一个asp.Net Mvc4 Web应用程序,其中有一个列表作为模型:

<table class="table_data" border="1">
            <tr>
                @if(Model.Count > 0){
                <th>
                    @Html.Label("Fonction")
                </th>
                <th>
                    @Html.Label("Nom du client")
                </th>
                <th>
                    @Html.Label("Date de réception")
                </th>
                <th>
                    @Html.Label("Date de clôture")
                </th>
                    <th></th>
                }
            </tr>
            @foreach(Planning.Models.Paire p in Model){
                <tr>
                <td>
                        <label>@p._tag</label>
                </td>
                <td>
                    <label>@p._client</label>
                </td>
                <td>
                   <label>@p._reception</label>
                </td>
                <td>
                   <label>@p._cloture</label>

               </td>  
                    <td>
                        @{
                                List<Planning.Models.Impaire> liste = u.Get_Impaire_List();
                             Planning.Models.Impaire imp = liste.Find(x => x.id_paire == p.Id);

                            if (imp != null){
                   @Html.ActionLink("voir plus de détails", "Details", new { id_paire = p.Id })}
                        }
               </td>  
            </tr> 
            }
        </table>

The Model is List<Paire> . ModelList<Paire>

I'd like to display each 15 lines together in the same view, so how can i add the pagination between pages at the same view? 我想在同一视图中同时显示每15行,因此如何在同一视图中的页面之间添加分页? Can i use Ajax to do that? 我可以使用Ajax来做到这一点吗?

Try following: 请尝试以下操作:

$('table.table_data').each(function () {
        var currentPage = 0;
        var numPerPage = 15;
        var $table = $(this);
        $table.bind('repaginate', function () {
            $table.find('tbody tr').hide().slice(currentPage * numPerPage, (currentPage + 1) * numPerPage).show();
        });
        $table.trigger('repaginate');
        var numRows = $table.find('tbody tr').length;
        var numPages = Math.ceil(numRows / numPerPage);
        var $pager = $('<div class="pager"></div>');
        for (var page = 0; page < numPages; page++) {
            $('<span class="page-number"></span>').text(page + 1).bind('click', {
                newPage: page
            }, function (event) {
                currentPage = event.data['newPage'];
                $table.trigger('repaginate');
                $(this).addClass('active').siblings().removeClass('active');
            }).appendTo($pager).addClass('clickable');
        }

            $pager.insertAfter($table).find('span.page-number:first').addClass('active');


    });

You need to add <tbody> tag, to split body's rows and head row: 您需要添加<tbody>标记,以拆分正文的行和头行:

<table class="table_data" border="1">
            <tr>
                @if(Model.Count > 0){
                <th>
                    @Html.Label("Fonction")
                </th>
                <th>
                    @Html.Label("Nom du client")
                </th>
                <th>
                    @Html.Label("Date de réception")
                </th>
                <th>
                    @Html.Label("Date de clôture")
                </th>
                    <th></th>
                }
            </tr>
            <tbody>
            @foreach(Planning.Models.Paire p in Model){
                <tr>
                <td>
                        <label>@p._tag</label>
                </td>
                <td>
                    <label>@p._client</label>
                </td>
                <td>
                   <label>@p._reception</label>
                </td>
                <td>
                   <label>@p._cloture</label>

               </td>  
                    <td>
                </tr>
             </tbody>

And add css for pager: 并为分页器添加CSS:

div.pager {
    text-align: center;
    margin: 1em 0;
}

div.pager span {
    display: inline-block;
    width: 1.8em;
    height: 1.8em;
    line-height: 1.8;
    text-align: center;
    cursor: pointer;
    background: #216aaf;
    color: #fff;
    margin-right: 0.5em;
}

div.pager span.active {
    background: #e6f1fb;
    color:#216aaf;
}

Try This One of the easiest method 尝试此最简单的方法之一

Controller 控制者

using PagedList;
public ActionResult Index(int ? pagePos)
{
        //return View(db.Items.ToList());// Change this as following
        int pageNumber = (pagePos ?? 1);
        return View(db.Items.ToList().ToPagedList(pageNumber, 30));
        //30 equels number of items per page
}

Index.cshtml Index.cshtml

@*@model IEnumerable<ProjectDB.Models.Item>*@
@*Change These Also as following*@
@model PagedList.IPagedList<ProjectDB.Models.Item>
@using PagedList.Mvc


<table class="table">
<tr>
    <th>
        Name
    </th>
    <th>
        Code
    </th>

    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Code)
    </td>

    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
        @Html.ActionLink("Details", "Details", new { id=item.Id }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.Id })
    </td>
</tr>
}   
</table>

@*Pagination Code Starts*@

<div class="pageCount">
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
</div>
    @Html.PagedListPager(Model, pagePos => Url.Action("Index", new { pagePos }))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM