Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Difficulty to close a modal window

Hello,

I'm using Element-Plus, VueJS 3, InertiaJS and I'm trying to use Inertia form helper to manage the post and put requests to the server --- I want to try to use more native InertiaJS features ---.

So I have changed my axios post and put requests with inertia ones via the form helper.

The scenario is this one : I have a list (datatable) and a modal window. To open the modal window, I click on a button which set the visibility of the modal to true. I fill the fields and I click on the save button which sends a request to the back to save or update the model. And the back redirects to the list. In this situation I can't close my modal window because the redirection reloads the list and the visibility property for the modal window doesn't exist any more in the new instance of the view (I get an error message saying that dialogVisible doesn't exist).

With axios it was a bit easier to do, I handled the reload from the front.

What could be the solution to use the back redirection and close the modal window ?

Here is my code.

// The list opens the window

function create() {
	dialogVisible.value = true
}

function edit(id) {
	roomId.value = id
	dialogVisible.value = true
}

// The list listens for the close event from the modal window (the modal window is a child component inside the list component)

function close() {
	dialogVisible.value = false
	roomId.value = null
}

// The form sends the close event

function submit() {
  if (props.roomId) {
    form.put('/rooms/'+props.roomId, {
      onSuccess: () => {
        close()
      },
      onError: () => {
        errors.value = error.response.data.errors
      }
    })
  } else {
    form.post('/rooms/', {
      onSuccess: () => {
        close()
      },
      onError: () => {
        errors.value = error.response.data.errors
      }
    })
  }
}

Is it possible to do what I want or the best way is anyway to use axios to handle the post and put requests in order to handle correctly the modal window ?

Thanks for your help.

V

0 likes
4 replies
Hiz's avatar

Hey I'm not at home in vue 3 and element plus. But I'm wondering if you could close the dialog using a template ref.

But what would happen if you try this?

1 . Add a ref="modal" to the el-dialog component

  1. Define the ref()

  2. Close the dialog through the ref after the call

<template>

  <el-dialog
<!-- add a template ref to the dialog component -->
            ref="modal"
    v-model="dialogVisible"
    title="Tips"
    width="30%"
    :before-close="handleClose"
  >
    <span>This is a message</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">Cancel</el-button>
        <el-button type="primary" @click="dialogVisible = false">
          Confirm
        </el-button>
      </span>
        <button @click="test">Hello</button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessageBox } from 'element-plus'

const dialogVisible = ref(false)

// Define the template ref
const modal = ref(null)

// Close the modal through the template ref
const closeTroughRef = () => {
  modal.value.visible = false
}

// After handling the request call closeTroughRef()
</script>

example: https://element-plus.run/#eyJBcHAudnVlIjoiPHRlbXBsYXRlPlxuICA8ZWwtYnV0dG9uIHRleHQgQGNsaWNrPVwiZGlhbG9nVmlzaWJsZSA9IHRydWVcIj5cbiAgICBjbGljayB0byBvcGVuIHRoZSBEaWFsb2dcbiAgPC9lbC1idXR0b24+XG5cbiAgPGVsLWRpYWxvZ1xuICAgICAgICAgICAgcmVmPVwibW9kYWxcIlxuICAgIHYtbW9kZWw9XCJkaWFsb2dWaXNpYmxlXCJcbiAgICB0aXRsZT1cIlRpcHNcIlxuICAgIHdpZHRoPVwiMzAlXCJcbiAgICA6YmVmb3JlLWNsb3NlPVwiaGFuZGxlQ2xvc2VcIlxuICA+XG4gICAgPHNwYW4+VGhpcyBpcyBhIG1lc3NhZ2U8L3NwYW4+XG4gICAgPHRlbXBsYXRlICNmb290ZXI+XG4gICAgICA8c3BhbiBjbGFzcz1cImRpYWxvZy1mb290ZXJcIj5cbiAgICAgICAgPGVsLWJ1dHRvbiBAY2xpY2s9XCJkaWFsb2dWaXNpYmxlID0gZmFsc2VcIj5DYW5jZWw8L2VsLWJ1dHRvbj5cbiAgICAgICAgPGVsLWJ1dHRvbiB0eXBlPVwicHJpbWFyeVwiIEBjbGljaz1cImRpYWxvZ1Zpc2libGUgPSBmYWxzZVwiPlxuICAgICAgICAgIENvbmZpcm1cbiAgICAgICAgPC9lbC1idXR0b24+XG4gICAgICA8L3NwYW4+XG4gICAgICAgIDxidXR0b24gQGNsaWNrPVwidGVzdFwiPkhlbGxvPC9idXR0b24+XG4gICAgPC90ZW1wbGF0ZT5cbiAgPC9lbC1kaWFsb2c+XG48L3RlbXBsYXRlPlxuXG48c2NyaXB0IGxhbmc9XCJ0c1wiIHNldHVwPlxuaW1wb3J0IHsgcmVmIH0gZnJvbSAndnVlJ1xuaW1wb3J0IHsgRWxNZXNzYWdlQm94IH0gZnJvbSAnZWxlbWVudC1wbHVzJ1xuXG5jb25zdCBkaWFsb2dWaXNpYmxlID0gcmVmKGZhbHNlKVxuXG5jb25zdCBtb2RhbCA9IHJlZihudWxsKVxuXG5jb25zdCB0ZXN0ID0gKCkgPT4ge1xuICBjb25zb2xlLmxvZyhtb2RhbC52YWx1ZS52aXNpYmxlID0gZmFsc2UpXG59XG5cbmNvbnN0IGhhbmRsZUNsb3NlID0gKGRvbmU6ICgpID0+IHZvaWQpID0+IHtcbiAgRWxNZXNzYWdlQm94LmNvbmZpcm0oJ0FyZSB5b3Ugc3VyZSB0byBjbG9zZSB0aGlzIGRpYWxvZz8nKVxuICAgIC50aGVuKCgpID0+IHtcbiAgICAgIGRvbmUoKVxuICAgIH0pXG4gICAgLmNhdGNoKCgpID0+IHtcbiAgICAgIC8vIGNhdGNoIGVycm9yXG4gICAgfSlcbn1cbjwvc2NyaXB0PlxuPHN0eWxlIHNjb3BlZD5cbi5kaWFsb2ctZm9vdGVyIGJ1dHRvbjpmaXJzdC1jaGlsZCB7XG4gIG1hcmdpbi1yaWdodDogMTBweDtcbn1cbjwvc3R5bGU+XG4iLCJpbXBvcnRfbWFwLmpzb24iOiJ7XG4gIFwiaW1wb3J0c1wiOiB7fVxufSIsIl9vIjp7fX0=

1 like
vincent15000's avatar

@Hiz Thank you ... here you suggest me to close the modal from the modal itself.

I just tested your suggestion and ... great ... it works ... but now the modal, once closed, doesn't want to open once again.

vincent15000's avatar

I just found this.

If I don't do any redirection, by default InertiaJS reloads the previous view.

In this case the modal window closes, but I have this error yet.

ReferenceError: dialogVisible is not defined
    at j.onUpdate:modelValue.e.<computed>.e.<computed>
vincent15000's avatar

I have solved the problem this way.

In the parent component, I have created a showModal ref property.

I share this property as a props with the child component (the modal window) and I open the modal by changing the value of showModal from false to true.

In the child component I have a dialogVisible ref property to handle the visibility of the modal. I initialize this property with the props showModal and I watch for any change of the showModal, each time it changes, I update the dialogVisible value.

And for closing the modal, I close it from the modal (and not from the parent any more) by changing directly the value of dialogVisible.

I don't use any template ref.

Please or to participate in this conversation.