简体   繁体   English

想要访问 angular 中的数组索引

[英]Want to access index of array in angular

I am getting array in this way, check attached image.我以这种方式获得数组,请检查附图。 在此处输入图像描述

Now I am trying to display edit icon as this below:现在我正在尝试显示如下所示的编辑图标:

{
      header: 'Edit',
      field: 'id',
      view: (value: string, obj: any) => { return `<i class='material-icons' style='cursor: pointer;font-size: 20px;margin-left: 10px;'
       aria-hidden='false' aria-label='Example edit icon' userGuid=${obj}>edit</i>`; }
    }

In this userGuid I want to pass index of array, like 0, 1 etc..在此 userGuid 中,我想传递数组的索引,如 0、1 等。

So in obj I am getting full array but I am not sure how can I access that index and pass it in userGuid.所以在 obj 中我得到了完整的数组,但我不确定如何访问该索引并将其传递给 userGuid。

Any help.任何帮助。

Can you try with cellRenderer property for rendering the edit button.您可以尝试使用cellRenderer属性来呈现编辑按钮吗?

{
  header: 'Edit',
  field: 'id',
  cellRenderer: (params) => { return `<i class='material-icons' style='cursor: pointer;font-size: 20px;margin-left: 10px;'
   aria-hidden='false' aria-label='Example edit icon' userGuid=${params.rowIndex}>edit</i>`; }
}

Let say the array from your picture has been saved to "const yourUserArray;".假设您图片中的数组已保存到“const yourUserArray;”。

There are 3 different way to access the your array:有 3 种不同的方式来访问您的数组:

Foreach:对于每个:

  for(const user of yourUserArray){
        console.log(user); // Here is one user
        // Here you can add your if-condition to filter out your user
    }

For loop:对于循环:

for(let i = 0, i > yourUserArray.length; i++){
    console.log(yourUserArray[i]); // Here is one user
    // Here you can add your if-condition to filter out your user
}    

If you already know which user you want to call, just use brackets:如果您已经知道要呼叫哪个用户,只需使用方括号:

console.log(yourUserArray[5]); // Gets the 6th User from yourUserArray by index. 

Now you need the id of the user right?现在您需要用户的 ID 对吗?

You can call it like this:你可以这样称呼它:

console.log(yourUserArray[5].id); // Gives you the id of the 6th 

Do you also want to list all your users in the frontend?您还想在前端列出所有用户吗?

If yes you can change your view into this:如果是,您可以将您的观点更改为:

<ng-container *ngFor="let user of yourUserArray">
<!-- Here some oder Information about the user? -->
<i class='material-icons' style='cursor: pointer;font-size: 20px;margin-left: 10px;'
       aria-hidden='false' aria-label='Example edit icon' userGuid={{user.id}}>edit</i>
</ng-container>

I hope this helps.我希望这有帮助。

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

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