How to get values in array of array in angular?

Rekha

I am getting the table column name as a status and I need to change the color of the buttons based on status value.

<ng-container matColumnDef="agentName">
    <th mat-header-cell *matHeaderCellDef>Agent Name</th>
    <td mat-cell *matCellDef="let element">{{element.agent_id.company_name}}</td>
</ng-container>

<ng-container matColumnDef="status">
    <th mat-header-cell *matHeaderCellDef>Status</th>
    <td mat-cell *matCellDef="let element"><span class="dot" [ngStyle]="{'background-color': getRowColor()}"></span></td>
</ng-container>

component.ts

// ...

export class Component {
    red = { 'background-color': 'red' }
    yellow = { 'background-color': '#DAA520' }
    green = { 'background-color': '#00FF00' }
    pageSizeOptions: number[];
    displayedColumns: string[] = ['title', 'agentName', 'status', 'star'];
    dat: any[] = [{
        id: 1,
        title: 'Test',
    }];
    state : any = [];
    getPos: any = [];

    dataSource: MatTableDataSource<{}>;
    @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;

    constructor(
        private store: Store<IAppState>,
        private userService: UserService,
        private poService: PoService,
        private propertiesService: PropertiesService,
        private _location: Location,
    ) {}

    ngOnInit() {
        this.getAllPO()
        this.pageSizeOptions = this.propertiesService.getPaginationSize()
    }

    edit(data) {
        this.poService.changeEditPurchaseOrder(data)
    }

    getAllPO() {
        const payload = {
            'company_id': this.userService.getUserdetails().CompanyUser.company_id.id,
        }

        this.showSpinner = true
        this.poSuccessSubscription.unsubscribe()
        this.poErrorSubscription.unsubscribe()
        this.store.dispatch(new GetAllPOs(payload))

        this.poSuccessSubscription = this.store
            .pipe(select(getAllPOsSuccess))
            .subscribe((result: any) => {
                if (!!result) {
                    this.getPos = result
                    this.state = this.getPos.find(element => {
                        return element.POMaterials.find(item =>{
                            return item.state
                        })
                    })
                    this.dataSource = new MatTableDataSource(result)
                    this.dataSource.paginator = this.paginator;
                    this.showSpinner = false
                } else {
                    this.dataSource = new MatTableDataSource([])
                    this.dataSource.paginator = this.paginator;
                    this.showSpinner = false
                }
            })

        this.poErrorSubscription = this.store
            .pipe(select(getAllPOsError))
            .subscribe((result: any) => {
                if (!!result) {
                    // alert(result.error)
                    this.showSpinner = false
                }
            })
    }

    getRowColor(state) {
        if (state === 'Not Received') {
            return 'red'
        } else if (state === 'Completed') {
            return '#00FF00'
        }
        else {
            return '#DAA520'
        }
    }

    applyFilter(filterValue: string) {
        this.dataSource.filter = filterValue.trim().toLowerCase();
        if (this.dataSource.paginator) {
            this.dataSource.paginator.firstPage();
        }
    }
}

My data looks like this. I need to get the value of state in each array and assign it to each row. Based on state value, I need to change the color of the buttons.

const result = [{
    Pomaterials: [{
        id: 1,
        state: 'not received'
    }],
    po_id: 1,
    mat_id: 3
}, {
    Pomaterials: [{
        id: 1,
        state: 'not received'
    }],
    po_id: 1,
    mat_id: 2
}];

Pierre-Henri Sudre

Your HTML and your JSON result is confusing:

<td mat-cell *matCellDef="let element"> {{element.agent_id.company_name}} </td>

And

this.dataSource = new MatTableDataSource(result)

imply you should have an agent_id object, like:

const result = [
    {
      Pomaterials: [
        { 
         id:1,
         state:"not received"
        }
      ]
      po_id:1,
      mat_id:3,
      agent_id: {
          company_name: "Company"
      }
    },
    ...
]

Anyway, your getRowColor(state) function has a state param and you don't provide any from the HTML:

<td mat-cell *matCellDef="let element"><span class="dot" [ngStyle]="{'background-color': getRowColor()}"></span> </td>

As element is a row of your JSON result, you may try:

<td mat-cell *matCellDef="let element"><span class="dot" [ngStyle]="{'background-color': getRowColor(element.Pomaterials[0].state)}"></span> </td>

