Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm coding for the function to see the Faculty's details and it shows number of majors of that faculty. How to count array with in template?

I’m coding for the function to see the Faculty’s details and it shows number of majors of that faculty. I don’t know how to count array majors with v-if and v-for. Please help me fix that. Many thanks.

Template tag

<tr>
    <td>Sumary: 
        <strong v-for="major in majors">
            <strong v-if="form.faculty_code==majors.major_faculty">
             {{ major }}
            </strong>
        </strong>
    </td>
</tr>

Script tag

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data() {
    return {
        faculties:[],
        majors:[],
        form: new Form({
            faculty_id:'',
            faculty_code:'',
            faculty_name:'',
            faculty_status: '',
        }),
    };
}, 
mounted() {
    this.fetchMajors();
    this.fetchFaculties();
},
methods: {
    fetchFaculties(page_url) {
        let vm = this;
        page_url = '../../api/admin/edu-faculty/faculty/'+this.currentEntries+'?page='+this.pagination.current_page;
        fetch(page_url)
        .then(res => res.json())
        .then(res => {
            this.faculties = res.data;
            this.pagination = res.meta;
        })
        .catch(err => console.log(err));
    },    
    fetchMajors(page_url) {
        let vm = this;
        page_url = '../../api/admin/edu-major/chuyen-nganh/major';
        fetch(page_url)
        .then(res => res.json())
        .then(res => {
            this.majors = res.data;
        })
        .catch(err => console.log(err));
    },
}

>Solution :

To only get the amount of majors with the given faculty code you can use in your mounted() function

this.total.majors = 0;
this.majors.forEach((element) => {
    element.major_faculty == this.faculty_code ? this.total_majors += 1  : ""
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading