forked from piccolo-orm/piccolo_admin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RowFilter.vue
115 lines (99 loc) · 3.12 KB
/
RowFilter.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<template>
<div class="filter_wrapper">
<h1>Filter</h1>
<a @click="closeSideBar()" class="button" id="sidebar-close">
<span> <font-awesome-icon icon="times" /> </span>Close
</a>
<form ref="form" v-on:submit.prevent="submitForm($event)">
<RowFormSearch v-bind:schema="schema" />
<button>Apply</button>
</form>
<button v-on:click.prevent="clearFilters">Clear filters</button>
</div>
</template>
<script lang="ts">
import Vue from "vue"
import RowFormSearch from "./RowFormSearch.vue"
import { APIResponseMessage } from "../interfaces"
export default Vue.extend({
props: {
showFilterSidebar: Boolean,
},
components: {
RowFormSearch,
},
computed: {
schema() {
return this.$store.state.schema
},
},
methods: {
closeSideBar() {
this.$emit("closeSideBar", false)
},
showSuccess(contents: string) {
var message: APIResponseMessage = {
contents: contents,
type: "success",
}
this.$store.commit("updateApiResponseMessage", message)
},
async submitForm(event: any) {
const form = new FormData(event.target)
const json = {}
for (const i of form.entries()) {
const key = i[0].split(" ").join("_")
let value: any = i[1]
if (value && value != "all") {
if (this.schema.properties[key]?.type == "array") {
// @ts-ignore
value = JSON.parse(value).filter((i) => i)
// Ignore any empty values.
value = Array.isArray(value)
? value.filter((i) => i)
: value
}
json[key] = value
}
}
this.$store.commit("updateFilterParams", json)
this.$store.commit("updateCurrentPageNumber", 1)
try {
await this.$store.dispatch("fetchRows")
} catch (error) {
return
}
this.showSuccess("Successfully applied filter")
},
async clearFilters() {
console.log("Clearing ...")
let form: any = this.$refs.form
let _ = [...form.elements].forEach((element) => {
if (element.type == "hidden") {
element.value = ""
}
})
form.reset()
this.$store.commit("updateFilterParams", {})
this.$store.commit("updateCurrentPageNumber", 1)
try {
await this.$store.dispatch("fetchRows")
} catch (error) {
return
}
this.showSuccess("Successfully cleared filters")
},
},
})
</script>
<style scoped lang="less">
div.filter_wrapper {
position: relative;
#sidebar-close {
cursor: pointer;
position: absolute;
top: 0.5rem;
right: 0.5rem;
}
}
</style>