angular新闻列表分页

news/2025/2/26 18:08:56

说明:使用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.niftyadmin.cn/n/5869037.html

相关文章

解决安卓recyclerView滚到底部不彻底问题

问题分析&#xff1a; 传统recycleview滚到到底部方式scrollToPosition(lastpositon)&#xff0c;只能定位到最后一条数据的顶部。由于数据过长&#xff0c;无法滚动到最底部。 问了下deepseek&#xff0c;给了个方案&#xff1a; private void recyclerViewScrollToBottom()…

Qt/C++项目积累:3.日志管理系统 - 3.1 项目介绍

在实际工程项目中&#xff0c;日志系统无疑是比较重要地分析问题的手段&#xff0c;常用的一般是将其写入到日志文件中&#xff0c;或者写入数据库文件&#xff0c;进行分析&#xff0c;而工程人员或者开发人员需要实时查看日志&#xff0c;可能不太方便&#xff0c;于是就需要…

DeepSeek 助力 Vue 开发:打造丝滑的分割线(Divider)

前言&#xff1a;哈喽&#xff0c;大家好&#xff0c;今天给大家分享一篇文章&#xff01;并提供具体代码帮助大家深入理解&#xff0c;彻底掌握&#xff01;创作不易&#xff0c;如果能帮助到大家或者给大家一些灵感和启发&#xff0c;欢迎收藏关注哦 &#x1f495; 目录 Deep…

《Vue全栈图形绘制系统开发实战》—— 第一章础架构与核心模块实现

第一章 基础架构与核心模块实现 #mermaid-svg-am3qjLePI9PBjAJy {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-am3qjLePI9PBjAJy .error-icon{fill:#552222;}#mermaid-svg-am3qjLePI9PBjAJy .error-text{fill:#552…

浅析DeepSeek在商业银行的应用

在金融科技蓬勃发展的时代背景下,人工智能技术正重塑商业银行的运营模式与服务生态。DeepSeek作为一款极具潜力的大语言模型,可凭借其独特的优势广泛应用于商业银行多个业务领域,为银行数字化转型注入新动力。然而,与所有新兴技术类似,DeepSeek在应用过程中也面临诸多挑战…

数据结构实战:高效的缓存系统(哈希表 + LRU算法)与分布式任务调度系统(树形结构 + 图形算法)

系列文章目录 01-从零开始掌握Python数据结构&#xff1a;提升代码效率的必备技能&#xff01; 02-算法复杂度全解析&#xff1a;时间与空间复杂度优化秘籍 03-线性数据结构解密&#xff1a;数组的定义、操作与实际应用 04-深入浅出链表&#xff1a;Python实现与应用全面解析 …

HTML操作详解

目录 vscode开发工具搭建 快速生成代码 快捷键 HTML的标签 img标签 src属性 alt属性 title属性 width/height属性 border属性 a标签 href属性 target属性 表格标签(table) 列表标签 表单标签 表单域 form标签 表单控件 input标签 lable/select/textarea标…

IP------PPP协议

3.PPP协议 1.PPP优点 网络类型&#xff1a;p2p PPP---点到点协议 兼容性会更强凡是接口或者链路支持全双工的工作模式&#xff0c;就可以运行PPP协议。 -----单工-----只支持一边发送数据 半双工------两边都可以发送数据&#xff0c;但是两边不能同时发送 ---------全双工-…