Name does not get fixed in variable

Asked

Viewed 52 times

1

Good morning! I have a survey that puts the names of some products in a variable, but if I search another product, it subscribes the new product in the old, so:

Search for 1 product:

inserir a descrição da imagem aqui

Search for 2° product:

inserir a descrição da imagem aqui

As you can see, it replaces the value of the second of the first, and I even understand why, but I don’t know how to fix... Note: is just the name of the product.

Product Search Code:

 case 'nomeProduto':
   params = params.set('cod', formGroup.controls['codigoProduto'].value);
   this.retornaNomeApi(NEGOCIUS_API + '/Pesquisa/RetornaNome', params).subscribe(nome => formGroup.patchValue({ nomeProduto: nome.result }));
   break;

Página ts:

  Codigo: any;
  digitacaoForm: FormGroup;
  itens: ItemPedidoModel[] = [];
  nomeProd: Produto[] = [];
    ngOnInit() {
    this.sub = this.route
      .queryParams
      .subscribe(params => {
        this.type = params['type'];
      });
    this.digitacaoForm = this.fb.group({
      pedido: this.fb.control('', Validators.required),
      codigoCliente: this.fb.control('', Validators.required),
      nomeCliente: this.fb.control({ value: '', disabled: true }),
      rep: this.fb.control('Selecione...', Validators.required),
      tipoped: this.fb.control('Selecione...', Validators.required),
      prazo: this.fb.control('Selecione...', Validators.required),
      forma: this.fb.control('Selecione...', Validators.required),
      codigoProduto: this.fb.control('', Validators.required),
      nomeProduto: this.fb.control({ value: '', disabled: true }, Validators.required),
      Quantidade: this.fb.control('', Validators.required),
      precoTabela: this.fb.control({ value: '', disabled: true }, Validators.required),
      PrecoDigitado: this.fb.control('', Validators.required),
      precoTotal: this.fb.control({ value: '', disabled: true }, Validators.required),
    });
  }

  retornaApi(url: string) {
    return this.http.get<any>(url);
  }

  GerarCodigo() {
    this.retornaApi(API + '/Pedido/GerarCodigoPedido').subscribe(codigo => (this.Codigo = codigo))
  }

  pegaNome(formGroup: FormGroup) {
    this.nomeProd = Object.assign('', formGroup.controls['nomeProduto'].value);
    return this.nomeProd;
  }

  excluirLinha(codigo: any) {
    for (let item of this.itens) {
      if (item.codigoProduto == codigo) {
        this.itens.splice(this.itens.indexOf(item), 1);
        break;
      }
    }
  }

HTML of the table:

<div class="col-md-12">
  <div *ngIf="itens.length" class="table-responsive">
    <table class="table">
      <thead class="thead-dark">
        <tr>
          <th></th>
          <th class="text-center">Código</th>
          <th class="text-center">Descrição</th>
          <th class="text-center">Quantidade</th>
          <th class="text-center">Preço Digitado</th>
          <th class="text-center">Preço Total</th>
        </tr>
      </thead>
      <tbody>
        <tr *ngFor="let item of itens">
          <th class="text-center">
            <button type="button" class="btn btn-danger" (click)="excluirLinha(item.codigoProduto)">
              <i class="glyphicon glyphicon-remove"></i>
            </button>
          </th>
          <td class="text-center">{{item.codigoProduto}}</td>
          <td class="text-center">{{nomeProd}}</td>
          <td class="text-center">{{item.Quantidade}}x</td>
          <td class="text-center">{{item.PrecoDigitado | currency: 'BRL': true}}</td>
          <td class="text-center"></td>
        </tr>
      </tbody>
    </table>
  </div>
</div>
  • It has how to post the whole code and html tbm?

  • @Eduardovargas I added the code of the table, I would need some more code?

  • Put ts whole for example you are doing for in this items and don’t have it on ts.

  • @Eduardovargas actually, the items won’t have much to do with what I’m trying to solve, they’ll just be passed by parameter, with no link to the product name, where is the problem

1 answer

1

Try to copy the string value instead of passing the reference

 this.nomeProd = Object.assign('', formGroup.controls['nomeProduto'].value);
  • i tried, that way, however the product name no longer appears in the table, would have to do something else outside that?

  • It is difficult to know that you are assigning a string to a value that expects an array nameProd: Product[] = []; stickName(formGroup: Formgroup) { Return formGroup.Controls['productname']. value;//would be better like this }

  • If I switch to string, it would be right to just call?

  • I don’t know I need to see the rest of the code

  • I put all the code

  • You’d have to model?

  • could see the code and give me some answer?

  • I don’t know, I didn’t see what you call the handle

  • I thought like this, the model that receives the value of the item is Itempedidomodel, and in this model has a field called "product name", I would like to put the result of this my research to this field?

  • but if you have a search field and several items all will always have the same product nameProduct because in your logic only has a product nameProduct.

  • Voce has to use push to have an array of nameProd. Ai with your for vc index can access it.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.