I have the following code to filter the table. I select a column from drop-down like age
then selects operator from another drop-down like <
and search input like 20
, And want to search table on them.
Filter :
// Select column name
<select class="dv-header-select" v-model="query.search_column">
<option v-for="column in columns" :value="column">{{column}}</option>
</select>
// Select condition(<,>,<=,>=)
<select class="dv-header-select" v-model="query.search_operator">
<option v-for="(value, key) in operators" :value="key">{{value}}</option>
</select>
// Search value
<input type="text" class="dv-header-input" placeholder="Search"
v-model="query.search_input">
Please read the code comments in JS section to get the idea.
<tr v-for="row in getSearchedRow">
<td v-for="(value, key) in row">{{value}}</td>
</tr>
JS:
data() {
return {
model: { data: [] },
// populating on API call
columns: {},
query: {
search_column: 'id',
search_operator: 'equal',
search_input: ''
}
},
getSearchedRow: function() {
return this.model.data.filter(row => {
let value = row[this.query.search_column];
for(var key in row){
if(String(row[key]).indexOf(this.query.search_input) !== -1){
// Return true required to populate table
if(this.query.search_column.length < 1) {
return true;
}
// when condition gets here, The table shows 0 records
if(this.query.search_operator == 'less_than') {
return value < this.query.search_input;
}
}
}
});
}
The table get populated because of first if()
but shows empty on second if()
.
What am i missing ?