ukrbublik / react-awesome-query-builder

User-friendly query builder for React
https://ukrbublik.github.io/react-awesome-query-builder
MIT License
1.97k stars 494 forks source link

Overdue Date / Field differrence #142

Open bogdancs92 opened 4 years ago

bogdancs92 commented 4 years ago

Hello,

I'm trying to use the component and I need to define a new operator I think.

Here it is what I want:

Also a operator who does the difference between two fields can help us :) : if I have a difference of more then 20€ between FIELD_A and FIELD_B : sql effect : (FIELD_A - FIELD_B)>RIGHT_VALUE

Thank you for your ideas :)

bogdancs92 commented 4 years ago

Hello,

does the "factory" property for a new widget is used by the master branch?

widgets: { text: { type: "text", //see 'types' section valueSrc: 'value', //'value' or 'field' (only for special 'field' widget) factory: (props) => <TextWidget {...props} />, //React component

If not, do you have any idea when it will be ?

ukrbublik commented 4 years ago

@bogdancs92 factory is used.

Addressing your request for overdue operator, I'll add support for it (or explain how to configure it without code changes, which is possible I guess) at this weekend when I have some free time.

Field difference can be implemented with functions. My lib supports exactly 1 field on left side. So if converting (FIELD_A - FIELD_B)>RIGHT_VALUE to FIELD_A > FIELD_B + RIGHT_VALUE would work for you, you can add function like difference which takes 2 args - field and number.

bogdancs92 commented 4 years ago

Hello @ukrbublik and thanks you for your work and your help.

For the momeent I succeeded in doing this : FIELD_A "bigger than" FIELD_B with "number input" using a new operator with cardinality 2 and overriding some properties :

const differenceOperator = { label : 'supérieur', cardinality : 2, reversedOp : 'difference', labelForFormat : 'diff', formatOp : (field, _op, value, _valueSrc, _valueType, opDef) => { console.log(differenceOperator > ${field} ${opDef.labelForFormat} ${value}); return ""; }, sqlFormatOp : (field, _op, value, _valueSrc, _valueType, opDef) => { return "(("+field+" - "+value._tail.array[0]+") > "+value._tail.array[1]+")"; }, jsonLogic : (field, _op, value, _valueSrc, _valueType, opDef) => { return "difference[" + value[1] +"]"; }, valueLabels :['Champ', {label: 'Combien', placeholder: 'Valeur'}], textSeparators : ["à", 'de'], factory: (props) => <NumberWidget {...props} />, }; and using this operator in the number types.

This seems to do what I want (exemple in sql : ((FIELD_A - FIELD_B) > 50)

When I try to do exactly the same thing for the date type (the "overdue operation) it doesn't work because the widget presents 2 fields of type date instead of leaving the first field of the same type field and a numeric field for the second....With this I think a need some help...

image

Thanks