0

Trying to display Total records. Students.length works the first time on page load thanks to the created() method. However, calling filteredStudents(), is out of date. What is the easiest way to make this reactive?

<template>
        <div class="d-inline-flex flex-row p-4 col-2">
          Total record(s): {{ recordCount }}
        </div>

    <table class="table border table-striped table-hover">
        <thead class="bg-secondary">
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="student in filteredStudents()" :key="student._id">
                <td>{{ student.firstName }}</td>
                <td>{{ student.lastName }}</td>
                <td>{{ student.email }}</td>
            </tr>
        </tbody>
    </table>
</template>

<script>
    import MixinCommon from '@/mixins/common.js'

    export default {
        data() {
            return {
                searchTerm: '',
                Students: [],
                studentcount: 0
            }
        },
        created() {
            this.Students = this.getSutdentList()
        },
        computed: {
            recordCount() {
                return this.Students.length
            }
        },
        mixins: [MixinCommon],
        methods: {
            filteredStudents() {
                return this.searchStudentList(this.searchTerm.toUpperCase(), this.Students)
            },
        }
    }
</script>
1
  • 4
    computed prop instead of a method Commented Nov 13, 2022 at 20:23

1 Answer 1

2

I don't know the implementation of the searchStudentsList method, but you could try using the filteredStudents as a computed property, or making a watch property on the searchTerm in order to make the search again:

Using computed:

<template>
        <div class="d-inline-flex flex-row p-4 col-2">
          Total record(s): {{ recordCount }}
        </div>

    <table class="table border table-striped table-hover">
        <thead class="bg-secondary">
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="student in filteredStudents" :key="student._id">
                <td>{{ student.firstName }}</td>
                <td>{{ student.lastName }}</td>
                <td>{{ student.email }}</td>
            </tr>
        </tbody>
    </table>
</template>

<script>
    import MixinCommon from '@/mixins/common.js'

    export default {
        data() {
            return {
                searchTerm: '',
                Students: [],
                studentcount: 0
            }
        },
        created() {
            this.Students = this.getSutdentList()
        },
        computed: {
            recordCount() {
                return this.Students.length
            },
            filteredStudents() {
                return this.searchStudentList(this.searchTerm.toUpperCase(), this.Students)
            },
        },
        mixins: [MixinCommon],
    }
</script>

Using watch property:

<template>
        <div class="d-inline-flex flex-row p-4 col-2">
          Total record(s): {{ recordCount }}
        </div>

    <table class="table border table-striped table-hover">
        <thead class="bg-secondary">
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="student in filteredStudents" :key="student._id">
                <td>{{ student.firstName }}</td>
                <td>{{ student.lastName }}</td>
                <td>{{ student.email }}</td>
            </tr>
        </tbody>
    </table>
</template>

<script>
    import MixinCommon from '@/mixins/common.js'

    export default {
        data() {
            return {
                searchTerm: '',
                Students: [],
                filteredStudents: [],
                studentcount: 0
            }
        },
        created() {
            this.Students = this.getSutdentList()
            this.filteredStudents = this.searchStudentList(this.searchTerm.toUpperCase(), this.Students)
        },
        computed: {
            recordCount() {
                return this.Students.length
            }
        },
        watch: {
          searchTerm(newValue) {
            this.filteredStudents = this.searchStudentList(newValue.toUpperCase(), this.Students)
          }
        }
        mixins: [MixinCommon],
    }
</script>
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.