【问题标题】:How to check the user who gave the vote如何查看投票的用户
【发布时间】:2017-10-22 15:49:29
【问题描述】:

我想制作一个像 stackoverflow 这样的投票系统,投票运行良好,但我仍然受到添加一个关于用户是否投票的类的限制。

我有这样的 json 数据:

[{
    "id": 157,
    "questionvote": 
     [
        {
            "user": 6,
            "question": 157,
            "value": true
        },
        {
            "user": 3,
            "question": 157,
            "value": false
        }
      ]
 }]

我用vue,投票的模板是这样的-

  <span type="submit" @click='positif(upvote, q)' v-bind:class="{ on: classVote }" class="vote"> </span>

我想检查是否曾对问题投票过的用户是否添加了班级投票

classVote: function () {
   return {
     /// how to check it
   }
}

我应该如何编写它才能有条件地确定类?

【问题讨论】:

  • 遍历questionvote并检查item['user']当前用户的ID。
  • 个人。我认为它没有解决我的问题

标签: javascript class vuejs2 vote-up-buttons


【解决方案1】:
<span type="submit" @click='positif(upvote, q)' v-bind:class="classUpVote(q)" class="vote"> </span>

在方法中:

methods:{
 classUpVote: function (q) {
  var qvote = q.questionvote
  var activeuser = this.usernow /// get current user
  var hasMatch = false

  for (var i = 0; i < qvote.length; ++i) {
   var quest = qvote[i];

   if(quest.user == activeuser && quest.value == true){
     hasMatch = true;
     break;
   }
  }

  return {
    on: hasMatch
  }
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多