Custom Filters¶
Imagine a scenario where you've created a custom Attribute that is the concatenation of two other imagine device Attributes generated automatically by Molten. For instance, a Full Name field which concatenates First Name and Last Name attributes together:
import { Attributes } from '@leverege/ui-attributes'
const options = {
filterable : true,
sortable : true
}
export default {
id : 'person.fullName',
name : 'person.fullName',
displayName : 'Full Name',
objectType : 'person',
valueType : 'string',
get : ( obj ) => {
return `${Attributes.get( 'person.firstName', 'person', obj )} ${Attributes.get( 'person.lastName', 'person', obj )}`
},
getOption : ( key ) => {
return options[key]
}
}
First, we will need to register a custom comparator against our new Attribute:
import {StringEqualsComparatorEditor} from '@leverege/ui-attributes';
const TYPE = 'person.fullName.equals'
const FullNameEqualsModel = {
create() {
return {
type : TYPE,
value1 : null
}
},
TYPE,
...ModelUtil.createAllValue( 'value1' ),
name : 'equals'
}
molten.addPlugin( 'AttributeConditionComparator', {
type : FullNameEqualsModel.TYPE,
model : FullNameEqualsModel,
name : FullNameEqualsModel.name,
attrName : 'person.fullName'
} )
molten.addPlugin( 'AttributeConditionComparatorEditor', { type : FullNameEqualsModel.type, editor : StringEqualsComparatorEditor } )
Note
Here we are using the StringEqualsComparatorEditor from the @leverege/ui-attribute
library as an editor for our comparator's model since the two models are essentially identical and this saves us a bit of work.
However, this comparator won't show up in our condition editor just yet, and that is because we need to regsiter a ComparatorSearchConverter to convert this comparator model into a Data Source Specific Filter Model.
import { Attributes } from '@leverege/ui-attributes'
export default {
id : 'custom.person.fullName.Equals.ComparatorFilterConverter',
type : 'groupDelegate.imagine.person.fullName.equals.ComparatorFilterConverter',
convert : ( type, cmp, attribute ) => {
if ( cmp?.value1 == null ) {
return null
}
const firstNameAttr = Attributes.getAttribute( 'person.firstName', 'person' )
const lastNameAttr = Attributes.getAttribute( 'person.lastName', 'person' )
const [ firstName, lastName ] = cmp.value1.split( ' ' )
return {
type : 'logical',
operator : 'and',
conditions : [ {
type : 'equals',
value : firstName,
field : firstNameAttr.blueprint.field
}, {
type : 'equals',
value : lastName,
field : lastNameAttr.blueprint.field
} ]
}
}
}