【问题标题】:Pagination not display when use Jquery Pagination although data display?使用 Jquery 分页时虽然显示数据但不显示分页?
【发布时间】:2022-12-11 08:28:32
【问题描述】:

我在 asp.net core 5 上工作。我面临问题

显示所有员工数据时不显示分页。

所有员工的展示都没有任何问题。

我的分页问题不显示。 检查浏览器时显示此错误

Uncaught TypeError: $(...).Pagination is not a function
    at HTMLDocument.<anonymous> (site.js:4:26)
    at e (jquery.min.js:2:30005)
    at t (jquery.min.js:2:30307)

我通过 Controller Employee action Index 显示数据 employee

我的行动观指数

   @model TestEmployee.ViewModels.EmployeePageViewModel

@{
    ViewData["Title"] = "Index";
}

<h1>Index</h1>


<link href="~/lib/simplePagination/simplePagination.css" rel="stylesheet" />
<style>
    body {
        font-family: 'Roboto', sans-serif;
        font-size: 14px;
        line-height: 18px;
        background: #f4f4f4;
    }

    .list-wrapper {
        padding: 15px;
        overflow: hidden;
    }

    .list-item {
        border: 1px solid #EEE;
        background: #FFF;
        margin-bottom: 10px;
        padding: 10px;
        box-shadow: 0px 0px 10px 0px #EEE;
    }

        .list-item h4 {
            color: #FF7182;
            font-size: 18px;
            margin: 0 0 5px;
        }

        .list-item p {
            margin: 0;
        }

    .simple-pagination ul {
        margin: 0 0 20px;
        padding: 0;
        list-style: none;
        text-align: center;
    }

    .simple-pagination li {
        display: inline-block;
        margin-right: 5px;
    }

        .simple-pagination li a,
        .simple-pagination li span {
            color: #666;
            padding: 5px 10px;
            text-decoration: none;
            border: 1px solid #EEE;
            background-color: #FFF;
            box-shadow: 0px 0px 10px 0px #EEE;
        }

    .simple-pagination .current {
        color: #FFF;
        background-color: #FF7182;
        border-color: #FF7182;
    }

    .simple-pagination .prev.current,
    .simple-pagination .next.current {
        background: #e04e60;
    }
</style>

<table class="table">
 
        <tr>
            <th>
              
                EmployeeName
            </th>
            <th>

                EmployeeAdress
            </th>
            <th>
    
                Logo
            </th>
            <th></th>
        </tr>
 

        @foreach (var emp in Model.employees)
        {
   
            <tr>
                <td>@emp.EmployeeName</td>
                <td>@emp.EmployeeAdress</td>
                <td>@emp.Logo</td>
            </tr>
}
 
</table>
<ul id="emp-pagination" class="pagination"></ul>

<input asp-for="Pager.NumberOfPages" type="hidden" id="hdnTotalNumberOfPages" value="@Model.Pager.NumberOfPages" />
<input asp-for="Pager.CurrentPage" type="hidden" id="hdnCurrentPage" value="@Model.Pager.CurrentPage" />
@section Scripts{
    <script>
        $(document).ready(function () {
            console.log("success")
            var items = $(".list-wrapper .list-item");
            var numItems = items.length;
            var perPage = 5;

            items.slice(perPage).hide();
            $('#emp-pagination').Pagination({

                pages: $('#hdnTotalNumberOfPages').val(),
                currentPage: $('#hdnCurrentPage').val(),
                itemsOnPage: 5,
                cssStyle: 'light-theme',
                onPageClick: function (pageNo) {
                    var url = "/Employee/Index?pageNumber=" + pageNo;
                    window.location.href = url;
                },
                hrefTextSuffix: '',
                selectOnClick: true
            })
        })
    </script>
    }

控制器 员工行动指数

 public ActionResult Index(int pageNumber = 1)
        {
            var allemployee = _employee.GetAllEmployee();
            var result = _pageHelper.GetPage(allemployee.AsQueryable(), pageNumber);

            var employeesData = new EmployeePageViewModel
            {
                employees = result.Items,
                Pager = result.Pager
            };

            return View(employeesData);
        }

更新后的帖子

