angular新闻列表分页

devtools/2025/2/28 0:38:38/

说明:使用angular技术,material控件,ngfor循环,img网络图片展示,分页组件

效果图:
在这里插入图片描述

step1: C:\Users\Administrator\WebstormProjects\untitled4\src\app\home\home.component.ts

javascript">import { Component, ViewChild, AfterViewInit } from '@angular/core';
import {MatListModule} from '@angular/material/list';
import {NgForOf, SlicePipe} from '@angular/common';
import {MatPaginator} from '@angular/material/paginator';// 定义列表项的数据结构
interface ListItem {id: number;title: string;description: string;avatarUrl: string;
}@Component({selector: 'app-home',imports: [MatListModule, NgForOf, MatPaginator, SlicePipe],templateUrl: './home.component.html',styleUrl: './home.component.css'
})
export class HomeComponent implements AfterViewInit {// 列表项数据数组listItems: ListItem[] = [{id: 1,title: 'Item 1',description: 'Item 1 description goes here',avatarUrl: 'https://randomuser.me/api/portraits/men/1.jpg'},{id: 2,title: 'Item 2',description: 'Item 2 description goes here',avatarUrl: 'https://randomuser.me/api/portraits/men/2.jpg'},{id: 3,title: 'Item 3',description: 'Item 3 description goes here',avatarUrl: 'https://randomuser.me/api/portraits/men/3.jpg'},{id: 4,title: 'Item 4',description: 'Item 4 description goes here',avatarUrl: 'https://randomuser.me/api/portraits/men/4.jpg'},{id: 5,title: 'Item 5',description: 'Item 5 description goes here',avatarUrl: 'https://randomuser.me/api/portraits/men/5.jpg'},{id: 6,title: 'Item 6',description: 'Item 6 description goes here',avatarUrl: 'https://randomuser.me/api/portraits/men/6.jpg'}];// 分页相关变量@ViewChild(MatPaginator) paginator!: MatPaginator;pageSize = 2; // 每页显示的条数currentPage = 0; // 当前页码constructor() { }// 处理点击事件handleClick(event: Event, itemId: number): void {console.log(`Clicked on item ${itemId}`);// 在这里可以添加其他逻辑,比如导航到详情页面}ngAfterViewInit(): void {// 将数据源连接到分页器this.paginator.length = this.listItems.length;this.paginator.pageSize = this.pageSize;}// 处理分页事件handlePageEvent(event: any): void {this.pageSize = event.pageSize;this.currentPage = event.pageIndex;console.log(`Page size: ${this.pageSize}, Page index: ${this.currentPage}`);}
}

step2: C:\Users\Administrator\WebstormProjects\untitled4\src\app\home\home.component.html

<mat-list role="list"><mat-list-item role="listitem" *ngFor="let item of listItems | slice: currentPage * pageSize : (currentPage + 1) * pageSize"><div class="item-container" (click)="handleClick($event, item.id)"><img[src]="item.avatarUrl"[alt]="item.title"class="item-avatar"/><div class="item-content"><h3 class="item-title">{{ item.title }}</h3><p class="item-description">{{ item.description }}</p></div></div></mat-list-item>
</mat-list><!-- 分页组件 -->
<mat-paginator[pageSize]="pageSize"[pageSizeOptions]="[2, 4, 6]"(page)="handlePageEvent($event)"[length]="listItems.length"[hidePageSize]="false"showFirstLastButtons="true">
</mat-paginator>

step3: C:\Users\Administrator\WebstormProjects\untitled4\src\app\home\home.component.css

/* 在你的 CSS 文件中添加以下样式 */
.custom-list {background-color: #f8f9fa; /* 列表背景色 */padding: 20px; /* 列表的内边距 */
}.list-item {background-color: #ffffff; /* 列表项背景色 */margin-bottom: 10px; /* 列表项之间的间距 */border-radius: 8px; /* 列表项的圆角 */box-shadow: 0 2px 4px rgba(0,0,0,0.1); /* 添加阴影效果 */transition: transform 0.3s ease; /* 添加悬停动画 */
}.list-item:hover {transform: translateX(5px); /* 悬停时向右移动5px */box-shadow: 0 4px 6px rgba(0,0,0,0.15); /* 悬停时增加阴影 */
}.item-container {padding: 15px; /* 内容的内边距 */display: flex;align-items: center; /* 垂直居中 */
}.item-avatar {width: 60px;height: 60px;border-radius: 50%;background-color: #e0e0e0;object-fit: cover;margin-right: 15px;box-shadow: 0 2px 4px rgba(0,0,0,0.15);transition: transform 0.3s ease;
}.item-avatar:hover {transform: scale(1.05);
}.item-content {flex: 1; /* 文本内容占满剩余空间 */
}.item-title {color: #2c3e50; /* 标题颜色 */margin: 0; /* 移除标题的默认外边距 */font-size: 18px; /* 标题字体大小 */font-weight: 600; /* 标题加粗 */
}.item-description {color: #7f8c8d; /* 描述文字颜色 */margin: 5px 0 0 0; /* 描述文字的外边距 */font-size: 14px; /* 描述文字大小 */line-height: 1.5; /* 行高 */
}

end


http://www.ppmy.cn/devtools/163205.html

相关文章

java进阶1——JVM

java进阶——JVM 1、JVM概述 作用 Java 虚拟机就是二进制字节码的运行环境&#xff0c;负责装载字节码到其内部&#xff0c;解释/编译为对 应平台上的机器码指令行&#xff0c;每一条 java 指令&#xff0c;java 虚拟机中都有详细定义&#xff0c;如怎么取操 作数&#xff0c…

突破“第一崇拜“:五维心理重构之路

一、视频介绍 在这个崇尚"第一"的时代&#xff0c;我们如何找到自己的独特价值&#xff1f;本视频将带您踏上五维心理重构之旅&#xff0c;从诗意人生的角度探讨如何突破"圣人之下皆蝼蚁"的局限。我们将穿越人生的不同阶段&#xff0c;从青春的意气风发到…

《Keras 3 使用 PointNet 进行点云分段》:此文为AI自动翻译

使用 PointNet 进行点云分段 作者:Soumik Rakshit、Sayak Paul 创建日期:2020/10/23 最后修改日期:2020/10/24 描述:实现基于 PointNet 的模型,用于分割点云。 (i) 此示例使用 Keras 3 在 Colab 中查看 GitHub 源 介绍 “点云”是用于存储几何形状数据的一种重要数据…

DeepSeek、微信、硅基流动、纳米搜索、秘塔搜索……十种不同方法实现DeepSeek使用自由

为了让大家实现 DeepSeek 使用自由&#xff0c;今天分享 10 个畅用 DeepSeek 的平台。 一、官方满血版&#xff1a;DeepSeek官网与APP 首推&#xff0c;肯定是 DeepSeek 的官网和 APP&#xff0c;可以使用满血版 R1 和 V3 模型&#xff0c;以及联网功能。 网址&#xff1a; htt…

是德科技keysight N5173B信号发生器,是一款经济高效的仪器

是德科技keysight N5173B信号发生器安捷伦N5173B信号源 是德N5173B微波模拟信号发生器&#xff0c;拥有 9 kHz 至 40 GHz 的频率覆盖范围&#xff0c;N5173B为宽带滤波器、放大器、接收机等器件的参数测试提供了必要的信号&#xff0c;是一款经济高效的仪器。 N5173B特点&…

低代码与开发框架的一些整合[3]

1.基本说明 审批流程是企业内部运营的运行流程&#xff0c;与业务板块进行关联&#xff0c;在企业数智化过程中启动业务串联的作用&#xff0c;与AI业务模型及业务agent整合后&#xff0c;将大大提升企业的运行效率以及降低运营风险。 近期对开源的近40个携带流程平台的项目进…

Skyeye 云智能制造办公系统 VUE 版本 v3.15.10 发布

Skyeye 云智能制造&#xff0c;采用 Springboot winUI 的低代码平台、移动端采用 UNI-APP。包含 30 多个应用模块、50 多种电子流程&#xff0c;CRM、PM、ERP、MES、ADM、EHR、笔记、知识库、项目、门店、商城、财务、多班次考勤、薪资、招聘、云售后、论坛、公告、问卷、报表…

[LeetCode]day29 232.用栈实现队列

题目链接 题目描述 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作&#xff08;push、pop、peek、empty&#xff09;&#xff1a; 实现 MyQueue 类&#xff1a; void push(int x) 将元素 x 推到队列的末尾 int pop() 从队列的开头移除并返回元素 i…