Only if your Pomaterials is always built like the JSON result you provide us.

If you need to loop over Pomaterials array for setting the state you can do something like:

this.poSuccessSubscription = this.store
    .pipe(select(getAllPOsSuccess))
    .subscribe((result: any) => {
      if (!!result) {
        ...
        this.getPos.forEach(element => {
          element.finalState = evalFinalStateElement(element.Pomaterials);
        });
        ...
      }
      ...
});

evalFinalStateElement(Pomaterials: Array<any>) {
  let finalState;
  Pomaterials.forEach(Po => {
    if(!finalState) {
      finalState = Po.state;
    }
    else if(finalState === 'XXX' && Po.state === 'YYY') {
      finalState = Po.state
    }
    // ...
  });
  return finalState;
}

And then use the defined element.finalState variable inside your HTML:

<td mat-cell *matCellDef="let element"><span class="dot" [ngStyle]="{'background-color': getRowColor(element.finalState)}"></span> </td>

with the getRowColor(state) function you posted.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Angular : How to convert values in array and get value in outside scope?

分類Dev

How to get text values to an array?

分類Dev

how to get array values, on input field

分類Dev

How to get the values of generic array in Java?

分類Dev

How to get all values of objects inside array

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

How to loop an array from lower values to upper values in angular js?

分類Dev

get a set of values from an array

分類Dev

How to get an array of last nonzero values in columns of matrix?

分類Dev

How to parse json to get all values of a specific key within an array?

分類Dev

How to get distinct values from an array of fetch API output

分類Dev

How to get the count of repeated key values of array object of consecutive elements?

分類Dev

How to get distinct values from an array in mongoDB and group them by ID

分類Dev

How to get JSON response array all index values?

分類Dev

How to get the values of a column from a table in jQuery into a multidimentional array

分類Dev

Get values from array and using the values in a program

分類Dev

How to split delimiter array values into another Array

分類Dev

How to add additional values in to array

分類Dev

How to return same values of the array

分類Dev

How to store multiple values in array

分類Dev

Angular takes Array as object and does not display the values

分類Dev

Angular - filter an array of objects based on values in another array of objects

分類Dev

How to get size of array embedded array mongoose?

分類Dev

How to get array of array value count?

分類Dev

AJAX: How to get array on multi dimensional array

分類Dev

Get Highest value of Unique Values in Object Array

分類Dev

get child array values in a parent div element

分類Dev

Is there a way to get an array of the values of the properties in an object?

分類Dev

Get distinct values of child array as additional property

Related 関連記事

  1. 1

    Angular : How to convert values in array and get value in outside scope?

  2. 2

    How to get text values to an array?

  3. 3

    how to get array values, on input field

  4. 4

    How to get the values of generic array in Java?

  5. 5

    How to get all values of objects inside array

  6. 6

    How to get the combination of array values from nested arrays in an array of objects

  7. 7

    How to loop an array from lower values to upper values in angular js?

  8. 8

    get a set of values from an array

  9. 9

    How to get an array of last nonzero values in columns of matrix?

  10. 10

    How to parse json to get all values of a specific key within an array?

  11. 11

    How to get distinct values from an array of fetch API output

  12. 12

    How to get the count of repeated key values of array object of consecutive elements?

  13. 13

    How to get distinct values from an array in mongoDB and group them by ID

  14. 14

    How to get JSON response array all index values?

  15. 15

    How to get the values of a column from a table in jQuery into a multidimentional array

  16. 16

    Get values from array and using the values in a program

  17. 17

    How to split delimiter array values into another Array

  18. 18

    How to add additional values in to array

  19. 19

    How to return same values of the array

  20. 20

    How to store multiple values in array

  21. 21

    Angular takes Array as object and does not display the values

  22. 22

    Angular - filter an array of objects based on values in another array of objects

  23. 23

    How to get size of array embedded array mongoose?

  24. 24

    How to get array of array value count?

  25. 25

    AJAX: How to get array on multi dimensional array

  26. 26

    Get Highest value of Unique Values in Object Array

  27. 27

    get child array values in a parent div element

  28. 28

    Is there a way to get an array of the values of the properties in an object?

  29. 29

    Get distinct values of child array as additional property

ホットタグ

アーカイブ