-
-
Notifications
You must be signed in to change notification settings - Fork 485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic number of args for function (eg. SUM of any number of numbers) #957
Comments
The correct way to achieve this is to add custom function (you can take LINEAR_REGRESSION as example). With version 6.4.0 you can use functions in LHS. |
@ukrbublik Thanks for the answer |
As a workaround, you can have several functions:
Or (probably better) one function |
I tried using this, but I think, two problems will appear if I want
Want I want is |
Please use
in |
Indeed, I already had it is, but I had not installed the latest version from yesterday
Do you have an estimate of the release dates for these versions? For both simple math function (because should be better in operator than in funcs) and for the dynamic arg lenght |
I don't have plans to extend operators. I have plans to add new math funcs. In your example you use operator |
All right. So the math functions will be considered as funcs But in JsonLogic, the negative operator can take only two values (similar as division), the alternative should be |
It's already possible to put func in func, of you specify |
If this is already possible, there might already be a way to manage the sum more easily: Could the arg be of type multi select ? like this, a sum function could take the set of values selected in the list (like for cars / vendor on the demo) If this is possible, it would be quite possible to be able to sum with several values (as for a dynamic arg number finally) |
Yes, it can be multiselect, but it provides array of strings, and you need array of integers. Probably you can customize multiselect widget to not allow any characters other than 0-9 |
Great, I will go deeper in the configuration, and maybe If I found some time, I can submit some pull request to improve the project Last thing, you said, we can select func in func, but on my side, the result isn't working (the second func dropdown is always empty), am I doing something wrong? Capture.video.du.26-07-2023.11.33.01.webm |
Please add |
@ukrbublik Thanks for the advice, the property is |
One question : with custom function it seems the The error in console is : Also, a standard jsonLogic generated by the UI, cannot be imported using the same method
Got an error :
|
You need to add const SUM = {
label: '+',
returnType: 'number',
// export
jsonLogic: ({a, b, c, d}) => ({ "+": [a, b, c, d]}),
// import
jsonLogicImport: (v) => {
const args = v["+"];
return [...args];
},
... |
As for the error, please provide your config, or codesandbox to reproduce |
Here is my config : [... imports ...]
const operators = {
...BootstrapConfig.operators,
between: {
...BootstrapConfig.operators.between,
label: "Between",
valueLabels: ['from', 'to'],
textSeparators: ['from', 'to'],
},
};
const widgets = {
...BootstrapConfig.widgets,
number: { ...BootstrapConfig.widgets.number },
select: { ...BootstrapConfig.widgets.select },
func: { ...BootstrapConfig.widgets.func },
time: {
...BootstrapConfig.widgets.time,
timeFormat: 'HH:mm',
valueFormat: 'HH:mm:ss',
}
};
const types = {
...BootstrapConfig.types,
boolean: merge(BootstrapConfig.types.boolean, {
widgets: {
boolean: {
widgetProps: {
hideOperator: true,
operatorInlineLabel: "is",
}
},
},
}),
};
const localeSettings = { locale: { short: 'en', full: 'en-US' } };
const settings = {
...BootstrapConfig.settings,
...localeSettings,
valueSourcesInfo: {
value: { label: "Value" },
field: { label: "Variable", widget: "field" },
func: { label: "Function", widget: "func" }
},
maxNesting: 3,
canLeaveEmptyGroup: false,
canReorder: true,
canRegroup: false,
fieldSources: ["field", "func"],
renderField: (props) => <BootstrapFieldSelect {...props} />,
renderValueSources: (props) => <BootstrapValueSources {...props} />,
};
const fields = {
"Pipeline": {
label: 'Pipeline',
tooltip: 'Pipeline',
type: '!struct',
subfields: GetPipelineQueryFields()
}
};
const funcs = {
...BootstrapConfig.funcs,
SUM: {
label: 'Sum',
returnType: 'number',
allowSelfNesting: true,
jsonLogic: ({firstNumber, secondNumber}) => ({ "+": [firstNumber, secondNumber]}),
jsonLogicImport: (v) => {
const args = v["+"];
return [...args];
},
renderSeps: [' + '],
args: {
firstNumber: {
label: "Number",
type: "number",
valueSources: ['field']
},
secondNumber: {
label: "Number or func",
type: "number",
valueSources: ['field', 'func']
},
}
},
MINUS: {
label: 'Subtraction',
returnType: 'number',
allowSelfNesting: true,
jsonLogic: ({firstNumber, secondNumber}) => ({ "-": [firstNumber, secondNumber]}),
jsonLogicImport: (v) => {
const args = v["-"];
return [...args];
},
renderSeps: [' - '],
args: {
firstNumber: {
label: "Number",
type: "number",
valueSources: ['field']
},
secondNumber: {
label: "Number or func",
type: "number",
valueSources: ['field', 'func']
},
}
},
MULTIPLY: {
label: 'Multiply',
returnType: 'number',
allowSelfNesting: true,
jsonLogic: ({firstNumber, secondNumber}) => ({ "*": [firstNumber, secondNumber]}),
jsonLogicImport: (v) => {
const args = v["*"];
return [...args];
},
renderSeps: [' * '],
args: {
firstNumber: {
label: "Number",
type: "number",
valueSources: ['field']
},
secondNumber: {
label: "Number or func",
type: "number",
valueSources: ['field', 'func']
},
}
},
DIVIDE: {
label: 'Division',
returnType: 'number',
allowSelfNesting: true,
jsonLogic: ({firstNumber, secondNumber}) => ({ "/": [firstNumber, secondNumber]}),
jsonLogicImport: (v) => {
const args = v["/"];
return [...args];
},
renderSeps: [' / '],
args: {
firstNumber: {
label: "Number",
type: "number",
valueSources: ['field']
},
secondNumber: {
label: "Number or func",
type: "number",
valueSources: ['field', 'func']
},
}
}
};
const config = {
ctx: BootstrapConfig.ctx,
conjunctions,
operators,
widgets,
types,
settings,
fields,
funcs
};
export default config; |
Please try version 6.4.1 to fix the error |
@ukrbublik seems working perfectly now! good and fast release ! |
I'm using a jsonlogic standard to create some queries, but on react query builder I didn't see a way to create custom operators like
plus
,minus
,multiply
with some constraintsMultiply
andplus
, can take multiple value (array [2,2,2,2] for example)Minus
andDivide
can take only two args (array [4,2] for example)I tried to add an element to the operators, but it's not showing (very simple without logic, but not showing at this time)
There is a way to create this kind of arithmetic operators ?
I saw a answer using #809 but I want to create an operator instead of function (easier to use and better UI for users)
Also, I tried to copy the
LINEAR_REGRESSION
and put the content in operators, but not showing anymoreIn JSONLOGIC, I need to generate something like :
Any idea ?
The text was updated successfully, but these errors were encountered: