To create a full-fledged dhxForm object you must make 4 steps.
Shortly, they look as:
For detailed 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 2 files:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" href="../codebase/dhtmlxform.css">
<script src="../codebase/dhtmlxform.js"></script>
</head>
<body>
...
</body>
</html>
If you use dhtmlxForm as a part of "dhtmlxSuite" package 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>
To include JS/CSS files of "dhtmlxSuite" package from CDN, you should set direct links to dhtmlx.js and dhtmlx.css files:
<link rel="stylesheet" href="http://cdn.dhtmlx.com/edge/dhtmlx.css"
type="text/css">
<script src="http://cdn.dhtmlx.com/edge/dhtmlx.js"
type="text/javascript"></script>
By setting links in this way you will get the latest version of dhtmlxSuite. To get some particular version, just specify the number of the required version in the link, like this:
<link rel="stylesheet" href="http://cdn.dhtmlx.com/5.0/dhtmlx.css"
type="text/css">
<script src="http://cdn.dhtmlx.com/5.0/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 the 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);
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 API.
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: