q-tree : how to validate a selection? #18204
Unanswered
Zambiorix
asked this question in
General - Components / Directives / etc
Replies: 1 comment
-
|
QTree does not have a built-in Here's a pattern that works: <template>
<q-tree
:nodes="nodes"
node-key="id"
v-model:selected="selectedNode"
@update:selected="onSelectionChange"
/>
<q-dialog v-model="showConfirm">
<q-card>
<q-card-section>You have unsaved changes. Leave anyway?</q-card-section>
<q-card-actions align="right">
<q-btn flat label="Stay" @click="revertSelection" />
<q-btn flat label="Leave" @click="confirmSelection" />
</q-card-actions>
</q-card>
</q-dialog>
</template>
<script setup>
import { ref } from "vue";
const selectedNode = ref("page-1");
const previousNode = ref("page-1");
const pendingNode = ref(null);
const showConfirm = ref(false);
const hasUnsavedChanges = ref(false);
function onSelectionChange(newKey) {
if (hasUnsavedChanges.value && newKey !== previousNode.value) {
pendingNode.value = newKey;
// Immediately revert — the dialog will decide what happens
selectedNode.value = previousNode.value;
showConfirm.value = true;
} else {
previousNode.value = newKey;
}
}
function confirmSelection() {
hasUnsavedChanges.value = false;
selectedNode.value = pendingNode.value;
previousNode.value = pendingNode.value;
showConfirm.value = false;
}
function revertSelection() {
pendingNode.value = null;
showConfirm.value = false;
}
</script>The key insight: when the user clicks a new node, you immediately set This gives you full control over navigation guards without needing a |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am using a q-tree in my drawer to navigate through a set of pages.
How can I prevent the change of the selected node?
For example, if my page has some unsaved changes, I want to show a dialog, instead of navigating away from that page.
Thank you
Cheers
Gerd
Beta Was this translation helpful? Give feedback.
All reactions