解决angular11+zorro使用table组件排序失效以及分页组件失效问题,附完整DEMO代码

本文主要是介绍解决angular11+zorro使用table组件排序失效以及分页组件失效问题,附完整DEMO代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

关于这个排序失效问题的核心点,跟这个有关系:[nzFrontPagination]=“false”

关于分页组件失效的问题,是你获取数据以后,需要给页码,页数,总条数都要重新赋值,这个真的是好无语

要考虑清楚,你是前端分页还是后端分页,如果是前端分页,代码可以参考官方zorro文档,里边很多,如果你是后端分页,那么可以参考下面的DEMO

首先新建 一个组件,在路由里和模块里都注册一下,注册路由你自己根据需求来,

  • 备注:如果不注册到当前模块,某些zorro的组件是无法使用的,会报错,这个是angular11版本和其他版本的不同之处。。。不再赘述了

来,上DEMO

html

<p>my-test01 works!</p>
<div class="bottom_table"><nz-table #filterTable [nzData]="listOfData" nzTableLayout="fixed" [nzShowPagination]="false"[nzFrontPagination]="false" [nzScroll]="{ x: '1200px', y: '570px' }"><thead><tr><th *ngFor="let column of listOfColumns" [nzSortFn]="column.sortFn" [nzWidth]="column.w"(nzSortOrderChange)="sortChange($event,column.key)">{{ column.name }}</th></tr></thead><tbody><tr *ngFor="let data of filterTable.data"><td>{{ data.item01 }}</td><td>{{ data.item02 }}</td><td>{{ data.item03 }}</td><td>{{ data.item04 }}</td><td>{{ data.item05 }}</td><td>{{ data.item06 }}</td></tr></tbody></nz-table><div class="my_page"><div class="pagination_css"><ng-template #totalTemplate let-total>共{{ total }}條</ng-template><nz-pagination  [(nzPageSize)]="page.pageSize" [(nzPageIndex)]="page.current" [nzTotal]="page.total"[nzSize]="'small'" nzShowSizeChanger   nzShowQuickJumper [nzShowTotal]="totalTemplate" (nzPageIndexChange)="currentChange($event)"(nzPageSizeChange)="pageSizeChange($event)"></nz-pagination></div></div>
</div>

ts

