Optimizing the Search Behaviour on DataGridQueryFilter #763
-
|
I need to optimize the DataGridQueryFilter to include following conditions
Current setup from contember allows me to perform lowecase search to obtain result. However, I am unable to remove the whitespaces and dots when searching.
import { createGenericTextCellFilterCondition } from './common'
import { createFieldFilterHandler } from './createFilterHandler'
export type TextFilterArtifactsMatchMode = 'matches' | 'matchesExactly' | 'startsWith' | 'endsWith' | 'doesNotMatch'
export type TextFilterArtifacts = {
mode?: TextFilterArtifactsMatchMode
query?: string
nullCondition?: boolean
}
export const createTextFilter = createFieldFilterHandler<TextFilterArtifacts>({
createCondition: filter => {
const condition = filter.query !== '' ? createGenericTextCellFilterCondition(filter) : {}
if (filter.nullCondition === true) {
return {
or: [condition, { isNull: true }],
}
} else if (filter.nullCondition === false) {
return {
and: [condition, { isNull: false }],
}
}
return condition
},
isEmpty: filter => {
return !filter.query && filter.nullCondition === undefined
},
})
`common.ts`
```js
import { EntityId } from '@contember/react-binding'
import { Input } from '@contember/client'
export type RelationFilterArtifacts = {
id?: EntityId[]
notId?: EntityId[]
nullCondition?: boolean
}
export type GenericTextCellFilterArtifacts = {
mode?: 'matches' | 'matchesExactly' | 'startsWith' | 'endsWith' | 'doesNotMatch'
query?: string
}
export const createGenericTextCellFilterCondition = (filter: GenericTextCellFilterArtifacts) => {
if (!filter.query) {
return {}
}
const baseOperators = {
matches: 'containsCI',
doesNotMatch: 'containsCI',
startsWith: 'startsWithCI',
endsWith: 'endsWithCI',
matchesExactly: 'eq',
}
let condition: Input.Condition<string> = {
[baseOperators[filter.mode ?? 'matches']]: filter.query,
}
if (filter.mode === 'doesNotMatch') {
condition = { not: condition }
}
return condition
}I can see that we have I tried creating a function to generate string as I intended in the let condition: Input.Condition<string> = {
[baseOperators[filter.mode ?? 'matches']]: custom_function(filter.query),
}However it didn't work as database still has the original text. Is there a way to implement such functionality with |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
You’re on the right track trying to transform the query! However, the problem is that Contember’s The best solution is to normalize the text at the database level using a view. You can learn more about views here: https://docs.contember.com/reference/engine/schema/views/ Here’s an example that:
@c.View(`
SELECT
article.id AS id,
article.id AS article_id,
LOWER(
REPLACE(
REPLACE(
TRIM(COALESCE(article.title, '') || ' ' || COALESCE(article.plain_subtitle, '')),
' ', ''
),
'.', ''
)
) AS normalized_search_field
FROM article
`)
export class ArticleMetadata {
article = c.oneHasOne(Article, 'meta')
normalizedSearchField = c.stringColumn()
}In this example:
Then you can build your filter on |
Beta Was this translation helpful? Give feedback.
-
|
Hi, I implemented this solution and I got the result as I expected. Thank you! I had used
To remove this glitch, I had to logout of the application and login again. After that |
Beta Was this translation helpful? Give feedback.
You’re on the right track trying to transform the query!
However, the problem is that Contember’s
DataGridQueryFilteronly builds conditions for the database – it doesn’t modify the actual column values at query time. So if the database contains values with spaces and dots, they will still be matched literally unless you normalize them.The best solution is to normalize the text at the database level using a view. You can learn more about views here: https://docs.contember.com/reference/engine/schema/views/
Here’s an example that: