Using the Solution Model

Hi,

I need to base a form on a dataset, so I began using the solution model to create my form. However, I am finding it difficult to assign fields the way I want to in the new form.
I read that there is a way of extending an existing form instead of creating a form from scratch. Can anyone show a sample code of this?

So i basically want to add some columns i retrieve from a dataset into an existing form (say, a form currently without any fields).

Or is there a way of assigning my dataset columns to the data providers of existing fields?

Regards,
Hareendra

hareendra:
I read that there is a way of extending an existing form instead of creating a form from scratch.

You could use the code as shown below, just fill in the blanks.

var $smForm = solutionModel.newForm( name,  server_name|data_source,  [table_name],  style,  show_in_menu,  width,  height);
   	$smForm.extendsForm = <formname you want to extend>

hareendra:
So i basically want to add some columns i retrieve from a dataset into an existing form

This can not be done at the moment. A dataset-datasource can only be assigned with the 'solutionModel.newForm ’ function.
BUT… you can not use the extendForm function as this will mess up with your assigned dataset-datasource.
Servoy confirmed somewhere else on this forum that this will be fixed in the next release.
A workaround:

  1. create a form with all the ‘permanent’ fields and a tabpanel
  2. create a simple form using the dataset-datasource and place this on the tabpanel.

hareendra:
Or is there a way of assigning my dataset columns to the data providers of existing fields?

No, as it is impossible to assign a dataset-datasource to an existing form.

I think the code below illustrates how to start:

function createDs()
{
        // create dataset with named columns
	var $dataset = databaseManager.createEmptyDataSet( 5,  ['my_id', 'my_name']);
	
        // fill your dataset in some way, below is just test...
        for(var i=1; i <= $dataset.getMaxRowIndex() ; i++)
        {
	     $dataset.setValue(i,1,i);
             $dataset.setValue(i,2,'name ' + i);
	}

        // create a datasource from your dataset. Make sure you enter a columntype for each column.
	var $datasource = $dataset.createDataSource('mydata', [DM_COLUMNTYPE.INTEGER, DM_COLUMNTYPE.TEXT])

	var $newForm = 'yourNewForm';
	var jsform = solutionModel.newForm($newForm, $datasource, null,true, 300, 300);
	jsform.view = SM_VIEW.LOCKED_TABLE_VIEW;
	jsform.newField('my_id',SM_DISPLAYTYPE.TEXT_AREA,0,5,100,20);
        jsform.newField('my_name',SM_DISPLAYTYPE.TEXT_AREA,70,5,100,20);
	forms[$newForm].controller.show();
}

Hi,

Thanks, I began as you instructed. However I came across a few problems.

  1. I need to add this new form into an existing tab. The tab I have already contains two forms. I need to add this form to it at tab index 1.
    e.g forms.frm_main_form.elements.tab_main.addTab($newForm)?

  2. I want to assign a value list to one of the fields. But when I do $field1.valuelist=‘lst_orders’ i get an error “cannot convert ‘lst_orders’ to JSValueList”

  3. Some properties do not seem to get applied. For instance I added a row color calculation and it does not seem to work,
    jsform.rowBGColorCalculation = globals.rowBGColorCalculation();

  4. And, I need to add a check box for user selection. In a normal instance, I would set the check box data provider to a calculation in the table. How do I do it in this case?

  5. Finally, if I need to add a tittle header to the new form, and set an image in the tittle header section? I tried;
    jsform.newTitleHeaderPart(20);
    and adjusted the coordinates of the fields to allow for the additional height, but it does not display properly. The field text gets messed up.

Thanks in advance!
Regards,
Hareendra

hareendra:

  1. I need to add this new form into an existing tab. The tab I have already contains two forms. I need to add this form to it at tab index 1.
    e.g forms.frm_main_form.elements.tab_main.addTab($newForm)?

Just treat your solutionModel form as any other form. It has a name, so you can call it by using that name.
Be sure you create the form first before assigning it to a tab.

hareendra:
2) I want to assign a value list to one of the fields. But when I do $field1.valuelist=‘lst_orders’ i get an error “cannot convert ‘lst_orders’ to JSValueList”

$field1.valuelist needs a JSValueList to be assigned. There’s a function for that, I thought it was solutionModel.getValuelist(‘lst_orders’).
So $field1.valuelist=solutionModel.getValuelist(‘lst_orders’) should be more like the correct syntax.

hareendra:
3) Some properties do not seem to get applied. For instance I added a row color calculation and it does not seem to work,
jsform.rowBGColorCalculation = globals.rowBGColorCalculation();

I admit documentation could be better. I ran into the same problem last week.
I thought it should be like: jsform.rowBGColorCalculation = ‘globals.rowBGColorCalculation’.
So: drop de brackets and add quotes.