console log browser errors
Failed to load resource: the server responded with a status of 404 ()
index:43 success
jquery.min.js:2 jQuery.Deferred exception: $(...).Pagination is not a function TypeError: $(...).Pagination is not a function
    at HTMLDocument.<anonymous> (https://localhost:44377/Employee/index:44:30)
    at e (https://localhost:44377/lib/jquery/dist/jquery.min.js:2:30005)
    at t (https://localhost:44377/lib/jquery/dist/jquery.min.js:2:30307) undefined
S.Deferred.exceptionHook @ jquery.min.js:2
jquery.min.js:2 Uncaught TypeError: $(...).Pagination is not a function
    at HTMLDocument.<anonymous> (index:44:30)
    at e (jquery.min.js:2:30005)
    at t (jquery.min.js:2:30307)
simplePagination.css:1

我的布局页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - TestEmployee</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
  
</head>



<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">TestEmployee</a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2022 - TestEmployee - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
        </div>
    </footer>
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <link href="~/lib/simplePagination/simplePagination.css" rel="stylesheet" />
        <script src="https://cdn.tutorialjinni.com/simplePagination.js/1.6/jquery.simplePagination.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>
    @section Scripts{
        <script>
            $(document).ready(function () {
                console.log("success")
                $('#emp-pagination').Pagination({

                    pages: $('#hdnTotalNumberOfPages').val(),
                    currentPage: $('#hdnCurrentPage').val(),
                    itemsOnPage: 5,
                    cssStyle: 'light-theme',
                    onPageClick: function (pageNo) {
                        var url = "/Employee/Index?pageNumber=" + pageNo;
                        window.location.href = url;
                    },
                    hrefTextSuffix: '',
                    selectOnClick: true
                })
            })
        </script>
    }
    @await RenderSectionAsync("Scripts", required: false)
</body>
</html>

【问题讨论】:

  • 如果您看到 Pagination is not a function,则表明没有为您的分页加载项正确加载库。
  • 所以我该怎么做才能加载它
  • 在动作控制器数据显示和分页上但在 jquery 上我不知道会发生什么
  • 检查你的控制台。我看到 ~/lib/simplePagination.js/jquery.simplePagination.js 是你的 src,确保该文件在网络服务器上是可读的。
  • 如何从控制台读取它

标签: jquery asp.net-core jquery-ui model-view-controller asp.net-core-mvc


【解决方案1】:

对于.net 5 MVC 项目,如果您通过模板创建项目,项目中已经包含了jquery,您可以在Views.Shared/_Layout.cshtml 找到它

我们还可以将 &lt;script src="~/lib/simplePagination.js/jquery.simplePagination.js"&gt;&lt;/script&gt; 放在 Jquery 引用的后面。请尝试将引用放入 _layout.cshtml 并测试错误消息是否仍然存在。如果仍然存在,请用 @section Scripts{ &lt;script&gt;&lt;/script&gt;} 包围您的 JS 代码

以上都是基于您在浏览器中发现的错误信息。而且我认为您也可以使用 CDN 而不是 ~/lib/simplePagination.js/xxx 来确保可以访问 JS 文件。

========================================

我在这里找到了一个online test sample。我将 html 内容和 js 复制到我的 asp.net core MVC 应用程序中,我创建了一个新的 cshtml 文件,并将 html 内容粘贴到此处。然后我添加js代码并用@section包围它们。对了,我在layout.cshtml中添加了CDN。除了 css,这一切在我这边都运作良好。

================================================ ======== 我的布局:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - WebAppMvcJwt</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
    <link rel="stylesheet" href="~/WebAppMvcJwt.styles.css" asp-append-version="true" />
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container-fluid">
                <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">WebAppMvcJwt</a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2022 - WebAppMvcJwt - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
        </div>
    </footer>
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="https://cdn.tutorialjinni.com/simplePagination.js/1.6/jquery.simplePagination.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    @*<script src="https://cdnjs.cloudflare.com/ajax/libs/simplePagination.js/1.4/jquery.simplePagination.min.js"></script>*@
    <script src="~/js/site.js" asp-append-version="true"></script>
    @await RenderSectionAsync("Scripts", required: false)
</body>
</html>

我的 cshtml:

@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}

<div class="list-wrapper">
    <div class="list-item">
        <h4>Iron Man</h4>
        <p>Iron Man is a 2008 American superhero film based on the Marvel Comics character of the same name, produced by Marvel Studios and distributed by Paramount Pictures. It is the first film in the Marvel Cinematic Universe (MCU). The film was directed by Jon Favreau, with a screenplay by the writing teams of Mark Fergus ...</p>
    </div>
    <div class="list-item">
        <h4>The Incredible Hulk</h4>
        <p>The Incredible Hulk is a 2008 American superhero film based on the Marvel Comics character the Hulk, produced by Marvel Studios and distributed by Universal Pictures. It is the second film in the Marvel Cinematic Universe (MCU). The film was directed by Louis Leterrier, with a screenplay by Zak Penn. It stars Edward ...</p>
    </div>
    <div class="list-item">
        <h4>Iron Man 2</h4>
        <p>Iron Man 2 is a 2010 American superhero film based on the Marvel Comics character Iron Man, produced by Marvel Studios and distributed by Paramount Pictures. It is the sequel to 2008's Iron Man, and is the third film in the Marvel Cinematic Universe (MCU). Directed by Jon Favreau and written by Justin Theroux, the film ...</p>
    </div>
    <div class="list-item">
        <h4>Thor</h4>
        <p>Thor is a 2011 American superhero film based on the Marvel Comics character of the same name, produced by Marvel Studios and distributed by Paramount Pictures. It is the fourth film in the Marvel Cinematic Universe (MCU). The film was directed by Kenneth Branagh, written by the writing team of Ashley Edward Miller ...</p>
    </div>
    <div class="list-item">
        <h4>Captain America: The First Avenger</h4>
        <p>Captain America: The First Avenger is a 2011 American superhero film based on the Marvel Comics character Captain America, produced by Marvel Studios and distributed by Paramount Pictures. It is the fifth film in the Marvel Cinematic Universe (MCU). The film was directed by Joe Johnston, written by the writing team of ...</p>
    </div>
    <div class="list-item">
        <h4>The Avengers</h4>
        <p>Marvel's The Avengers or simply The Avengers, is a 2012 American superhero film based on the Marvel Comics superhero team of the same name, produced by Marvel Studios and distributed by Walt Disney Studios Motion Pictures. It is the sixth film in the Marvel Cinematic Universe (MCU). The film was written and ...</p>
    </div>
    <div class="list-item">
        <h4>Iron Man 3</h4>
        <p>Iron Man 3 is a 2013 American superhero film based on the Marvel Comics character Iron Man, produced by Marvel Studios and distributed by Walt Disney Studios Motion Pictures. It is the sequel to 2008's Iron Man and 2010's Iron Man 2, and the seventh film in the Marvel Cinematic Universe (MCU). The film was directed ...</p>
    </div>
    <div class="list-item">
        <h4>Thor: The Dark World</h4>
        <p>Thor: The Dark World is a 2013 American superhero film based on the Marvel Comics character Thor, produced by Marvel Studios and distributed by Walt Disney Studios Motion Pictures. It is the sequel to 2011's Thor and the eighth film in the Marvel Cinematic Universe (MCU). The film was directed by Alan Taylor, with a ...</p>
    </div>
    <div class="list-item">
        <h4>Captain America: The Winter Soldier</h4>
        <p>Captain America: The Winter Soldier is a 2014 American superhero film based on the Marvel Comics character Captain America, produced by Marvel Studios and distributed by Walt Disney Studios Motion Pictures. It is the sequel to 2011's Captain America: The First Avenger and the ninth film in the Marvel Cinematic ...</p>
    </div>
    <div class="list-item">
        <h4>Guardians of the Galaxy</h4>
        <p>Guardians of the Galaxy is a 2014 American superhero film based on the Marvel Comics superhero team of the same name, produced by Marvel Studios and distributed by Walt Disney Studios Motion Pictures. It is the tenth film in the Marvel Cinematic Universe (MCU). The film was directed by James Gunn, who wrote the ...</p>
    </div>
    <div class="list-item">
        <h4>Avengers: Age of Ultron</h4>
        <p>Avengers: Age of Ultron is a 2015 American superhero film based on the Marvel Comics superhero team the Avengers, produced by Marvel Studios and distributed by Walt Disney Studios Motion Pictures. It is the sequel to 2012's The Avengers and the eleventh film in the Marvel Cinematic Universe (MCU). The film was ...</p>
    </div>
    <div class="list-item">
        <h4>Ant-Man</h4>
        <p>Ant-Man is a 2015 American superhero film based on the Marvel Comics characters of the same name: Scott Lang and Hank Pym. Produced by Marvel Studios and distributed by Walt Disney Studios Motion Pictures, it is the twelfth film in the Marvel Cinematic Universe (MCU). The film was directed by Peyton Reed, with a ...</p>
    </div>
    <div class="list-item">
        <h4>Captain America: Civil War</h4>
        <p>Captain America: Civil War is a 2016 American superhero film based on the Marvel Comics character Captain America, produced by Marvel Studios and distributed by Walt Disney Studios Motion Pictures. It is the thirteenth film in the Marvel Cinematic Universe (MCU), and the sequel to 2011's Captain America: The First ...</p>
    </div>
    <div class="list-item">
        <h4>Doctor Strange</h4>
        <p>Doctor Strange is a 2016 American superhero film based on the Marvel Comics character of the same name, produced by Marvel Studios and distributed by Walt Disney Studios Motion Pictures. It is the fourteenth film in the Marvel Cinematic Universe (MCU). The film was directed by Scott Derrickson, who wrote it with Jon ...</p>
    </div>
    <div class="list-item">
        <h4>Spider-Man: Homecoming</h4>
        <p>Spider-Man: Homecoming is a 2017 American superhero film based on the Marvel Comics character Spider-Man, co-produced by Columbia Pictures and Marvel Studios, and distributed by Sony Pictures Releasing. It is the second Spider-Man film reboot and the sixteenth film in the Marvel Cinematic Universe (MCU).</p>
    </div>
    <div class="list-item">
        <h4>Guardians of the Galaxy Vol. 2</h4>
        <p>Guardians of the Galaxy Vol. 2 is a 2017 American superhero film based on the Marvel Comics superhero team Guardians of the Galaxy, produced by Marvel Studios and distributed by Walt Disney Studios Motion Pictures. It is the sequel to 2014's Guardians of the Galaxy and the fifteenth film in the Marvel Cinematic ...</p>
    </div>
    <div class="list-item">
        <h4>Thor: Ragnarok</h4>
        <p>Thor: Ragnarok is a 2017 American superhero film based on the Marvel Comics character Thor, produced by Marvel Studios and distributed by Walt Disney Studios Motion Pictures. It is the sequel to 2011's Thor and 2013's Thor: The Dark World, and is the seventeenth film in the Marvel Cinematic Universe (MCU). The film ...</p>
    </div>
    <div class="list-item">
        <h4>Black Panther</h4>
        <p>Black Panther is a 2018 American superhero film based on the Marvel Comics character of the same name. Produced by Marvel Studios and distributed by Walt Disney Studios Motion Pictures, it is the eighteenth film in the Marvel Cinematic Universe (MCU). The film is directed by Ryan Coogler, who co-wrote the ...</p>
    </div>
    <div class="list-item">
        <h4>Avengers: Infinity War</h4>
        <p>Avengers: Infinity War is a 2018 American superhero film based on the Marvel Comics superhero team the Avengers, produced by Marvel Studios and distributed by Walt Disney Studios Motion Pictures. It is the nineteenth film in the Marvel Cinematic Universe (MCU). It is the sequel to 2012's The Avengers and 2015's ...</p>
    </div>
</div>

@*<div id="pagination-container"></div>*@

<ul id="pagination-container"></ul>

@section Scripts{
    <script>
        $(document).ready(function () {
        var items = $(".list-wrapper .list-item");
        var numItems = items.length;
        var perPage = 4;

        items.slice(perPage).hide();

        $('#pagination-container').pagination({
            pages: 3,
            currentPage: 1,
            //items: numItems,
            itemsOnPage: 4,
            prevText: "&laquo;",
            nextText: "&raquo;",
            onPageClick: function (pageNumber) {
                var showFrom = perPage * (pageNumber - 1);
                var showTo = showFrom + perPage;
                items.hide().slice(showFrom, showTo).show();
            }
        });
        })
    </script>
}

    @*@section Scripts{
    <script>
        $(document).ready(function () {
            console.log("success")
            var items = $(".list-wrapper .list-item");
            var numItems = items.length;
            var perPage = 5;

            items.slice(perPage).hide();
            $('#emp-pagination').Pagination({
                pages: $('#hdnTotalNumberOfPages').val(),
                currentPage: $('#hdnCurrentPage').val(),
                itemsOnPage: 5,
                cssStyle: 'light-theme',
                onPageClick: function (pageNo) {
                    var url = "/Employee/Index?pageNumber=" + pageNo;
                    window.location.href = url;
                },
                hrefTextSuffix: '',
                selectOnClick: true
            })
        })
    </script>
    }*@

【讨论】:

  • 我在布局上添加它之后我检查错误不存在所以错误解决但分页不显示
  • @ahmedbarbary 感谢您的回复,pagination not display 是另一回事,我们不知道您如何配置插件/数据查询/...最好提供错误消息,以便我们继续进行故障排除
  • 放置脚本部分的正确位置是什么
  • 你的cshtml的底部?你可以看到我的截图,我的部分在底部
  • 你是说 layout.cshtml 的底部吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
  • 2021-02-17
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多