fix: getting filterable fields and added operators with logic

This commit is contained in:
Shariq Ansari 2023-08-10 17:31:33 +05:30
parent 75f8568b84
commit 2bf5cea7b4
2 changed files with 90 additions and 30 deletions

View File

@ -14,7 +14,7 @@
</template> </template>
<template #body="{ close }"> <template #body="{ close }">
<div class="rounded-lg border border-gray-100 bg-white shadow-xl my-2"> <div class="rounded-lg border border-gray-100 bg-white shadow-xl my-2">
<div class="p-2 min-w-[537px]"> <div class="p-2 min-w-[500px]">
<div <div
v-if="filterValues.length" v-if="filterValues.length"
v-for="(filter, i) in filterValues" v-for="(filter, i) in filterValues"
@ -26,18 +26,16 @@
{{ i == 0 ? 'Where' : 'And' }} {{ i == 0 ? 'Where' : 'And' }}
</div> </div>
<Autocomplete <Autocomplete
class="!w-32" class="!min-w-[140px]"
v-model="filter.field" :value="filter.field"
:options="[ :options="filterableFields.data"
{ label: 'Name', value: 'lead_name' }, @change="(e) => updateFilter(e, i)"
{ label: 'Status', value: 'status' },
]"
placeholder="Filter by..." placeholder="Filter by..."
/> />
<FormControl <FormControl
type="select" type="select"
v-model="filter.operator" v-model="filter.operator"
:options="operators" :options="getOperators(filter.fieldtype)"
placeholder="Operator" placeholder="Operator"
/> />
<FormControl <FormControl
@ -57,10 +55,7 @@
</div> </div>
<div class="flex items-center justify-between gap-2"> <div class="flex items-center justify-between gap-2">
<Autocomplete <Autocomplete
:options="[ :options="filterableFields.data"
{ label: 'Name', value: 'lead_name' },
{ label: 'Status', value: 'status' },
]"
value="" value=""
placeholder="filter by" placeholder="filter by"
@change="(e) => setfilter(e)" @change="(e) => setfilter(e)"
@ -103,36 +98,101 @@ import {
} from 'frappe-ui' } from 'frappe-ui'
import { ref } from 'vue' import { ref } from 'vue'
const props = defineProps({
doctype: {
type: String,
required: true,
},
})
const filterValues = ref([]) const filterValues = ref([])
const operators = ref([ const filterableFields = createResource({
{ label: 'Contains', value: 'contains' }, url: 'crm.api.doc.get_filterable_fields',
{ label: 'Equals', value: 'equals' }, auto: true,
{ label: 'Not Equals', value: 'not equals' }, params: {
{ label: 'Starts With', value: 'starts with' }, doctype: props.doctype,
{ label: 'Ends With', value: 'ends with' }, },
{ label: 'Less Than', value: 'less than' }, transform(fields) {
{ label: 'Greater Than', value: 'greater than' }, fields = fields.map((field) => {
{ label: 'Between', value: 'between' }, return {
{ label: 'In', value: 'in' }, label: field.label,
{ label: 'Not In', value: 'not in' }, value: field.fieldname,
{ label: 'Is', value: 'is' }, ...field,
{ label: 'Is Not', value: 'is not' }, }
{ label: 'Is Set', value: 'is set' }, })
{ label: 'Is Not Set', value: 'is not set' }, return fields
]) },
})
const typeCheck = ['Check']
const typeLink = ['Link']
const typeNumber = ['Float', 'Int']
const typeSelect = ['Select']
const typeString = ['Data', 'Long Text', 'Small Text', 'Text Editor', 'Text']
function getOperators(fieldtype) {
let options = []
if (typeString.includes(fieldtype) || typeNumber.includes(fieldtype)) {
options.push(
...[
{ label: 'Equals', value: 'equals' },
{ label: 'Not Equals', value: 'not equals' },
{ label: 'Like', value: 'like' },
{ label: 'Not Like', value: 'not like' },
]
)
}
if (typeNumber.includes(fieldtype)) {
options.push(
...[
{ label: '<', value: '<' },
{ label: '>', value: '>' },
{ label: '<=', value: '<=' },
{ label: '>=', value: '>=' },
{ label: 'Equals', value: 'equals' },
{ label: 'Not Equals', value: 'not equals' },
]
)
}
if (typeSelect.includes(fieldtype) || typeLink.includes(fieldtype)) {
options.push(
...[
{ label: 'Is', value: 'is' },
{ label: 'Is Not', value: 'is not' },
]
)
}
return options
}
function setfilter(data) { function setfilter(data) {
let operator = getOperators(data.fieldtype)[0].value
filterValues.value = [ filterValues.value = [
...filterValues.value, ...filterValues.value,
{ {
field: data.value, field: data.value,
operator: 'contains', operator: operator,
value: '', value: '',
label: data.label,
fieldtype: data.fieldtype,
options: data.options,
}, },
] ]
} }
function updateFilter(data, index) {
let operator = getOperators(data.fieldtype)[0].value
filterValues.value[index] = {
field: data.value,
operator: operator,
value: '',
label: data.label,
fieldtype: data.fieldtype,
options: data.options,
}
}
function removeFilter(index) { function removeFilter(index) {
filterValues.value.splice(index, 1) filterValues.value.splice(index, 1)
} }

View File

@ -116,7 +116,7 @@ const sortButtonRef = ref(null)
const sortValues = ref(initialOrderBy()) const sortValues = ref(initialOrderBy())
const sortOptions = createResource({ const sortOptions = createResource({
url: 'crm.extends.doc.sort_options', url: 'crm.api.doc.sort_options',
auto: true, auto: true,
params: { params: {
doctype: props.doctype, doctype: props.doctype,