angular - 如何在角度 8 中使用 for 循环显示更多/隐藏更少切换以显示更多文本
问题描述
我有一个表,其数据来自循环。当我的内容文本超过 10 个字符时,应该自动出现“显示更多”链接并且应该减少文本长度。再次,当我单击“显示更多”时,文本应该再次扩展为所有/剩余字符,并带有“隐藏更少”链接。再次如果我再次单击“隐藏更少”返回到较早的位置。展开/折叠应该单独工作。这是下面的代码
home.component.html
<table>
<tr *ngFor="let items of statusdata"><td>{{items.groupname}} <span>Show more..</span></td></tr>
</table>
home.component.ts
import { Component, OnInit } from '@angular/core';
import { CommonserviceService } from './../utilities/services/commonservice.service';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
declare var $: any;
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
statusdata: any;
ngOnInit() {
this.statusdata = [{"groupid":1,"groupname":"project1project1project1project1project1project1project1project1project1"},{"groupid":2,"groupname":"project2project1project1project1project1project1project1project1"},{"groupid":3,"groupname":"project3project1project1project1project1project1project1project1project1project1"}];
}
}
解决方案
试试这样:
.html
<table>
<tr *ngFor="let items of statusdata">
<td>
<span *ngIf="!items.showMore"> {{trimString(items.groupname,10)}}</span>
<span *ngIf="items.showMore"> {{items.groupname}} </span>
<div *ngIf="items.groupname.length> 10" (click)="items.showMore = !items.showMore">Show
<span>{{items.showMore ? 'less' : 'More'}}</span>
</div>
</td>
</tr>
</table>
.ts
constructor() {
this.statusdata = this.statusdata.map(item => ({
...item,
showMore:false,
}));
}
trimString(text, length) {
return text.length > length ?
text.substring(0, length) + '...' :
text;
}
推荐阅读
- alpine.js - 将 AlpineJS 点击事件附加到刀片组件
- python - 您可以将 Github 机密合并到自定义配置 yml 文件中吗?
- reactjs - 如何限制文本输入和额外文本添加但不是斜体粗体突出显示,以便用户无法添加文本但可以加粗文本
- c# - 如何将所有页面和附件从 PDF 提取为 PNG
- sql - 在多个数据库中索引同一个表
- vaadin - Vaadin 14 TreeGrid MultiSelection 模式与分层数据
- sql - 不存在列 ORACLE SQL 的 CASE WHEN 语句
- python - 导入前路径管理的良好编码实践(python)是什么?
- gwt - GWT:如何从 gwt 2.7.0 迁移到 gwt 2.8.2
- sap-erp - SAP 模块 MM 中是否有任何事务表?