Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare v-if with a value in Vue.js

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>
like image 618
adam78 Avatar asked Dec 26 '15 08:12

adam78


People also ask

What does V-if do in Vue?

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.

What is the difference between V-if and V-show?

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.

What is V cloak in VUE JS?

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.

What is V text in VUE JS?

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.


1 Answers

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/

like image 186
Gus Avatar answered Oct 26 '22 16:10

Gus