Skip to content

Commit 6e5534f

Browse files
committed
add filter function
1 parent be20166 commit 6e5534f

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

src/components/DgFilter.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
round-checkbox.sortOption(v-for="(val, key) in sort", :label="key", :value="sort[key]", :isBlock="false", @input="onSortCheck(key, $event)")
1111
.control
1212
p.control-label LIMIT RANGE
13-
multi-range-slider(:min="range['min']", :max="range['max']", v-model="range.value")
13+
multi-range-slider(:min="bound[0]", :max="bound[1]", :value="range", @input="onRangeChange")
1414
</template>
1515

1616
<script>
@@ -20,18 +20,15 @@ import Icon from 'components/Icon'
2020
2121
export default {
2222
props: {
23-
isActive: Boolean
23+
isActive: Boolean,
24+
bound: Array,
25+
range: Array
2426
},
2527
data () {
2628
return {
2729
sort: {
2830
ascending: false,
2931
descending: false
30-
},
31-
range: {
32-
min: 0,
33-
max: 10000,
34-
value: [0, 10000]
3532
}
3633
}
3734
},
@@ -60,6 +57,9 @@ export default {
6057
order = -1
6158
}
6259
this.$emit('sort', order)
60+
},
61+
onRangeChange (value) {
62+
this.$emit('range', value)
6363
}
6464
}
6565
}

src/components/DgTable.vue

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
:key="'header--'+attribute", :class="headerClass(attribute)",
99
@click.self="onHeaderClick(attribute, $event)", @contextmenu.prevent="openMenu") {{ attribute | capitalize }}
1010
dg-filter(v-if="filterables.includes(attribute)", :isActive="activefilterables[attribute]",
11-
@change="onFilterMenuChange(attribute, $event)", @sort="onSort(attribute, $event)")
11+
:bound="filterBounds[attribute]", :range="filterRanges[attribute]",
12+
@change="onFilterMenuChange(attribute, $event)", @sort="onSort(attribute, $event)", @range="onRange(attribute, $event)")
1213
tbody
1314
transition-group(v-for="(record, index) in sortedRecords", name="fade", tag="tr")
1415
td.cell.cell--date(v-if="isfirstOfDateGroup(index)", key="cell-date", :rowspan="getNumOfDateGroupByIndex(index)")
@@ -33,7 +34,7 @@ import _ from 'lodash'
3334
import { TweenMax, Linear } from 'gsap'
3435
import { records } from '../data.json'
3536
import settings from '../tableSettings'
36-
import { toCurrency, toMMMMYYYY, capitalize, toGMapQuery } from 'utils/filters'
37+
import { toCurrency, toMMMMYYYY, capitalize, toGMapQuery, toUpperMagnitude } from 'utils/filters'
3738
import { offsets } from 'utils/mouse'
3839
import DgMenu from 'components/DgMenu'
3940
import DgCellMenu from 'components/DgCellMenu'
@@ -53,10 +54,16 @@ export default {
5354
},
5455
data () {
5556
let sortOrders = {}
57+
let filterRanges = {}
58+
let filterBounds = {}
5659
let activefilterables = {}
5760
settings.filterables.forEach((attribute) => {
58-
sortOrders[attribute] = 0
5961
activefilterables[attribute] = false
62+
sortOrders[attribute] = 0
63+
filterBounds[attribute] = [0, toUpperMagnitude(records.reduce((acc, cur) => {
64+
return cur[attribute] > acc ? cur[attribute] : acc
65+
}, 0))]
66+
filterRanges[attribute] = _.clone(filterBounds[attribute])
6067
})
6168
return {
6269
records: records,
@@ -69,6 +76,8 @@ export default {
6976
omitOnMenu: settings.omitOnMenu,
7077
sortAttribute: '',
7178
sortOrders: sortOrders,
79+
filterRanges: filterRanges,
80+
filterBounds: filterBounds,
7281
activefilterables: activefilterables,
7382
expanding: '',
7483
focusCell: {
@@ -85,10 +94,19 @@ export default {
8594
},
8695
computed: {
8796
sortedRecords () {
88-
let {sortAttribute, sortOrders, records} = this
97+
const {sortAttribute, sortOrders, filterRanges, records} = this
8998
const order = sortOrders[sortAttribute] || 0
99+
100+
// Filtered fitted in ranges records
101+
const filteredRecords = records.filter((record) => {
102+
return Object.keys(filterRanges).reduce((acc, key) => {
103+
return filterRanges[key][0] <= record[key] && filterRanges[key][1] >= record[key]
104+
}, true)
105+
})
106+
90107
if (sortAttribute) {
91-
return records.slice().sort((a, b) => {
108+
// Sort by sortAttribute locally and by date globally
109+
return filteredRecords.slice().sort((a, b) => {
92110
const aSort = a[sortAttribute]
93111
const bSort = b[sortAttribute]
94112
const localOrder = (aSort === bSort ? 0 : aSort > bSort ? 1 : -1) * order
@@ -97,7 +115,8 @@ export default {
97115
return moment(aDate).isSame(bDate) ? localOrder : moment(aDate).isBefore(bDate) ? -1 : 1
98116
})
99117
} else {
100-
return records.slice().sort((a, b) => {
118+
// Only sort by date
119+
return filteredRecords.slice().sort((a, b) => {
101120
return moment(a.date).isBefore(b.date) ? -1 : 1
102121
})
103122
}
@@ -212,7 +231,11 @@ export default {
212231
this.sortAttribute = order ? attribute : ''
213232
this.sortOrders[attribute] = order
214233
},
234+
onRange (attribute, range) {
235+
this.filterRanges[attribute] = range
236+
},
215237
onFilterMenuChange (attribute, active) {
238+
this.clearFocusCell()
216239
this.activefilterables[attribute] = active
217240
}
218241
}

src/utils/filters.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ function toGMapQuery (address) {
1818
return `https://www.google.com/maps?q=${address.replace(/\s/g, '+')}`
1919
}
2020

21-
export {toCurrency, toMMMMYYYY, capitalize, toGMapQuery}
21+
function toUpperMagnitude (num) {
22+
return Math.pow(10, num.toString().length)
23+
}
24+
25+
export {toCurrency, toMMMMYYYY, capitalize, toGMapQuery, toUpperMagnitude}

0 commit comments

Comments
 (0)