简体   繁体   English

单击列表项时如何显示其他数据?

[英]How can I display additional data when clicking on list item?

I managed to display some mock data (name, email and username) but as you can see I wanted to display more detailed information (city,country,...) on a User by clicking on a row 我设法显示一些模拟数据(名称,电子邮件和用户名)但是你可以看到我想通过点击一行在用户上显示更详细的信息(城市,国家,...)

I am still new to angular and still have problems using the correct syntax. 我仍然是角度新手,使用正确的语法仍然有问题。 I am stuck searching the web for hours although I know it should be quite easy... 我在网上搜索了好几个小时,虽然我知道它应该很容易...

I thank you all in advance 我提前感谢你们

This is what I have so far : 这是我到目前为止:
https://i.stack.imgur.com/WD7gv.png

(this is my first question on stack overflow, I am sorry for mistakes :P ) (这是关于堆栈溢出的第一个问题,我很抱歉错误:P)

export class ScrollComponent {
  users;
  constructor() { 
    this.users = Array(100)
    .fill(1)
    .map(_ => {
      return {
        name: faker.name.findName(),
        email: faker.internet.email(),
        exMail: faker.internet.exampleEmail(),
        userName: faker.internet.userName(),
        url: faker.internet.url(),
        ip: faker.internet.ip(),
        mac: faker.internet.mac(),
        pass: faker.internet.password(),
        address: faker.address.streetAddress(), 
        zip: faker.address.zipCode(),
        city: faker.address.city(),
        country: faker.address.county(),
        iban: faker.finance.iban(),
        bic: faker.finance.bic(),
        bitcoin: faker.finance.bitcoinAddress()

      };
    });

<cdk-virtual-scroll-viewport itemSize="100">


  <li *cdkVirtualFor="let u of users" class="animated slideInUp">
    <h2>{{ u.name }} </h2>
    <p> {{ u.email }} {{ u.userName }} </p>
  </li>


</cdk-virtual-scroll-viewport>

Welcome to SO! 欢迎来到SO!

First of all keep in mind, that when using libraries like Angular Material or ng-bootstrap you have components that can already do this. 首先要记住,当使用Angular Materialng-bootstrap等库时,您可以使用已经可以执行此操作的组件。 As you are using the CDK, you probably use Angular Material as well? 在使用CDK时,您可能也使用Angular Material? Then you could use the Expansion Panel . 然后你可以使用扩展面板

Otherwise as you expected you can achieve this quite easily. 否则如您所料,您可以轻松实现这一目标。 There are different ways to do this, depending on your specific needs (ie should every list-element be expandable at the same time or only one?). 有不同的方法可以做到这一点,具体取决于您的具体需求(即每个列表元素应该同时扩展还是只扩展一个?)。 I will give you a hint for what I think is the easiest way and then you can adjust it to your needs. 我会给你一个我认为最简单方法的提示,然后你可以根据自己的需要进行调整。

First thing is to add a boolean to your user-type holding the state of each list-element. 首先是为用户类型添加一个boolean ,保存每个list-element的状态。 Let's call it detailsVisible . 我们称之为detailsVisible

Then you want to add a ClickHandler to the list items which toggles this boolean : 然后,您要将ClickHandler添加到切换此boolean的列表项:

<li *cdkVirtualFor="let u of users" class="animated slideInUp" (click)="u.detailsVisible = !u.detailsVisible">

and then add the the details into some element whichs visibility you control using *ngIf : 然后将细节添加到使用*ngIf控制的可见性元素中:

<li *cdkVirtualFor="let u of users" class="animated slideInUp" (click)="u.detailsVisible = !u.detailsVisible">
   <h2>{{ u.name }} </h2>
   <p> {{ u.email }} {{ u.userName }} </p>
   <div *ngIf="u.detailsVisible"> additional details in here </div>
</li>

If you only want one element to be allowed to be expanded at the same time you would go for something like this. 如果你只想让一个元素同时被扩展,那么你会想要这样的东西。 The idea is to just store the index of the selected element and only show details for this. 我们的想法是只存储所选元素的索引,并仅显示其详细信息。

<li *cdkVirtualFor="let u of users; let i = index" class="animated slideInUp" (click)="expandedElement = i">
   <h2>{{ u.name }} </h2>
   <p> {{ u.email }} {{ u.userName }} </p>
   <div *ngIf="users.indexOf(u) === expandedElement"> additional details in here </div>
</li>

and then of course add the expandedElement variable to your .ts-file. 然后当然将expandedElement变量添加到.ts文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM