VueJs Dialog Reusable Component with buttons different action - javascript

I am trying to build a reusable component that called v-dialog.
The idea is that I when the the dialog pop up there will be consist of 2 buttons which is called submit and cancel.
For the submit button of dialog component will be linked with different actions based on user clicks which button.
For example button A will call function name A and button B will call function name B and so on when user clicks on submit button of the dialog.
Let's say this is a component file I called DialogReusable.vue
<v-dialog
v-model="dialog"
persistent
max-width="300"
>
<v-card>
<v-card-title
class="text-h5"
style="word-break: break-word"
>
Title
</v-card-title>
<v-card-actions>
<v-spacer />
<v-btn
color="green darken-1"
text
#click="dialog = false"
>
Cancel Button
</v-btn>
<v-btn
class="primary"
#click="functionSubmits()"
>
Submit
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
And this is the parent file that I called MainParent.vue
For this file it has like 3 buttons which link to different function.
When user clicks on each button the Dialog should appears and when user clicks on the submit button of the dialog then it will call the respective function name that I set #click on each button.
<v-btn
v-if="condition"
color="primary"
dark
#click="functionA()"
>
Function A
</v-btn>
<v-btn
v-if="condition"
class="primary"
#click="functionB()"
>
Function B
</v-btn>
<v-btn
v-if="condition"
class="primary"
#click="functionC()"
>
Function C
</v-btn>

