I've got an employee table. Every employee has a role, I'm trying to filter that with radio buttons (for example a radio button admin or superadmin).
How can I use a variable within a component in another component? Right now I've this:
<template id="searchemployees">
<div class="form-group">
<label class="col-md-2 control-label">Filter</label>
<div class="col-md-10">
<div class="radio radio-primary">
<label>
<input type="radio" name="intern" id="intern" value="intern" v-model="selectedrole"/>
Toon alles
</label>
</div>
<div class="radio radio-primary">
<label>
<input type="radio" name="employee" id="employee" value="employee" v-model="selectedrole">
Stagiaire
</label>
</div>
</div>
</div>
</template>
<template id="employees">
<table class="table">
<thead>
<tr>
<th>Logo</th>
<th>Bedrijfsnaam</th>
<th>Plaats</th>
</tr>
</thead>
<tbody>
<tr class="table-row-link" data-href="" v-for="employee in list | role selectedrole">
<td><img class="img-circle-company" src=""/></td>
<td>@{{ employee.role.RoleName }}</td>
<td>@{{ employee.LastName }}</td>
</tr>
</tbody>
</table>
</template>
<script>
Vue.config.debug = true;
Vue.filter('role',function(value,role)
{
if(!role || 0 === role.length)
{
return value;
}
return value.filter(function(item) {
return item.role.RoleName == role
});
});
Vue.component('searchemployees', {
template: '#searchemployees'
});
Vue.component('employees', {
template: '#employees',
props:['list'],
created() {
this.list = JSON.parse(this.list);
}
});
new Vue({
el: 'body'
});
</script>
In my #searchemployeesit takes the right selected role. But how can I use it in #employees for my filter?
Thankyou
You should be able to send a property from a component upwards to the root / global Vue instance by dispatching an event. Dispatching an event means you're sending a message, one that contains a property, to a component's parent in the Vue tree. You dispatch an event using the following syntax:
this.$dispatch('event-name', this.list);
This says, send an event with an ID of event-name, containing the property this.list, to the component's parent.
Your component's parent can only receive this dispatched property if it is listening for that exact message with the ID of event-name. You can listen to events from within the parent component using the following code (this goes in parent):
events: {
'event-name': function(property) {
console.log("Property from child component" + property);
}
}
Overall, your code should look something like this:
Vue.component('employees', {
template: '#employees',
props:['list'],
created() {
this.list = JSON.parse(this.list);
this.$dispatch('send-list', this.list);
}
});
new Vue({
el: 'body',
data: {
rootlist: {}
},
events: {
'send-list': function(property) {
this.rootlist = property;
console.log("Property from child component" + property);
}
}
});
This will make all of your [list] data available within your root Vue instance. If you'd like to send it down to all components, you can create a broadcast event this.$broadcast('send-list-down', this.list); within the send-list function. $broadcast does the exact same thing as $dispatch, but it sends the data down the Vue tree instead of up. Just make sure you create event listeners within your components and for the send-list-down event.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With