[Solved] How to use q-toggle with @input and v-model (was: with @input and value)

  • I’m successfully using q-toggles to control the visibility of q-table columns. As explained in one of the q-table doc examples, this is done by having the toggles control an array that holds values that correspond to the table columns and their visibility.

    Those toggles work like this.

                          <div class="col-2">

    This works successfully. Now I wanted to have a new toggle that controls the visiblity of multiple table columns. I have already successfully implemented this with a q-button, that, upon an @click event, executes a method that manipulates the above array (visibleColumns…) that controls the table column visibility.

    Now I wanted to do the same with a q-toggle. I see in the docs that the q-toggle does not react to @click, but to @input.

    So I’m trying this:

                          <div class="col-2">
                              label="Alle/keine Normkategorien"

    When I click on that new toggle, the method toggleNormkategorien is called. That method successfully manipulates the above array, so the control over table column visibility is handled correctly. That method explicitly inverts/toggles the (Boolean) value of toggleAllNormkategorienState before it returns.

    The remaining problem is however that the new toggle does not switch from unticked to ticked and vice versa. Although the rest of it works, it always remains in its initial “unticked” appearance.

    Do I have to trigger this in my own code, and, if yes, how?

    I already tried at the end of my @input method toggleNormkategorien the following:

    this.$refs.toggleAllNormkategorien.toggle(); // Leads to an infinite loop!!!

    which however leads to an infinite loop at runtime. Not sure which other options I have over the toggle “ticking”. Thanks for advise.

  • Can you show your toggleNormkategorien method?


  • @Mickey58 you can chain the statement in your input event or do the value change inside the method. Ie @input="toggleNormkategorien, toggleAllNormkategorienState!=toggleAllNormkategorienState".

  • Yeah, that’s what I wanted to make sure is missing. 😁

    In other words @Mickey58, if you use the value and @input, you are then responsible for keeping the value up-to-date.


  • Thanks. Indeed, I had the inverting of the Boolean variable already in my toggleNormkategorien method. I see in the logs that it is correctly “flip/flopped” - both in the case when I click on the q-button that uses it in its v-model, and when I click on the (“non-ticking”) q-toggle that I would like to get working to replace the q-button.

    Here is the code of that method:

    toggleNormkategorien() {
          if (!this.toggleAllNormkategorienState) {
              // Adds 10 "Normkategorien" to control q-table column visibility at end of array 
              "In toggleNormkategorien mit Toggle auf EIN - visibleColumnsBaubestimmungen:",
          } else { // filter out those values that control column visibility from the array,
              // Filtering is needed because the array has other values in addition that can be toggled
             // and added at the end of the array
              this.visibleColumnsBaubestimmungen = this.visibleColumnsBaubestimmungen.filter(
              value =>
                value !== "grundNorm" &&
                value !== "terminologieNorm" &&
                value !== "prüfNorm" &&
                value !== "produktNorm" &&
                value !== "verfahrensNorm" &&
                value !== "dienstleistungsNorm" &&
                value !== "schnittstellenNorm" &&
                value !== "deklarationsNorm" &&
                value !== "fachbereichsNorm" &&
                value !== "werkNorm"
              "In toggleNormkategorien mit Toggle auf AUS - visibleColumnsBaubestimmungen:",
          this.toggleAllNormkategorienState = !this.toggleAllNormkategorienState;
            "Am Ende von toggleNormKategorien - toggleAllNormkategorienState neu: ",
          // this.$refs.toggleAllNormkategorien.toggle(); // leads to infinite loop!!!
          // await this.$nextTick(); // needs async function above....but no effect
          // console.log("After next tick");

    When I chain the toggleAllNormkategorienState = !this.toggleAllNormkategorienState directly in the @input of the q-toggle, it has no effect on q-toggle ticking. So the problem is still there.

  • Actually, just stick with v-model and use @input to trigger what you need to do in your component. You can use the “value” given as the @input parameter to check the state of the toggle.


    I hope that solves the predicament here. 😃


  • Wow…so I tried this combination of v-model=“toggleAllNormkategorienState” and @input=“toggleNormkategorien”…which is in my view a bit “creative”…at least not what I would have expected from the docs 😉

    On the first try, I got disappointed because it did not immediately work for me (neither control for table column visibility nor the ticking of q-toggle, worse than my original combination of @input= and value=).

    But the additional trick to get this combination to work was to REMOVE the inverting of the toggleAllNormkategorienState from the toggleAllNormkategorien method. So, with that super-creative combination, it seems to WORK!!! Still need to a bit more testing to be 100% sure.

    Thanks a lot for your always competent and quick help.

Log in to reply