That is the concept of passing the functions through props attribute in Vue.
Let's me show you the first example of passing the function from parent into child component.
Getting a value from the parent
If you want a child component to access a parent's method, it seems obvious to just pass the method straight down as a prop.
<!-- Parent -->
<template>
<ChildComponent :method="parentMethod" />
</template>
// Parent
export default {
methods: {
parentMethod() {
// ...
}
}
}
And you child component would be like:
// Child
export default {
props: {
method: { type: Function },
},
mounted() {
// Use the parent function directly here
this.method();
}
}
Getting a value from the child
Other case you might want to get a value from child into parent component, for example the function in parent component has a param name.
<!-- Parent -->
<template>
<ChildComponent :method="parentMethod" />
</template>
// Parent
export default {
methods: {
parentMethod(valueFromChild) {
// Do something with the value
console.log('From the child:', valueFromChild);
}
}
}
Where in the child component you pass the value in when calling it:
// Child
export default {
props: {
method: { type: Function },
},
mounted() {
// Pass a value to the parent through the function
this.method("some param name");
}
}
Regarding you question, I think you can achieve by the following:
Let's say you have 3 submit buttons and each button has different action.
First let's create the reusable pop up dialog that acts like a child component.
<template>
<v-dialog
:value="dialog"
persistent
max-width="300"
#input="$emit('input', $event)"
>
<v-card>
<v-card-title
class="text-h5"
style="word-break: break-word"
>
Title Dialog
</v-card-title>
<v-card-actions>
<v-spacer />
<v-btn
color="green darken-1"
text
#click="close"
>
Cancel
</v-btn>
<v-btn
class="primary"
#click="onBtnSubmit"
>
Confirm
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</template>
<script>
export default {
name: 'Dialog',
props: {
dialog: {
type: Boolean,
required: true
},
buttonActionName: {
type: String,
required: true
},
method: {
type: Function,
required: true
}
},
methods: {
close() {
this.$emit('close-dialog')
},
onReportBtnSubmit() {
if (this.buttonActionName.length > 0) {
this.method(this.buttonActionName)
} else {
this.method()
}
this.close()
}
}
}
</script>
<style scoped>
</style>
I assumed that you understand the concept of passing the functions through props attribute in Vue.
// This dialog will pass function `firstBtnSubmit` without param name
<Dialog
:dialog.sync="firstBtnDialog"
:method="firstBtnSubmit"
button-action-name=""
#close-dialog="firstBtnDialog= false;"
/>
<v-btn
#click="showDialogFirstBtn">
firstBtnClick
</v-btn>
// This dialog will pass function `secondBtnSubmit` with param name
<Dialog
:dialog.sync="secondBtnDialog"
:method="secondBtnSubmit"
button-action-name="id1"
#close-dialog="secondBtnDialog = false;"
/>
<v-btn
#click="showDialogSecBtn">
secondBtnClick
</v-btn>
// you can create a third button below by yourself.
export default {
data() {
return {
firstBtnDialog: false,
secondBtnDialog: false,
thirdBtnDialog: false,
},
methods: {
firstBtnSubmit() {
// Do your stuff
},
secondBtnSubmit(param) {
// do your staff with param
},
thirdBtnSubmit () {
// do your stuff
},
// To trigger dialog to pop up
showDialogFirstBtn() {
this.firstBtnDialog = true;
},
showDialogSecBtn() {
this.secondBtnDialog= true;
},
showDialogThirdBtn() {
this.thirdBtnDialog= true;
}
}
}
Note
I do NOT own all contents that I wrote here.
All contents I got from different site
Concept to pass function as props
How to use reusable component in Vue: Stackoverflow

Related

Vue.js & vuex handling SMS by server-side-events

I have a app, which is basically a Call centrum. You can receive calls, call to someone, receive sms and send sms etc.. I have problem with showing my SMS on screen, when I receive event from backend, I am showing that data on screen using vuex and V-for for specific component. Problem is that when I receive another event from backend with different number, I would like to show it under that first sms, but it will overwrite first sms and show only that new sms. I was trying multiple approaches, but nothing worked for me so I hope someone will be able to show me my mistake.
Here is photo of screen with one sms (red box is where second sms should be with own informations like number...)..
Here is code where I receive events.
export default function setupStream(){
let evtSource = new EventSource('/hzs/events.sse');
evtSource.addEventListener('receive_sms', event => {
let sms_data = JSON.parse(event.data);
store.dispatch('receiveSMS', sms_data);
}, false)
}
Here is my vuex code
const state = {
sms: [],
};
const getters = {
getSMS: (state) => state.sms,
};
const actions = {
receiveSMS({ commit }, sms_data) {
commit('setSMS', sms_data);
},
};
const mutations = {
setSMS: (state, sms) => (state.sms = sms),
};
export default {
state,
getters,
actions,
mutations
}
And here is component.
<template>
<v-card>
<v-card-title class="primary white--text">
{{ $t("Communication") }}
</v-card-title>
<v-card d-flex flex-column height="100%" class="card-outter scroll">
<v-col>
<div v-for="sms in getSMS" :key="sms.id">
<v-card-actions>
<v-row>
<v-btn #click="openChat" icon class="mt-4"
><v-img
max-width="30px"
max-height="30px"
class="mt-2"
src="#/assets/icons/icon-sms.svg"
alt="icon-sms"
/></v-btn>
<v-col>
<span>{{sms.date_time}}</span> <br />
<h4>{{sms.sender}}</h4>
<!-- Dialog for Adding new Note -->
<v-dialog
v-model="showEditor"
max-width="400px"
persistent
scrollable
>
<template v-slot:activator="{ on, attrs }">
<v-btn
#click="showEditor = true"
depressed
small
v-bind="attrs"
v-on="on"
>{{$t("Add Note")}}</v-btn
>
</template>
<AddNoteDialog v-on:close-card="showEditor = false"
/></v-dialog>
</v-col>
<v-spacer></v-spacer>
<v-btn class="mt-5" icon #click="deleteCommunication"
><v-img
max-width="20px"
src="#/assets/icons/icon-delete.svg"
alt="icon-delete"
/></v-btn>
</v-row>
</v-card-actions>
<v-divider></v-divider>
</div>
<v-spacer></v-spacer>
<v-divider></v-divider>
<v-card-actions class="card-actions">
<v-row>
<v-text-field
class="ml-4"
color="primary white--text"
required
:label="$t('Mobile number')"
clearable
></v-text-field>
<v-dialog
v-model="showEditor1"
max-width="450px"
persistent
scrollable
>
<template v-slot:activator="{ on, attrs }">
<v-btn
#click="showEditor1 = true"
class="mt-5 mr-4"
depressed
icon
v-bind="attrs"
v-on="on"
><v-icon>mdi-plus-circle</v-icon></v-btn
>
</template>
<AddNummberDialog v-on:close-card="showEditor1 = false"
/></v-dialog>
</v-row>
</v-card-actions>
</v-col>
</v-card>
</v-card>
</template>
<script>
import AddNoteDialog from "#/components/UI/AddNoteDialog";
import AddNummberDialog from "#/components/UI/AddNummberDialog";
import { mapGetters, mapActions } from 'vuex';
export default {
name: "Communication",
data() {
return {
dialog: false,
showEditor: false,
showEditor1: false,
note: '',
chat: this.switchChat,
};
},
computed: {
...mapGetters(['getSMS']),
},
components: { AddNoteDialog, AddNummberDialog },
props: ["switchChat"],
methods: {
...mapActions(['setupEvents']),
openChat() {
this.$emit('openChat')
},
async deleteCommunication() {
alert("Deleted");
},
},
};
</script>
<style>
.scroll {
overflow-y: scroll;
}
.card-outter {
padding-bottom: 50px;
}
.card-actions {
position: absolute;
bottom: 0;
width: 100%;
}
</style>
I think that solution is creating new array, where I will store every single SMS that I receive. Problem is that I don't know how and where to do it.
You already have your vue state array called sms which is a good start. You'll need to update your Vuex to have an additional mutation called "addNewSMS" or something:
const mutations = {
setSMS: (state, sms) => (state.sms = sms),
addNewSMS: (state, newSMS) => state.sms.push(newSMS),
};
This will update your state.sms array to include more than one element, which you should be able to loop through using a v-for loop in your template.
Of course, you'll also need to update your actions like this:
const actions = {
receiveSMS({ commit }, sms_data) {
commit('addNewSMS', sms_data);
},
};
As a sidenote, I'd personally change the sms variable name to messages so its clearer to you and other coders that it contains multiple objects.

move dialog to a compontent in Vuetify

here's my code:
<v-btn #click="showDialog = true" >
<v-dialog
v-model="showDialog"
max-width="600"
>
<v-btn #click="showDialog = false">save</v-btn>
</v-dialog>
</v-btn>
I know that this is fairly simple code, but I would like to extract whole v-dialog to a component. I don't know how to manage showDialog. Should I use prop? I now I shouldn't modify prop from inside of the component. What's the other way?
You can use a function as a prop for changing value of the showDialog in the another component . ( To avoid getting error avoid mutating a prop directly )
exampleComponent.vue
<template>
<v-dialog v-model="showDialog" max-width="600">
<v-btn #click="hideDialog">save</v-btn>
</v-dialog>
</template>
<script>
export default {
props: {
showDialog : Boolean ,
hideDialog: Function,
},
};
</script>
mainFile.vue
<template>
<example :hideDialog="hideMethod" :showDialog="showDialog"></example>
</template>
<script>
export default {
data() {
return {
showDialog : false
}
},
methods: {
hideMethod() {
this.showDialog = false;
},
},
};
</script>
I've never been a fan of driving the visibility of a dialog with a reactive v-model value. Normally, dialogs need to do some amount of setup and state related things before displaying and before hiding.
So, what I do is I move showDialog to be a hidden internal value to the component itself, I put a ref= on the component, I implement an open() method on the component and call that when I want to show it.
This pattern feels more natural when a dialog is performing more complicated tasks than just showing static information.
So in your case:
<script id="myDialog" type="text/x-template">
<v-dialog
v-model="showDialog"
max-width="600"
>
<v-btn #click="save">save</v-btn>
</v-dialog>
</script>
[...]
<v-btn #click="openMyDialog">
<myDialog ref="myDialog">
</myDialog>
</v-btn>
On myDialog:
data: function () {
return {
[ other attributes ]
showDialog: false
}
},
methods: {
[ other methods ]
open: function (initializationData) {
[ initialization code ]
this.showDialog = true;
},
save: function (event) {
[ save code ]
this.showDialog = false;
}
}
On the parent component:
methods: {
[ other methods ]
openMyDialog: function (event) {
this.$refs.myDialog.open([ initialization data ]);
}
}

How to reset form which is not rendered at the moment - How to reset it the right way

I try to realise a upload page as part of a project. It's working fine but I wonder if there is a better way to reset my v-form for my inputs.
So here is what I do:
<v-container v-if="!success">
<v-flex>
<v-card>
<v-form ref="form" class="pa-5">
<v-select
...
>
</v-select>
<v-select
...
>
</v-select>
<v-text-field
...
/>
<form enctype="multipart/form-data">
<v-file-input
...
></v-file-input>
</form>
</v-form>
<v-card-actions>
<v-btn #click="addDocument">Add document</v-btn>
</v-card-actions>
</v-card>
</v-flex>
</v-container>
<v-container v-if="success">
<v-flex>
<v-card>
<v-alert v-if="notification.show" :type="notification.type" class="pa-3">
{{notification.text}}
</v-alert>
<v-card-actions>
<v-btn #click="resetPage">Okay</v-btn>
</v-card-actions>
</v-card>
</v-flex>
</v-container>
Two v-containers which will either be rendered if success is true or not. If success is false all the required inputs will be rendered and if its true (for me this means the upload was successfull) the second v-container should be rendered.
The second v-container shows just a message about the success and a "Okay" button which calls the resetPage function.
This function should reset all validations and clear all inputs.
What I've tried:
At first I tried this:
resetPage() {
this.success = false;
this.$refs.form.reset()
}
which obviously doesn't work because $refs.from is not rendered because its part of the first v-container.
So I thought I have to call the reset after it's been rendered.
So I tried this:
methods: {
resetPage() {
this.success = false;
}
},
updated() {
this.$refs.form.reset();
}
But quickly I learned that updated() is called if a input field changes or in my case one of the selects. I should have thought about this.
So I added allowReset and ended up with this:
methods: {
resetPage() {
this.success = false;
this.allowReset = true;
}
},
updated() {
if(this.allowReset) {
this.$refs.form.reset();
this.allowReset = false;
}
}
Its working like I need it to. But as I said updated() gets called everytime I select something or choose a file etc.
Is there maybe a better way to reach my goal?
Thank you in advance for all tips and tricks!
Add a v-model to your v-form. Whenever you reset the page by using this.success = false, the form should reset by itself.
When adding a v-model to a form it checks all of the rules set for the elements inside of that form. If they all pass the checks, the formValid will be true, otherwise false
simple example:
<div v-if="!success">
<v-form v-model="formValid">
<v-text-field v-model="a" :rules="[required]"></v-text-field>
</v-form>
<!-- Button will stay disabled as long as the form is invalid -->
<v-btn :disabled="!formValid">Save</v-btn>
</div>
<div v-else>
Success
</div>
And in your data:
formValid: false,
required: (v) => !!v || "Required" // Will print required if textfield is empty

passing a callback function via props to child component is undefined in child vue

I have a Vue component that passes a callback function to another child component via props. However, it is the only piece that is undefined in the child.
I have created a repo for this so the files can be looked at. In the file brDialog.vue, I am passing button to the function click(), which should have access to the buttons callback that was passed within props from App.vue, however it is undefined within brDialog while the other two things passed with it are present(label and data).
I'll post the brDialog file, and will post the others if needed, but figured it would be easier to link a repo than post all the different files. I'm a bit new to Vue, so possibly something I'm missing in the documentation.
If you run the repo and click the Form Test button in the header, this is where the issue is.
brDialog.vue
<template>
<v-container>
<v-layout row wrap>
<v-flex xs12>
<v-dialog
v-model="show"
width="500"
persistent
>
<v-card>
<v-card-title> {{ title }} </v-card-title>
<slot name="content"></slot>
<v-card-actions>
<v-btn
v-for="button in buttons"
:key="button.label"
small
#click.native="click(button)"
>
{{ button.label }}
</v-btn>
<v-btn
v-if="showCloseButton"
small
#click.native="closeDialog()"
>
{{ closeButtonLabel }}
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-flex>
</v-layout>
</v-container>
</template>
<script>
import { props } from './props.js'
export default {
name: 'brForm',
components: {
brTextField: () => import('#/controls/brTextField/brTextField.vue'),
brTextArea: () => import('#/controls/brTextArea/brTextArea.vue'),
brSelectList: () => import('#/controls/brSelectList/brSelectList.vue')
},
props: props,
data () {
return {
}
},
methods: {
async click (button) {
const response = await button.callback(button.data)
if (response.close) {
this.closeDialog()
}
},
closeDialog () {
this.$emit('close')
}
},
computed: {
}
}
</script>
<style>
</style>
Maybe this is something I'm missing with an $emit in Vue or something, but it seems it should be working. Can someone point out why the callback is undefined after being passed to brDialog?
callback is undefined because you define your data property (App.vue from your repo) with an arrow function and loose the Vue context on this:
data: () => {
return {
testingForm: {
//...
dialog: {
props: {
buttonCallback: this.testingFormSave, //<-- here
buttons: [
{
label: 'Save',
data: {},
callback: this.testingFormSave //<-- and here
}
]
}
}
}
}
},
To fix your issue, change data: () => {...} to data () {...}

Why does V-select value changes on second click instead of first?

I have a V-select like below, and when I load the page it gets filled with data from my Vuex-store. I then have a computed property to get the currently selected company. My problem is that the value from the currently selected company only updates after I click on it Twice. Have I done something wrong in the implementation?
So when a user changes value in the V-select I want to update the list of users being shown, but this only works if the user clicks twice on the v-select selection.
<template>
<v-container fluid fill-height>
<v-layout child-flex>
<v-card>
<v-card-title>
<h3>Användare</h3>
<v-spacer></v-spacer>
<v-select
v-bind:items="listOfCompanys"
v-model="selectedCompany"
item-value="customerid"
item-text="name"
single-line
bottom
v-on:change="onChangeCompany"
autocomplete></v-select>
</v-card-title>
<UserTable></UserTable>
</v-card>
</v-layout>
</v-container>
</template>
<script>
import { FETCH_COMPANY_LIST, FETCH_USER_LIST } from '../store/actions.type'
import UserTable from './UserTable.vue'
export default {
name: 'Users',
data: () => ({
selectedCompany: 0
}),
components: {
UserTable
},
methods: {
onChangeCompany () {
this.$store.dispatch(FETCH_USER_LIST, this.currentCompany)
}
},
mounted: function () {
this.$store.dispatch(FETCH_COMPANY_LIST)
},
computed: {
listOfCompanys () {
return this.$store.state.users.companyList
},
currentCompany () {
return this.selectedCompany
}
}
}
</script>
Don't do both v-model and v-on:change. You're sending this.currentCompany, which I think is supposed to be selectedCompany. If the idea is to send the value when it changes, put a watch on the value, not on the widget. The widget feeds the value, the value feeds the store.

Categories

Resources