I have the following data and I wish to check if the user has the Admin role.
{
"users": [
{
"id": 3,
"first_name": "Joe",
"last_name": "Blogs",
"roles": [
{
"id": 1,
"name": "Admin",
"created_at": "2015-12-25 15:58:28",
"updated_at": "2015-12-25 15:58:28",
"pivot": {
"user_id": 3,
"role_id": 1,
"created_at": "2015-12-25 16:03:09",
"updated_at": "2015-12-25 16:03:09"
}
},
{
"id": 2,
"name": "Member",
"created_at": "2015-12-25 15:58:28",
"updated_at": "2015-12-25 15:58:28",
"pivot": {
"user_id": 3,
"role_id": 2,
"created_at": "2015-12-25 16:03:09",
"updated_at": "2015-12-25 16:03:09"
}
}
],
}
]
}
In my html I have the following, but how can I use the v-if directive to check if the user has the Admin role? The other problem is that v-if directive below also generates unnecessary html markup if the condition is untrue.
<tr>
<th>User</th>
<th>Admin</th>
<th></th>
</tr>
<tr v-for="user in users">
<td>
@{{user.first_name}} @{{user.last_name}}
</td>
<td>
<span v-for="role in user.roles">
<span v-if="role.name" == 'Admin'>Yes</span>
<span v-else>-</span>
</span>
</td>
</tr>
The directive v-if is used to conditionally render a block. The block will only be rendered if the directive's expression returns a truthy value.
The key difference is that v-if conditionally renders elements and v-show conditionally displayselements. This means that v-if will actually destroy and recreate elements when the conditional is toggled. Meanwhile, v-show will always keep the element in the DOM and will only toggle its display by changing its CSS.
The v-cloak directive is a Vue. js directive that will remain on the element until the associated Vue instance finishes compilation. Combined with CSS rules such as [v-cloak] { display: none }, this directive can be used to hide uncompiled mustache bindings until the Vue instance is ready.
The v-text directive is a Vue. js directive used to update a element's textContent with our data. It is just a nice alternative to Mustache syntax. First, we will create a div element with id as app and let's apply the v-text directive to this element with data as a message.
You have a syntax problem on you html, it should be:
<span v-for="role in user.roles">
<span v-if="role.name == 'Admin'">Yes</span>
<span v-else>-</span>
</span>
You will still have extra spans for each role the user has, though.
It's simpler to use a method to check if a user has the admin role and that way you won't have the extra html:
<div id="container">
<table>
<tr>
<th>User</th>
<th>Admin</th>
<th></th>
</tr>
<tr v-for="user in users">
<td>
{{user.first_name}} {{user.last_name}}
</td>
<td>
{{isAdmin(user)}}
</td>
</tr>
</table>
</div>
And the javascript:
new Vue({
el: '#container',
data: {
"users": [{
"id": 3,
"first_name": "Joe",
"last_name": "Blogs",
"roles": [{
"id": 1,
"name": "Admin",
"created_at": "2015-12-25 15:58:28",
"updated_at": "2015-12-25 15:58:28",
"pivot": {
"user_id": 3,
"role_id": 1,
"created_at": "2015-12-25 16:03:09",
"updated_at": "2015-12-25 16:03:09"
}
}, {
"id": 2,
"name": "Member",
"created_at": "2015-12-25 15:58:28",
"updated_at": "2015-12-25 15:58:28",
"pivot": {
"user_id": 3,
"role_id": 2,
"created_at": "2015-12-25 16:03:09",
"updated_at": "2015-12-25 16:03:09"
}
}],
}]
},
methods: {
isAdmin: function(user) {
var i;
for (i = 0; i < user.roles.length; i++) {
if (user.roles[i].name === "Admin") {
return "Yes";
}
}
return "-";
}
}
});
Working example here: https://jsfiddle.net/gmsa/t56oo4tw/
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