javascript - 如何使用 typescript 通过切换按钮显示 html 表的内联详细信息

标签 javascript angular typescript html-table

我需要为我的表格创建一个切换按钮(以显示/隐藏)所选行的更多详细信息。

鉴于这是我的 table :

<table>
        <tr>
            <th>Company</th>
            <th>Contact</th>
            <th>Country</th>
        </tr>
        <tr *ngFor='let c of companies'>
            <td><button id="{{c.companyId}}" (click)="showDetail()">Details</button> </td>
            <td>{{ c.company}}</td>
            <td>{{ c.contact}}</td>
            <td>{{ c.country }}</td>
        </tr>
    </table>

当我单击“详细信息”按钮时,需要在表格中内嵌显示详细信息。这是 Kendo Grid 中的一种主细节网格方法。有没有更好的方法使用 Angular2 和 typescript 来轻松显示详细信息?

最佳答案

与 birwin 的答案相比有一个小变化,因为如果没有指令,您无法在此处使用 template ,因此请使用 ng-container 代替。

<ng-container *ngFor='let c of companies'>
    <tr>
        <td><button id="{{c.companyId}}" (click)="c.details = !c.details">Details</button> </td>
        <td>{{ c.company}}</td>
        <td>{{ c.contact}}</td>
        <td>{{ c.country }}</td>
    </tr>
    <tr *ngIf="c.details">
        <td>Details</td>
    </tr>
</ng-container>

Demo

关于javascript - 如何使用 typescript 通过切换按钮显示 html 表的内联详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43547312/

相关文章:

javascript - 传递值将 Ajax 抛出到一个 php 文件

javascript - Highmaps 出现 'Uncaught TypeError: undefined is not a function ' 并且未加载?

javascript - 使用带有身份服务器 4 的 oidc 客户端静默登录

javascript - React.cloneElement -> 添加 className 元素

javascript - 防止 'hashchange' 事件的默认行为

html - border-radius-top-left 不适用于 Angular2

javascript - 在操作之前检查 Javascript/Typescript 中嵌套的 JSON 以查看 Data 是否为 null

reactjs - 以编程方式导航 : React Router V4+ Typescript giving error

html - Angular Nested *ngFor 在 HTML 模板中使用时给出无限结果

angular - 与 react-query for angular 类似的库