Is it possible to create a Boolean DataProvider on the fly?
Looking through the documentation, it seems you’re supposed to use form.newFormVariable(name,type), but the type only accepts DATETIME, INTEGER, MEDIA, NUMBER, or TEXT.
Basically I am creating a data-driven form, creating checkboxes on the fly, and need a Boolean dataprovider to attach them too.
I know this works statically, as I have used it just fine.
var ds = databaseManager.createEmptyDataSet();
ds.addColumn('bool');
// add data
ds.addRow([true]);
ds.addRow([false]);
var tmpDataSource = ds.createDataSource('temp');
The datasource must be a public variable, to access in a public function.
Seems like your method is creating a private datasource, limited to the function it’s contained in?
To do it statically, you can simply declare a variable and set the @type.
ex:
/**
@type {Boolean}
@properties { etc etc }
**/
var cbDataSource = null;
And in the design view, you can simply set the datasource to your global variable.
I have a function that runs and checks to see which of those dynamic checkboxes are checked, and performs an operation depending on each.
Sorry, as I meant I need to create a DataProvider on the fly that can be used in an external function.
I need do to create the dataprovider and attach it to the dynamic check boxes, so when i submit the form I can pull in if each box is checked or not.
Well it required some trickery but I got around the incomplete JSVariable constant specification. These constanst are actually just readable replacements for the numbers that get returned when they are used. When you add a boolean form var to a normal form you will see that it gets a variable id of -4 (in the JSDoc line). So here’s a small sample that shows how to do it:
var dataSet = databaseManager.getDataSetByQuery('example_data','select * from customers',null,200);
var dataSource = dataSet.createDataSource('myDataSource');
var form = solutionModel.newForm('myForm',dataSource,null,false,800,600)
form.newVariable('_bool', -4);
var mybool = form.getVariable('_bool');
mybool.defaultValue = true;
application.output("Variable: " + typeof forms['myForm']._bool);
forms['myForm'].controller.show();
Actually while looking at it some more I discovered that -4 is what looks like the general specification for the “Object” type. I guess initializing the variable with a default value is what really matters.
Actually, like the Chris figured out in the beginning, there’s no Boolean…
You should use Integer, that’s how the Checkbox works. It always returns 0 or 1 no matter the dataprovider type.
jbrancoIF:
Actually, like the Chris figured out in the beginning, there’s no Boolean…
You should use Integer, that’s how the Checkbox works. It always returns 0 or 1 no matter the dataprovider type.
I actually had the thought pass through my head yesterday evening, but that does not work.
It creates the integer variable properly, and assigns it as the DataProvider for that checkbox, but since it’s an integer, it will not let you check the checkbox.
Seems the only way you are able to check the checkbox, is if it’s been assigned a Boolean dataprovider.
That must be something else in your code, because I just tested that with a simple form with one checkbox with an Integer form variable as Dataprovider and it works!
omar:
Well it required some trickery but I got around the incomplete JSVariable constant specification. These constanst are actually just readable replacements for the numbers that get returned when they are used. When you add a boolean form var to a normal form you will see that it gets a variable id of -4 (in the JSDoc line). So here’s a small sample that shows how to do it:
var dataSet = databaseManager.getDataSetByQuery('example_data','select * from customers',null,200);
var dataSource = dataSet.createDataSource('myDataSource');
var form = solutionModel.newForm('myForm',dataSource,null,false,800,600)
form.newVariable('_bool', -4);
var mybool = form.getVariable('_bool');
mybool.defaultValue = true;
application.output("Variable: " + typeof forms['myForm']._bool);
forms['myForm'].controller.show();
Are you going to file a case or shall I?
I tried using your method, but still am unable to check the checkbox.
I’m not sure a JSVariable is going to work unless I manually create global variables with @type {Boolean} with a naming scheme I can mimic in the onLoad build function.
You don’t get any errors? What Servoy version are you using?
I don’t find any JSForm.newVariable() method.
And the defaultValue should be a String that will be interpreted at runtime as the real default value.
And the -4 in the newFormVariable is the same as JSVariable.MEDIA if you want to use it instead of the JSVariable.Integer.
My test code:
var newForm = solutionModel.newForm('TEST', null, '', false, 200, 200);
var newVar = newForm.newFormVariable('checkDP', JSVariable.INTEGER);
newVar.defaultValue = '0';
var newCheck = newForm.newCheck(newVar,10,10,100,10);
newCheck.titleText = 'ABC';
newCheck.transparent = true;
newCheck.fontType = 'Arial,plain,11';
var form = forms['TEST'];
form.controller.show();
jbrancoIF:
You don’t get any errors? What Servoy version are you using?
I don’t find any JSForm.newVariable() method.
And the defaultValue should be a String that will be interpreted at runtime as the real default value.
And the -4 in the newFormVariable is the same as JSVariable.MEDIA if you want to use it instead of the JSVariable.Integer.
My test code:
var newForm = solutionModel.newForm('TEST', null, '', false, 200, 200);
var newVar = newForm.newFormVariable(‘checkDP’, JSVariable.INTEGER);
newVar.defaultValue = ‘0’;
var newCheck = newForm.newCheck(newVar,10,10,100,10);
newCheck.titleText = ‘ABC’;
newCheck.transparent = true;
newCheck.fontType = ‘Arial,plain,11’;
var form = forms[‘TEST’];
form.controller.show();
I am using Servoy 6.1, where newFormVariable was deprecated for newVariable.
Tried switching it to JSVariable.INTEGER, with a defaultValue of ‘0’, and it still will not allow me to check the checkbox.
Using the command console, I can see the dataproviders are correctly being generated. But for whatever reason, creating the DP on the fly, is not allowing the checking of the checkboxes.
omar:
Maybe the default value should not be ‘0’ but 0?
That’s not the problem.
I checked with 6.0 and 6.1 and both do the same thing. I can’t tell if it’s a bug or not.
The problem is that the recreateUI is not creating the new variables on the form. It creates the checkBox as expected but not the variable that is the dataprovider.
Usually when you change the solutionModel of a form you should do it before it’s loaded. The onLoad method maybe it’s a bit late. That’s why my code worked, because I created a new form, didn’t change one on the onLoad.
I can’t remember if there’s something else apart from controller.recreateUI(); to reload all the form(a variable is not UI in reality…) and create the variable like we want.
As a workaround, you can do form[‘newFormName’].newDataProvider = 0; after the recreateUI call for each new variable and it works.
omar:
Maybe the default value should not be ‘0’ but 0?
That’s not the problem.
I checked with 6.0 and 6.1 and both do the same thing. I can’t tell if it’s a bug or not.
The problem is that the recreateUI is not creating the new variables on the form. It creates the checkBox as expected but not the variable that is the dataprovider.
Usually when you change the solutionModel of a form you should do it before it’s loaded. The onLoad method maybe it’s a bit late. That’s why my code worked, because I created a new form, didn’t change one on the onLoad.
I can’t remember if there’s something else apart from controller.recreateUI(); to reload all the form(a variable is not UI in reality…) and create the variable like we want.
As a workaround, you can do form[‘newFormName’].newDataProvider = 0; after the recreateUI call for each new variable and it works.
You are on to something here.
Needs to be a way to refresh variables on the form too.
Just verified it works by doing form[‘ManageUsers’].administratorsDP = 0; right before or after the recreateUI.
Though this will not work for the end solution as these variables should be setup inside the for loop inside the onload function.
Removes the data-driven aspect to have to manually call those variables again.