import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { _HttpClient } from '@delon/theme';
interface DataItem {item01: number;item02: number;item03: string;item04: string;item05: string;item06: string;
}
interface ColumnItem {name: string;w: string;key: string,sortFn: boolean,
}
@Component({selector: 'app-my-test01',templateUrl: './my-test01.component.html',styleUrls: ['./my-test01.component.less']
})export class MyTest01Component implements OnInit {constructor(private msgSrv: NzMessageService,public http: _HttpClient,) { }ngOnInit(): void {this.setData()}// 分頁代碼page = {current: 1,total: 0,pageSize: 10}totalTemplate = true// 分頁代碼// 表格代碼----startlistOfColumns: ColumnItem[] = [{name: '序號',key: 'item01',sortFn: false,w: '4%',},{name: '第二列',key: 'item02',sortFn: true,w: '4%',},{name: '第三列',key: 'item03',sortFn: true,w: '5%',},{name: '第四列',key: 'item04',sortFn: true,w: '4%',},{name: '第五列',key: 'item05',sortFn: true,w: '4%',},{name: '第六列',key: 'item06',sortFn: true,w: '12%',},];listOfData: DataItem[] = [{item01: 1,item02: 0.01,item03: '1',item04: '1',item05: '1',item06: '1',},];setData() {this.listOfData = new Array(500).fill({}).map((_, i) => {return {item01: i + 1,item02: 0.01 * (i + 1),item03: i + 10 + '數據',item04: i + 100 + '數據',item05: i + 1000 + '數據',item06: i + 10000 + '數據',}})this.page.total = this.listOfData.length}sortChange(e, name) {console.log(e, name)this.listOfData = e === 'ascend' ? this.listOfData.sort((a, b) => (typeof (a[name]) === 'string') ? a[name].localeCompare(b[name]) : a[name] - b[name]) : this.listOfData.sort((a, b) => (typeof (a[name]) === 'string') ? b[name].localeCompare(a[name]) : b[name] - a[name])}currentChange(e) {this.page.current = econsole.log('頁碼this.page.current:', this.page.current)const params = {cur_page: this.page.current,page_size: this.page.pageSize,}this.getTable(params)}pageSizeChange(e) {this.page.pageSize = econsole.log('頁碼this.page.pageSize:', this.page.pageSize)const params = {cur_page: 1,page_size: this.page.pageSize,}this.getTable(params)}baseUrl = ''async getTable(params) {// const res4: any = await this.myHttpRes('post', this.baseUrl + '/testData', params, false);// if (res4 && res4.success) {//   this.msgSrv.success(res4.msg ? res4.msg : 'Get Data Success!')//   this.listOfData = res4.data.tableData//   this.page.total = res4.data.total//   console.log(this.listOfData)// } else {//   this.msgSrv.error(res4 && res4.msg ? res4.msg : 'Get Table Data Fail!')// }// 模擬數據console.log('第几頁:',params.cur_page)console.log('每頁條數:',params.page_size)let newArr = []newArr = new Array(params.page_size).fill({}).map((_, i) => _ = {item01: i + 1 + (params.cur_page - 1) * 10,item02: 0.01 * (i + 1),item03: i + 10 + '數據',item04: i + 100 + '數據',item05: i + 1000 + '數據',item06: i + 10000 + '數據',})this.listOfData = newArrthis.page.current = params.cur_page // 解決奇葩的問題this.page.pageSize = params.page_size // 解決奇葩的問題this.page.total = 500}// 下面都是工具函數-----------分割線----------------------分割線-----------async myHttpRes(method: string, url: string, formData: object = {}, needMsg, msgScu = 'Success!', msgErr = 'Network Error!'): Promise<Node[]> {return new Promise<Node[]>((resolve: any, reject: any) => {// this.http[method](url, formData).pipe(timeout(12000)).subscribe(this.http[method](url, formData).subscribe({next: (res: any) => {if (res && (['success', 'OK'].indexOf(res.status) !== -1 || res.success || [0].indexOf(res.errorNum) !== -1 || ['1', '0', '000000', '001', '002'].indexOf(res.code) !== -1)) {needMsg && this.msgSrv.success(res.message || msgScu)resolve(res)// resolve(res)} else {needMsg && this.myAlert(res.message || msgErr)// 操作失敗,請檢查網絡重試...reject(new Error(res.message || msgErr))}},error: (err: any) => {// console.log('走這裡了123',err)needMsg && this.myAlert(msgErr)reject(new Error(err.message || 'Error'))}})})}// 因需求增加提示框,樣式需要增加到容易修改的地方myAlert(name) {var myAlertBigBoxIsTrue = document.getElementById('myMLBAlertBigBox');// console.log(myAlertBigBoxIsTrue);if (myAlertBigBoxIsTrue === null) {// 創建一個遮罩層var bigbox = document.createElement("div");bigbox.id = "myMLBAlertBigBox";//创建一个大盒子var box = document.createElement("div");var myspan = document.createElement('span');//创建一个关闭按钮var button = document.createElement("button");bigbox.appendChild(box);// 設置遮罩層的樣式var bigboxName = {"width": "100%","height": "100vh","background-color": "rgba(0,0,0,0.4)","position": "fixed","top": "0","left": "0","z-index": "1000","text-align": "center"}//给元素添加元素for (var k in bigboxName) {bigbox.style[k] = bigboxName[k];}//定义一个对象保存样式var boxName = {width: "500px",height: "180px",backgroundColor: "white",border: "1px solid rgb(226,222,222)",position: "absolute","box-shadow": "5px 5px 10px 2px rgba(0,0,0,0.4)",top: "20%","border-radius": "5px",left: "50%",margin: "-90px 0 0 -250px",zIndex: "1001",textAlign: "center",lineHeight: "180px"}//给元素添加元素for (var k in boxName) {box.style[k] = boxName[k];}//把创建的元素添加到body中document.body.appendChild(bigbox);//把alert传入的内容添加到box中if (arguments[0]) {// box.innerHTML = arguments[0];myspan.innerHTML = arguments[0];}// 定義span樣式var spanName = {"text-align": "left","line-height": "30px","border-radius": "5px","outline": "none","word-break": "break-all","position": "absolute","overflow-y": "auto","overflow": "auto","height": "112px","top": "20px","right": "25px","left": "25px",}for (var j in spanName) {myspan.style[j] = spanName[j];}// bigbox.appendChild(box);box.appendChild(myspan);button.innerHTML = '關閉'// 改為I18N// button.innerHTML = this.fanyi('button.close');//定义按钮样式var btnName = {border: "1px solid #ccc",backgroundColor: "#fff",width: "70px",height: "30px",textAlign: "center",lineHeight: "27px","border-radius": "5px",outline: "none",position: "absolute",bottom: "10px",right: "20px",}for (var j in btnName) {button.style[j] = btnName[j];}//把按钮添加到box中box.appendChild(button);//给按钮添加单击事件button.addEventListener("click", function () {bigbox.style.display = "none";var idObject = document.getElementById('myMLBAlertBigBox');if (idObject != null)idObject.parentNode.removeChild(idObject);})} else {return;}}// 因需求增加提示框,樣式需要增加到容易修改的地方
}

在这里插入图片描述

这篇关于解决angular11+zorro使用table组件排序失效以及分页组件失效问题,附完整DEMO代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/805146

相关文章

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

qt5cored.dll报错怎么解决? 电脑qt5cored.dll文件丢失修复技巧

《qt5cored.dll报错怎么解决?电脑qt5cored.dll文件丢失修复技巧》在进行软件安装或运行程序时,有时会遇到由于找不到qt5core.dll,无法继续执行代码,这个问题可能是由于该文... 遇到qt5cored.dll文件错误时,可能会导致基于 Qt 开发的应用程序无法正常运行或启动。这种错

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(