0

I have this Json

all_subjects: [
{
id: 1,
subject_code: "COMP101",
description: "Knowledge Work Software & Presentation ",
name: "",
grade: [
{
id: 103,
subject_id: "1",
user_id: "17",
grade: "F",
reviewed: "0"
},
{
id: 104,
subject_id: "1",
user_id: "16",
grade: "F",
reviewed: "0"
}
]
},

my view

 <tbody v-for="subject in subjects">
        <tr>
          <td>
            <span> @{{ subject.subject_code }}</span>
          </td>
          <td>
            <span> @{{ subject.description }} </span>
          </td>
          <td>
            <span> @{{ subject.grade.grade->count() }} </span>
          </td>
          <td><span> <div class="btn btn-crimson btn-inline-block"><a href="#" data-toggle="modal" data-target="#myModal" class="btn-view-more">View more info</a></div> </span></td>
        </tr>       
      </tbody>

how do I count the that is F. I received the json value using vue.resource this.$http.get throught API Call. I'm trying this @{{ all_subjects.grade.grade->count() }} it doesn't work. I wanted to count the subjects failure inside the v-for loop in vue.js what are the ways around this?

1
  • From the JSON grade is a string so what do you exactly mean by count the grade. And you should not use PHP's -> operator where you are using it because Blade will not process it as you have ignored it with the @. Guessing you are using Laravel Commented Apr 10, 2016 at 6:16

1 Answer 1

1

You are trying to call a method count() that doesn't exist in your code. Also, you can't call methods the same way in Javascript as you do in PHP. So you can't use the -> operator to make a method call.

You can add a new method in your Vue vm:

methods: {
        count: function(subject) {
            var grades = subject.grade.filter(function(grade) {
                return grade.grade === 'F';
            });
            return grades.length;
        }
    },

The code above works by using Javascript's filter() method.

Then call that method in your view:

<ul>
    <li v-for="subject in all_subjects">
        Number of F's: {{ count(subject) }}
    </li>
</ul>

Also note that in your v-for you are referencing subjects instead of all_subjects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.