To create a full-fledged dhxForm object you must make 4 steps.
Shortly, they look as:
For verbose explanation we'll create a simple app (based on form) and show initialization details by an example.
Each DHTMLX component can be used standalone or as a part of the general library.
If you use dhtmlxForm standalone you need to include 3 files:
Note: if you are going to use DHTMLX based form controls (calendar, combo, colorpicker, editor), you'll need to add js files(s) from [dhtmlxForm]/codebase/ext folder. For more details see Dhtmlx Components Integration.
<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="../codebase/skins/dhtmlxform_dhx_skyblue.css"> <script src="../codebase/dhtmlxcommon.js"></script> <script src="../codebase/dhtmlxform.js"></script> </head> <body> ... </body> </html>
If you use dhtmlxFrom as a part of 'dhtmlxSuite' package (not for dhtmlxForm v.3.0 beta) you need to include 2 files:
<link rel="STYLESHEET" type="text/css" href="../codebase/dhtmlx.css"> <script src="../codebase/dhtmlx.js" type="text/javascript"></script>
The next step: an HTML container for your future form.
To implement this step we can choose one of the ways:
<div id="form_container" style="width:280px;height:250px;"></div>
Here we'll define form items and their positions.
For our form we need:
For all the items (except fieldset and newcolumn) we'll specify name attribute. We will use it later, for getting(setting) item's value.
Full list of available items(form's controls) and their attributes
formStructure = [ {type:"settings",position:"label-top"}, {type: "fieldset",name:"calculator", label: "Calculator", list:[ {type: "input", name: 'firstNum', label: 'First number:'}, {type:"input", name:"secNum", label:"Second number:"}, {type:"input", name:"resNum", label:"Result:"}, {type:"newcolumn"}, {type:"button", name:"plus", width:20,offsetTop:2, value:"+"}, {type:"button", name:"minus",width:20,offsetTop:10, value:"-"}, {type:"button", name:"multiply",width:20,offsetTop:10, value:"*"}, {type:"button", name:"divide",width:20,offsetTop:10, value:"/"} ]} ];
All the preparations are finished. Now you need to create a form instance.
In case of HTML container, initialization code will be as follows:
var myForm = new dhtmlXForm("form_container",formStructure);
If you put the form into DHTMLX container component the initialization code will be different (see details in Dhtmlx Components Integration):
myForm = layout.cells("a").attachForm(formStructure);
We've created all the needed elements but to work as calculator, our form must implement a number of actions: addition, subtraction, multiplication, division.
We'll add this functionality by means of onButtonClick event. The method attachEvent will help to attach event handler. For more details about events see Events Handling
myForm.attachEvent("onButtonClick", function(id){ var res, num1, num2; num1 = parseInt(myForm.getItemValue("firstNum"));// returns the value of item num2 = parseInt(myForm.getItemValue("secNum")); // returns the value of item if (id=="plus") //defines addition { res=num1+num2;} else if (id=="minus") //defines subtraction {res=num1-num2;} else if (id=="multiply")//defines multiplication {res=num1*num2;} else if (id =="divide")//defines division { if (num2==0) //if division by zero - generates a message {alert("Error.Division by zero!");res="";} else {res=num1/num2;} } myForm.setItemValue("resNum",res);// sets the value of item })
Used methods: