No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Using checkbox array with objects and mark selected

    Help
    2
    4
    302
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • alhidalgodev
      alhidalgodev last edited by alhidalgodev

      I have an array of objects and I would like to mark the checkboxes that are already inside my array "selected:

      Example code;

        data () {
          return {
            selection: [ {name: 'teal'}, {name: 'red'} ]
          }
        }
      
      <div id="q-app">
        <div class="q-pa-md">
          <div class="q-gutter-sm">
            <q-checkbox
                v-for="(option, index) in options"
                v-model="selection" 
                :val="option"
                :label="option.label"
                :color="option.label"></q-checkbox>
          </div>
      
          <div class="q-px-sm">
            The model data: <strong>{{ selection }}</strong>
          </div>
        </div>
      </div>
      

      What am I waiting for?

      33c105e3-75a4-4c63-8e45-f83c9c310987-image.png

      What’s going on ?

      979f2a61-3bfb-4ea9-9364-0ed01e2fb61d-image.png

      Example: https://codepen.io/itsalb3rt/pen/oNYZNrZ?editors=101

      beets 1 Reply Last reply Reply Quote 0
      • beets
        beets @alhidalgodev last edited by

        @alhidalgodev The tricky thing, is that the objects in selection are not equal to the objects in options. They need to pass an equality test (i.e. ==) in order to be considered selected. This will depend on how you want to define the pre-selected options, if it’s hardcoded or from vuex / api. See this example for the most basic idea:
        https://codepen.io/pianopronto/pen/wvoJqLV?editors=1010

        alhidalgodev 1 Reply Last reply Reply Quote 1
        • beets
          beets last edited by

          Another option, where we store selected right in the options object, and optionally use a computed variable to turn it back to what you want: https://codepen.io/pianopronto/pen/XWNMeWg?editors=101

          1 Reply Last reply Reply Quote 1
          • alhidalgodev
            alhidalgodev @beets last edited by

            @beets Thanks a lot!!! you are my hero!

            1 Reply Last reply Reply Quote 0
            • First post
              Last post