Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

328
Visualizações
How to switch from graph view and tableview with a toggle button in Ionic 6/Angular?

I have two components to show data. The table view and the graph view. Both components has the same data. I use a toggle switch button to switch between these views. This is my code:

<ion-header>
  <ion-toolbar class="toolbar-color">
    <ion-title>Views</ion-title>
  </ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
  <ion-grid>
    <ion-row>
      <ion-col>
        <div class="ion-text-start">
          <ion-item>
            <ion-label>Switch to table view</ion-label>
            <ion-toggle (click)="showTable()"></ion-toggle>
          </ion-item>
        </div>
      </ion-col
    </ion-row>
    <ion-row>
      <ion-col>
        <app-tableview hidden="true" id="tableview"></app-tableview>
        <canvas height="200" #lineCanvas></canvas>
      </ion-col>
    </ion-row>
  </ion-grid>
</ion-content>

The default view is the graphview via chart.js. Both components works fine. This is my code to hide and show:

showTable() {
    if (this.tableHidden === true) {

      this.tableHidden = false;
      document.getElementById("tableview").hidden = false;
      

    } else if (this.tableHidden === false) {

      this.tableHidden = true;    
      document.getElementById("tableview").hidden = false; 
   
    }

  }

When I click on the toggle switch it doesn't hide the graphview but it shows the table view among each other. I have tried the visibility from this post but doesn't work.

JavaScript hide/show element

How can I switch between the components and show only view?

JSFiddle link: https://jsfiddle.net/f8a3wgxb/

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

An example based off @alex87's comment:

<ng-container *ngIf="showTableView">
    <app-tableview id="tableview"></app-tableview>
</ng-container>
<ng-container *ngIf="!showTableView">
    <canvas height="200" #lineCanvas></canvas>
</ng-container>

Doesn't have to be an ng-container, but that Angular-only tag will not add anything to the DOM. You can always use a or anything else to do just the same... You can also neaten it up by using an *ngIfElse... https://angular.io/api/common/NgIf

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda