Home > Net >  Compare two values of different rows of an array in one table Angular
Compare two values of different rows of an array in one table Angular

Time:12-06

I want to compare 2 values that are in different rows. The idea is that when selecting each row with the mat-checkbox, I obtain the data I need and the following condition is met: if the data in the "tipo de gasto" column is different, the data in the "insumo" column must be the same, otherwise, if the "tipo de gasto" is different but the "insumo" is not the same, an error must be generated when pressing the button to perform this function.

I'm doing it with a "for" to go through the entire array, the problem is that it is passing straight and it is not taking the condition into account.

HTML:

The button is disabled until at least 1 mat-checkbox has been selected. When I click, I want the condition to be performed. And if it possible deselect the mat-checkbox that was selected.

<button
  mat-raised-button
  class="solicitarCdp"
  [disabled]="comprobarFilas()"
  (click)="procesarClic()"
>
  Solicitar CDP
</button>

TS

Request has the entire array that I am analyzing.

procesarClic() {
  const request = this.selection.selected;    
  for (let i = 0; i < request.length; i  ){
    let insumoFila = request[i].insumo;
    let tipoGastoFila = request[i].tipoGasto;
    if(insumoFila[i] !== insumoFila[i  ] && tipoGastoFila[i] !== tipoGastoFila[i  ]) {
      window.alert("Los insumos no coinciden");
      return request
    }
  }    
  const newTable = this.dialog.open(SolicitudCdpDialog, {
    width: '400px',
    disableClose: true,
    data: { datosItem:request }
  });    
}

Thank you very much for your help!

CodePudding user response:

Using Set to see if all are same or all are different.

procesarClic() {
  const request = this.selection.selected;
  // assuming that these two columns has either number or string in them.(not reference type)

  // Set will only have unique values
  let tipoGastoSet = new Set(request.map(c => c.tipoGastoFila));
  let insumo = new Set(request.map(c => c.insumo));

  // check if all tipoGastoSet are same, size 1 , and insumo should be size of request if all are different
  if (request.length === 2  &&  tipoGastoSet.size === 2 && insumo.size !== 1) {
    window.alert("Los insumos is not differnt");
  }
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Thanks for your help Vaira. I was making some adjustments and I found the answer: since I have to compare 2 columns between 2 rows, I create some constants and I create 3 conditions: if the "tipo de gasto" is different between the 2 rows and the "insumo" is different between the 2 rows and the length of the request is less than 3 (I put this condition because I can select more than 2 rows) I MUST PUT the error message.

TS

procesarClic() {
    const request = this.selection.selected;
    
    for (let i = 0; i < request.length; i  ){
      const insumoFila = request[i].insumo;
      const tipoGastoFila = request[i  ].tipoGasto;
      const insumoFila2 = request[i].insumo;
      const tipoGastoFila2 = request[i  ].tipoGasto;
      if (tipoGastoFila !== tipoGastoFila2 &&
        insumoFila !== insumoFila2 &&
          request.length < 3) {
          window.alert("Los insumos no coinciden");
          return request
      }
      else {
        const newTable = this.dialog.open(SolicitudCdpDialog, {
          width: '400px',
          disableClose: true,
          data: { datosItem:request }
        });
        return newTable
      }
    }
  • Related