Creating calculations on the fly

Hello,

is there any possibility to create calculations field on the fly? How can i do this?

Hi,

Yes you can create a new calculation on the fly using the SolutionModel (solutionModel.newCalculation()) in Servoy 6.x.
But what are you trying to do?

I tried with servoy 6.0.6 but how can i associate this calculation with a dynamic textfiled?

it’s a long story … :)

Hi,

When you create a new calculation you define it with a functionname. This function name is your dataprovider name.
This also means that if you define a function name that matches a column name in the datasource you are defining it on it will be a stored calculation.

I tested the following code in 6.0.6 and it works fine.

var _oForm = solutionModel.getForm(controller.getName()),
    _oLabel,
    _oCalc;
    
// check if calculation already exists
if (!foundset['myCustomCalc']) {

    // create calculation. Name of the function is the name of the calculation
    _oCalc = solutionModel.newCalculation("function myCustomCalc() {return column1 + ":" + column2;}", JSVariable.TEXT, controller.getDataSource());
    
    // Add the label to the form with the newly created calculation
    _oLabel = _oForm.newLabel("Calc", 150, 0, 80, 20); // titletext, x, y, width, height
    _oLabel.dataProviderID = _oCalc.getName();
    
    // recreate the UI of the form
    controller.recreateUI();
}

Hope this helps.

Hello!
I tried with a textField but nothing…i can’t see nothing into the textfiled…it’s empty …

Can you show us your code ?
Like I said the code I posted worked for me.

Hi,

I changed a few details (“:” gave a syntax error) and replaced the label with a textbox and used example_data.order_details as the datasource for my testform. newCalculation is deprecated but that doesn’t matter. Now it works:

	// check if calculation already exists
	var _oForm = solutionModel.getForm(controller.getName()),
    _oField,
    _oCalc;
	if (!foundset['myCustomCalc']) {

	    // create calculation. Name of the function is the name of the calculation
	    _oCalc = solutionModel.newCalculation("function myCustomCalc() {return quantity * unitprice;}", JSVariable.TEXT, controller.getDataSource());
	    
	    // Add the label to the form with the newly created calculation
	    _oField = _oForm.newField("_oCalc",JSField.TEXT_FIELD,150,0,80,20); 
	    _oField.dataProviderID = _oCalc.getName();
	    
	    // recreate the UI of the form
	    controller.recreateUI();
	}