Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run method when Vuetify dialog is opened

I have a v-data-table and the user can click on any row and a dialog opens. Inside on my vuetify dialog is a dropdown of data.

I want to filter this data everytime the user clicks on a row and filter out what the user clicked from the dropdown inside the dialog.

Here is my dialog:

       <v-dialog v-model="edit" max-width="1200"  @input="closeDialog()">
            <editCompany :isEdit="true"
                         v-if="showEdit"
                         :company="selected"
                         :adminEdit="true"></editCompany>
          </v-dialog>

You can see I'm passing in the values from the table row the user clicked.

Now, I need to use the value being passed in to filter the dropdown. The mounted event only runs once, so my logic inside of it only fires for the first row clicked.

Here is the mounted event inside of my editCompany template:

     mounted: async function () {
        this.filterOutResults(); //this is where i do my filtering and it works
       },

Every other row the user clicks doesn't fire mounted, so I cant use that unless I can unmount the dialog when its closed.

I found how to fire an event when the dialog closes, but I cannot find a vuetify open event.

How do I run a function everytime the dialog opens so I can filter the results or how do I unmount the dialog everytime it closes, so the mounted event can run everytime? Thanks

like image 627
BoundForGlory Avatar asked Jan 02 '20 14:01

BoundForGlory


People also ask

Is Vuetify responsive?

With the VuetifyJs grid system, you can create very powerful responsive layouts without any line of JavaScript code.

Does Vuetify 2 work with vue3?

The current version of Vuetify does not support Vue 3.

How do I close dialog in Vuetify?

By default, dialog can be closed by pressing Esc key and clicking the close icon on the right of dialog header.

Can you use Vuetify without Vue?

No, you can't run Vuetify without Vue. The reason is pretty simple, most of Vuetify is built with vue and most of those components require their script to run, so everything that's not entirely css based will not work.


2 Answers

For future references, I'll expand @mynd comment, which should be the answer:

export default {
  data() {
    return {
      dialogVisible: false,
    },
  },
  watch: {
    dialogVisible(visible) {
      if (visible) {
        // Here you would put something to happen when dialog opens up
        console.log("Dialog was opened!")
      } else {
        console.log("Dialog was closed!")
      }
    }
  },
<v-dialog v-model="dialogVisible" max-width="1200" @input="closeDialog()">
  <!-- Add code here, according to Vuetify docs -->
</v-dialog>

For further information (about constructing the v-dialog component itself), refer to the official docs

like image 140
Tzahi Leh Avatar answered Nov 15 '22 10:11

Tzahi Leh


if you want to do something inside the dialog component when it's open, there is a little workaround that you can do.

 <v-dialog v-model="edit" max-width="1200"  @input="closeDialog()">
        <editCompany :isEdit="true"
                     v-if="showEdit"
                     :company="selected"
                     :dialogOpen="edit"
                     :adminEdit="true"></editCompany>
      </v-dialog>

as you see you can pass dialog's handler variable as dialog's parameter. its 'dialogOpen' in this case.

then inside editCompany component,

watch: {
    'dialogOpen' :{
      handler(newVal, oldVal) {
       //do your stuff.
      },
      deep: true
    },
}

So in short it will watch the variable which is controlling dialog, based on its value which mostly is true or false, you can do your operation.

like image 23
Juhil Kamothi Avatar answered Nov 15 '22 12:11

Juhil Kamothi