gpt4 book ai didi

javascript - 不使用 Material 的 Angular 8 分页自定义

转载 作者:行者123 更新时间:2023-12-05 07:01:57 26 4
gpt4 key购买 nike

我想在不使用 Material 库的情况下在 Angular 8 项目中进行分页。我收到一个包含 10 个数据行、第一页链接、最后一页链接和总页数的数组。

服务器响应:

{
data : [
{name: 'abc': id: 1},
{name: 'abc': id: 2},
{name: 'abc': id: 3},
{name: 'abc': id: 4}
],
first_page_url: "http://example.com/api/data?page=1"
from: 1,
last_page: 5,
last_page_url: "http://example.com/api/data?page=5",
next_page_url: "http://example.com/api/data?page=2",
path: "http://example.com/api/data",
per_page: 30,
prev_page_url: null,
to: 30,
total: 123,
}

如何使分页正常工作?

最佳答案

实际上我是通过 vanilla js 为我的 laravel(bootstrap)应用程序做的..

@yasirali 你拥有几乎所有的东西..只有 current_page 变量丢失..

我认为它会帮助你...

注意: 您可以通过减去表单 next_page_url 轻松获取当前页码 (current_page ) :http://example.com/api/data?page=5

var last_page_url = "http://example.com/api/data?page=5";
var last_page = last_page_url.split("=");

var next_page_url = 'http://example.com/api/data?page=1';
var current_page = next_page_url.split("=");

if(current_page[1] != 1 || current_page[1] != last_page[1]){
current_page = current_page[1] - 1;
}else if (current_page[1] == last_page[1]){
current_page = last_page[1];
}else if (current_page[1] == 1){
current_page = 1;
}

如果你在最后一页或第一页 next_page_url last_page_url 你不需要减去

注意:如果您在最后一页或第一页.. 然后检查您现在得到什么 next_page_url last_page_url url。 . 如果你得到 null 然后根据你的需要应用你的条件......(在 laravel 应用程序中我得到 null)......

enter image description here

 function paginate (data){
let pagination = '';
let cDisabled = data.prev_page_url == null ? 'disabled' : '';
let lDisabled = data.next_page_url == null ? 'disabled' : '';

let start = ( data.current_page -2 ) > 0 ? (data.current_page -2) : 1 ;
let last = Math.ceil(data.total /data.per_page) ;
let end = ((data.current_page +2) < last) ? (data.current_page +2 ) : last ;
pagination += `
<li class="page-item ${cDisabled}"><a class="page-link" href="${data.first_page_url}">First</a></li>
<li class="page-item ${cDisabled}"><a class="page-link" href="${data.prev_page_url}">Previous</a></li>
`;

if( start > 1 ){
pagination += `
<li class="page-item ${cDisabled}"><a class="page-link" href="${data.first_page_url}">1</a></li>
<li class="page-item "><a class="page-link" href="#" >...</a></li>

`;
}

for(let i= start ; i <= end ; i++){
var active = data.current_page == i ? 'active' : false;

pagination += `
<li class="page-item ${active}"><a class="page-link" href="${data.path}?page=${i}">${i}</a></li>
`;
}


if(end < last){
pagination += `
<li class="page-item ${lDisabled}"><a class="page-link" href="#">...</a></li>
<li class="page-item ${lDisabled}"><a class="page-link" href="${data.last_page_url}">${last }</a></li>

`;
}

pagination += `
<li class="page-item ${lDisabled}"><a class="page-link" href="${data.next_page_url}">Next</a></li>
<li class="page-item ${lDisabled}"><a class="page-link" href="${data.last_page_url}">Last</a></li>
`;
document.getElementById("pagination").innerHTML = pagination;
}

关于javascript - 不使用 Material 的 Angular 8 分页自定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63700423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com