hareendra:
4) And, I need to add a check box for user selection. In a normal instance, I would set the check box data provider to a calculation in the table. How do I do it in this case?

We’re talking about a datasource here, so why don’t you create an extra column in your datasource which can be a placeholder for your checkbox?

hareendra:
5) Finally, if I need to add a tittle header to the new form, and set an image in the tittle header section? I tried;
jsform.newTitleHeaderPart(20);
and adjusted the coordinates of the fields to allow for the additional height, but it does not display properly. The field text gets messed up.

Hmmm, didn’t use this one yet. :? I would think there’s still something wrong with the placement of the elements which should be on the body, but I can be mistaken. As it’s hard to tell from your description where it goes wrong I think checking your code will maybe clarify the behaviour. Otherwise copy/paste the code and a screenshot…maybe I (or someone else can help you out)…

Good luck!

Hi,

I’m making progress. :)

Thanks for the help.

Now I need an on action function for my check box.
So I tried,
_checkField.setOnActionMethod(globals.onSelected);

Apparently, it only takes a global function…

So I need to pass the user selected entry values as parameters to the global function.

But as soon as I try to pass parameters i get an error, “Can’t find method com.servoy.j2db.scripting.solutionmodel.JSField.js_setOnActionMethod…”

Can you give a code example of how to do this?
assume the fields I have created are $field1, $field2

And on the title header issue,
I added the header as;
jsform.newTitleHeaderPart(30);
var _image = jsform.newImageMedia(‘title_bar.gif’,10,10,500,16);
_image = SM_MEDIAOPTION.KEEPASPECT;
But the image i’m referring is not displayed. Only a grayed area is shown for the given height, width and cordinates. The image is in image media of a module in my solution. Any Idea?

Thanks!

Hareendra

hareendra:
Apparently, it only takes a global function…

That’s right… :)

hareendra:
But as soon as I try to pass parameters i get an error,

arguments are not allowed as you don’t even have the brackets to place them…
In a ‘real’ form you don’t have the ability either, that should be your startingpoint.
You could dynamicaly create form variables and read those in the global method.
Syntax of setting on action method should be something like

$smForm.getField([yourfield]).setOnActionMethod([globals.youractionmethod]);

hareendra:
But the image i’m referring is not displayed. Only a grayed area is shown for the given height, width and cordinates. The image is in image media of a module in my solution. Any Idea?

hmmm… servoy refers to: .newImageMedia(dataprovidername/jsvariable, number x, number y, number width, number height)
So it looks like your image should be either dataprovidername or jsvariable and not the name of the media entry.

var _media = solutionModel.getMedia('title_bar.gif');
var _image = jsform.newImageMedia(_media,10,10,500,16);
_image = SM_MEDIAOPTION.KEEPASPECT;

We attached media to a label on a form, see the example below:

$smLabel = $smForm.newLabel('titlebar',10,10,500,16);
$smLabel.text			= '';
$smLabel.imageMedia	= solutionModel.getMedia('title_bar.gif');
$smLabel.mediaOptions	= SM_MEDIAOPTION.CROP;
$smLabel.showClick 	= false;
$smLabel.showFocus 			= false;

Good luck again!

p.s. a lot of mistakes (it also happened to us) occur messing up referring to text or to a jsObject. We try to avoid that by storing text in vars like ‘$varname’ and jsObjects in vars like ‘$jsVarname’ or ‘$smVarname’
you might consider this difference in declaring your variables…

Hi,
again!

Ok finally I got it to do what I want. But there is one issue;
I call my create form function in the on show event of my main form. When it loads the first time its ok. But when I click on a different form, and then select the fist form again, i get an error: The name ‘frm_new_form’ already exists as another form
the error occurs on the line where im doing; solutionModel.newForm()

So I tried to do solutionModel.removeForm(‘frm_new_form’), prior to solutionModel.newForm();
However this still doesn’t seem to have any effect.
The sample code tells to use history.removeForm(‘frm_new_form’) before solutionModel.removeForm(), which I tried but still with the same result.

Any idea?

Thanks

Regards,
Hareendra

hareendra:
So I tried to do solutionModel.removeForm(‘frm_new_form’), prior to solutionModel.newForm();
However this still doesn’t seem to have any effect.
The sample code tells to use history.removeForm(‘frm_new_form’) before solutionModel.removeForm(), which I tried but still with the same result.

Hi Hareendra,

You are getting the issue as because you are creating the form with same name(frm_new_form), which is already present.

You can remove the form (solutionModel.removeForm(‘frm_new_form’)) after the working with the form or after you navigating away from that form. So, when you again create(solutionModel.newForm()) the form, then that form has already been removed, so you won’t get any error.