Filter table with Vue-multiselect

Asked

Viewed 132 times

2

I am using Vue-multiselect to filter a calendar using Vue-cal and am having trouble filtering a table with this selector. I believe the problem is to filter the table with an array of data, for example: filter the table by the name of two teachers. What I have: When some teacher is selected, I have it here:

[{teacher: "Ana"}, {teacher: "Danielly}]

And I try to filter like this:

filteredEvent() {
     return this.events.filter(events => {
        return events.teacher.toLowerCase().
        indexOf(this.value.forEach(el => el.teacher.toLowerCase() > -1))
    })
}

The calendar event slot looks like this:

<template slot="event-renderer" v-for="events in filteredEvent" slot-scope="{event, view}">
<div class="wrapper">
    <div class="event-content" :style="{border: event.border,}">
        <div class="vuecal__event-title">
            <div class="level-color" :style="{background: event.color}"></div>
            <m-avt-level pClass="table__avt-icon" class="centered-icon" :level="event.level" />
            <p class="title">{{event.level}}</p>
        </div>
        <div class="vuecal__event-time mt-2">
            {{event.time}}
        </div>
        <div class="vuecal__event-bar mt-1 mb-2">
            <b-container>
                <m-progress-bar :animated="                 
           :occupedVacancys=" parseInt(event.occupedVacancys) "                                          
           :maxVacancys="parseInt(event.maxVacancys) "/>
          </b-container>
        </div>
    </div>
</div>

This is the select:

   <multiselect
       v-model="value"
       :options="options"
       :multiple="true"
       :close-on-select="false"
       track-by="teacher"
       :custom-label="customLabel">
            <span class="checkbox-label" slot="option" slot-scope="scope" @click.self="select(scope.option)">
            <input type="checkbox" v-model="scope.option.selected" @focus.prevent/>
                {{ scope.option.teacher }}
            </span>
            <span slot="placeholder" slot-scope="scope">
            <svgicon icon="icn_teacher" class="filter-icon" width="18" height="auto" color="#959BA6" />
                Todos os professores
            </span>
</multiselect>

And the object this.Events:

border: "2px solid #3057a5"
class: "ap3"
classDay: 2
classId: ""
color: "#3057a5"
end: "2019-05-29"
level: "Aperfeiçoamento 3"
levelId: 8
maxVacancys: 1
occupedVacancys: 0
start: "2019-05-29"
teacher: "Danielle Kelley"
time: "00:00 - 01:05"
title: ""
  • Where it comes from this.events? what is the relationship between the teacher and the event array? how to compare them?

  • this.Vents is the date I use to popular the calendar, and within this array of events is the teacher who will teach the class at each time. So like I need to compare the select teacher with the teachers of each schedule

  • I didn’t put all the code because I thought it would look giant rs, but I can put too

  • "the teacher of select " .... you can put this select and an object as an example of this.events?

  • I put the select code and an example of this.Events

  • And what values can be given to this.value? is an array with the same string teacher that in the array you have in the question?

  • That, what comes in this.value is the array with the same string as teacher

Show 2 more comments

1 answer

0


If your Select arrow the this.value with an array of values that corresponds to the key value teacher of the event array, so this computed can be done like this:

filteredEvent() {
  return this.events.filter(({teacher}) => this.value.includes(teacher));
}

If this.value for an array of objects such as [{teacher: "Ana"}, {teacher: "Danielly"}] then you can use the .find() to find that value so:

filteredEvent() {
  return this.events.filter(({teacher}) => this.value.find(obj => obj.teacher === teacher));
}

Note: I avoid using value as key in the data of an instance. Because it is not very descriptive in most cases, and because Vue internally uses value in the logic/magic of v-model.

  • Good to know, I’ll avoid then use value from now on. I believe I understand what you did there, but I have passed you something wrong rs actually the select arrow the this.value as an array of objects, and the key of that object is the string that corresponds to the key teacher. For example [{teacher: "Ana"}, {teacher: "Danielly"}]

  • @Felipejardim I edited the answer

  • Perfect! Now I need to take a look and understand this syntax this.events.filter(({teacher})rs but thanks even, I was a while stuck in it

  • @Felipejardim this syntax is modern Javascript, but basically ({teacher}) is the same as obj and then do const teacher = obj.teacher. That is a shortcut to directly extract a key from the object the function receives.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.