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

    Checkbox using array of objects: how to determine which 'object' was clicked

    Help
    3
    4
    1011
    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.
    • S
      sfdlkelkas last edited by

      I want to render a list of objects that contain various fields. One of the fields is a boolean value, that needs to be rendered as a checkbox

      In order to update the state, an API request needs to be made, containing the object.id.

      <q-item v-for="thing in things">
        <q-item-section>
          <q-checkbox v-bind:id="thing.id" v-model="thing.is_complete" v-bind:name="thing.name" @input="setThingCompleteMethod" />
        </q-item-section>
      </q-item>
      
      <q-item-section>
         <q-item-label>{{ thing.name }}</q-item-label>
          <q-item-label caption>
              {{ thing.id }}
          </q-item-label>
      </q-item-section>
      

      In normal Vue it’s possible to bind the “id” tag along with v-model, then the @click method will be able to access the id via the event.target.id property.

      Anyway, in quasar the @input method has ‘event’ and ‘value’ parameters, but they don’t contain any usable way of determining which object has actually been clicked.

      The event object has the ‘id’, but it’s several nodes above the ‘target’, and even then it appears to be only present when the checkbox value is ‘true’ and not ‘false’…

      event.target.parentNode.parentNode.parentNode.id …

      What’s the best way to do this?

      metalsadman 1 Reply Last reply Reply Quote 0
      • metalsadman
        metalsadman @sfdlkelkas last edited by metalsadman

        @sfdlkelkas pass something as parameter on the event handler. Ie. @input="v=>{handler(v, item.id, otherparams)}", same method can be used for all events.

        S 1 Reply Last reply Reply Quote 0
        • S
          sfdlkelkas @metalsadman last edited by

          @sfdlkelkas pass something as parameter on the event handler. Ie. @input="v=>{handler(v, item.id, otherparams)}", same method can be used for all events.

          Ok, wow. That’s great! I really had no idea that was possible. The documentation for that event mentions nothing of that feature:

          @input -> function(value, evt)
          

          Thanks

          dobbel 1 Reply Last reply Reply Quote 0
          • dobbel
            dobbel @sfdlkelkas last edited by

            @sfdlkelkas

            @input -> function(value, evt)

            This means the @input event callback expects max 2 params. ( value and evt)

            But be aware that with this line:
            @input="v=>{handler(v, item.id, otherparams)}"
            your are only using first parameter(value) of the @click event callback.

            To use both params:
            @input="(v,evt)=>{handler(v, item.id, otherparams, evt)}"

            So if the api doc for an event looks like this ( 3 params):
            @input -> function(newValue, oldValue, evt)
            you can do:
            @input="(oldValue,newValue, evt)=>{handler(oldValue, newValue, ect....

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