Showing posts with label Dojo. Show all posts
Showing posts with label Dojo. Show all posts

Tuesday, August 11, 2009

Dojo: Creating image using dojo script

Dojo provides numerous functionality like:
1.Easy and smooth creation of different widget such as dojo grid, forms, datapicker, calendar etc.
2.Prolific method of interacting with html elements.
3.Achievement of ajax functionality with far more less code.
In this article I am going to discuss how to create an image element using dojo script and attach that image to certain html element.
In order to achieve functionality on loading the page, you will need to use dojo.addOnLoad() method. This takes the following form.
<script>
dojo.addOnLoad(function(){
alert(‘called on load of page’);
});
</script>


Now in the next few lines we are going to put the actual functionality.
<script>
dojo.addOnLoad(function(){
dojo.extend(dojo.NodeList, {
addImage: function() {

return this;
}
});
});
</script>

The most important things here are extending dojo.NodeList and then adding addImage function that initially has nothing, but we are going to add real functionality to it later on. This addImage method will contain the functionality for creating an image.
The question here however is, why we extend NodeList.
Well this is pretty simple. If you want to achieve functionality like;
dojo.query(‘input[id=name]’).addImage().removeClass(‘complusory_image’);
you will need to extend you method from NodeList. Doing so will enable you to access entire NodeList method in the way above.

Don’t worry about dojo.query() method, I will explain it shortly for you.
Now lets put the real script for creating img tag in our addImage method.
var img = dojo.doc.createElement('img');
dojo.attr(img, {
src: "path/to/imges/img.png",
alt: "This is my simple image",
style: {cursor: "pointer"}
});

In the first line we have created img tag. In the second line we have attached different attributes to the img. You can define as many valid attributes associated with img tag as you wish.

The code in the addImage function looks, however, a bit different. See the following code.
<script>
dojo.addOnLoad(function(){
dojo.extend(NodeList, {
addImage: function(args){
this.forEach(function(n){
var img = dojo.doc.createElement('img');
dojo.attr(img, {
src: "path/to/imges/img.png",
alt: "This is my simple image",
style: {cursor: "pointer"}
});
dojo.place(img, n, "after");
});
return this;
}
});
});
</script>

The function for addImage contain args as parameter. This means that the image tag created can be attached to as many html elements as you like. In the forEach loop we create image tag and attach at to the arguments passed to it.
Now in the dojo.addOnLoad function, at the end, write the following script.
dojo.query(‘input[id=name]’).addImage();

The above script states that attach an image to input tag having id equals to name.
It you want to attach element to all input tags on your page, write
dojo.query(‘input’).addImage(); 

The final script on my page look like this.
<script>
dojo.addOnLoad(function(){
dojo.extend(NodeList, {
addImage: function(args){
this.forEach(function(n){
var img = dojo.doc.createElement('img');
dojo.attr(img, {
src: "path/to/imges/img.png",
alt: "This is my simple image",
style: {cursor: "pointer"}
});
dojo.place(img, n, "after");
});
return this;
}
});
dojo.query("input[id=name]").addImage();
});
</script>

Friday, July 31, 2009

Creating dojo datepicker with dojo script

Dojo date picker is nice and beautiful widget. The nice thing about any dojo widgets is that they can be easily attached to appropriate html tag. To assign date picker to input box take the following cod.


<html>
<head>
<script src=”path/to/dojo.js”>
require(‘dijit.form.DateTextBox’);
</script>
<style type=text/css>
@import "path/to/dojo/dijit/themes/tundra/tundra.css";
</style>
</head>
<body class= “tundra”>
<input type=”text” dojoType=”dijit.form.DateTextBox” name=”dd” id=”dd”/>
</body>
</html>



If you correctly include dojo.js and tundra.css, you will see a nice

Read More

Monday, July 27, 2009

Zend Framework, dojo and Json

I have already written about json in one, actually some of my articles. Cool functionality can be achieved using json. Thanks to http://www.json.org, http://www.zend.com and http://www.dojotoolkit.org for provide easier solutions.

I'm not gonna talk what these three are all about, but instead share a little secret about these.
Json creation in dojo is as simple as this.
var arr = new Array();
arr[0] = “apple”;
arr[1] = “ball”;
arr[2] = “cat”;

dojo.toJson(arr);


Using Zend to convert array into json took only one line of code.
Zend_Json::encode($arr);

And decoding- converting json to array, is as simple as this
Zend_Json::decode(json_string);

Monday, July 13, 2009

Create dojo tab cotainer in Zend Framework

Try online: Tab Container
In my previous posts I discussed how to create accordion container holding accordion panes and border container having content panes.
In this article I am going to show you how easy it is to create tab continer that hold contents panes. Each content pane having different contents. Contents in one tab pane are shown at a time. when use click on the tab pane's title, contents in that tab pane are shown.
Before reading this post, you better read and implement what I have discussed in my article "Zend Framework and Dojo: configuration". Before creating tab continer and panes, you will need to understand and perform necessary configuration.I assume that you have read the post I have mentioned and have done the necessary configuration. Now let's get started.
First of all, you will need to create your controller as


<?php
class ContainerController extends Zend_Controller_Action
{
public function tabAction()
{

}
}


In the code above we have defined our ContainerController extending it from Zend_Controller_Action, and then we have defined our tabAction.
Now, the next step is to create tab.phtml in /application/views/scripts/container/ and place the following code in it.
<?php

$this->tabContainer()->captureStart('main-container',
array('design' => 'headline'),
array(
'style'=>'height:400px;width:800px'
));

echo $this->contentPane(
'home',
'This is home page',
array('region' => 'top','title'=>'Home'),
array('style' => 'background-color: white;')
);

echo $this->contentPane(
'gallery',
'Contents of gallery here',
array('region' => 'left', 'title'=>'Gallery'),
array('style' => 'width: 200px; background-color: white;')
);

echo $this->contentPane(
'Blog',
'Blog post here',
array('region' => 'center','title'=>'Blog'),
array('style' => 'background-color: white;')
);

echo $this->contentPane(
'about',
'Information about your company etc',
array('region' => 'bottom', 'title'=>'About Us'),
array('style' => 'background-color: white;')
);

echo $this->tabContainer()->captureEnd('main-container');
?>


Here we first call captureStart() method of tabContainer() dojo view helper, giving it an id, and array containing tab container special attributes. Next we define content panes. Each content pane holds the data. The contents panes in the tab container will become tab panes.
each content pane is defined as
echo $this->contentPane(
'home',
'This is home page',
array('region' => 'top','title'=>'Home'),
array('style' => 'background-color: white;')
);

Here "home" is the id of the content page, Next "string" is the contents that will appear in the tab pane. Next we have defined the array of the special attributes of the contents. Here title is the most important attribute. This title will appear on the tab. The last array contain css related attributes.
That's it. we have now written all necessary code for creating nice and beautiful Tab container.
Cheers.

Sunday, July 12, 2009

Creating dojo border container in Zend Framework

Try Online : Border Container
In my previous artilce I discuss how to create accordion container and panes. If you wish to create accordion container and panes, read my article "Creating dojo accordion Container in Zend".
If you don't know how to configure dojo and Zend Framework, read here.
In this article, however I am going to discuss how to create border container. Border continer divide our page in different section so that we can display different contents in those sectitons.

To create the border container we first need to create our controller as


<?php
class ContainerController extends Zend_Controller_Action
{
public function borderAction()
{

}
}


Now in your /application/views/scripts/container/border.phtml, place the following code.
<?php

$this->borderContainer()->captureStart('main-container',
array('design' => 'headline'),
array(
'style'=>'height:400px;width:800px',
'align' => 'center'
));

echo $this->contentPane(
'menuPane',
'This is the menu pane',
array('region' => 'top'),
array('style' => 'background-color: darkblue;color:white')
);

echo $this->contentPane(
'navPane',
'This is the navigation pane',
array('region' => 'left'),
array('style' => 'width: 200px; background-color: lightblue;')
);

echo $this->contentPane(
'mainPane',
'This is the main content pane area',
array('region' => 'center'),
array('style' => 'background-color: white;')
);

echo $this->contentPane(
'statusPane',
'Status area',
array('region' => 'bottom'),
array('style' => 'background-color: lightgray;')
);

echo $this->borderContainer()->captureEnd('main-container');
?>


Here we have called captureStart() method to start our border container, and then we have placed different content panes.
Each content pane has an "id", "contents" it holds, array containing content panes attributes, and an array containing css attributes.

Creating dojo accordion Container/panes in Zend

Try Online : Accordion Container
Dojo provide very easy and nice way of creating different containers such as accordion, tab, border, and stack etc.
These containers hold different type of panes. These different panes in turn use to hold contents. contents can be simple text or it can be complete html.
Zend Framework collaboration with Dojo has made things quite easy. Zend Framework provide view helpers to achieve different type of dojo functionality.
In this article I am going to discuss how to create Accordion Container. accordion Container hold accordion panes. Each accordion pane hold different contents and open in a stylish way when user click on the title of the pane.
In order to enable dojo helpers in you Zend Framework application, you will need to make some initial configuration. read my tutorial "Zend Framework and Dojo: configuration" before reading this tutorial. After successfull configuration create a controller called "ContinerController.php" in your controllers directory and place the following code in it.


<?php
class ContainerController extends Zend_Controller_Action
{
public function accordionAction()
{

}
}


We have only defined an action in our controller, putting no code at all. you however can put whatever code you wish.
Now in /application/views/scripts/container/accordion.phtml place the following code.

<?php

$this->accordionContainer()->captureStart('main-container',
array('design' => 'headline'),
array(
'style'=>'height:400px;width:400px'
));

echo $this->accordionPane(
'home',
'This is home page',
array('region' => 'top','title'=>'Home'),
array('style' => 'background-color: white;')
);

echo $this->accordionPane(
'gallery',
'Contents of gallery here',
array('region' => 'left', 'title'=>'Gallery'),
array('style' => 'background-color: white;')
);

echo $this->accordionPane(
'Blog',
'Blog post here',
array('region' => 'center','title'=>'Blog'),
array('style' => 'background-color: white;')
);

echo $this->accordionContainer()->captureEnd('main-container');
?>


The code is pretty simple. we first call captureStart() method on accordionContainer(). this will start accordion container for us.
Once the accordion continer is started, we need to put accordion panes in it and at the end we will need to call captureEnd() method to end our accordion container.
The container holding the following accordion pane.
echo $this->accordionPane(
'home',
'This is home page',
array('region' => 'top','title'=>'Home'),
array('style' => 'background-color: white;')
);

The above code call accordionPane() helper method. This dojo helper method take an "id", "contents" to be displayed in the accordion pane, an array containing the special attributes of the accordion pane, and array of css attributes.

Wednesday, July 1, 2009

Zend Framework and dojo checkbox example

Few weeks back, I used dojo check box in my Zend framework application. Scenario was a bit different at that time. I was submitting Zend dojo form on the click event of the checkbox. When I created a form and submitted that onclik, It didn’t give me desired results.
I set its checked value to 1 and unchecked value to 0, however when I submited it always gave me a single value. I was looking for 0 and 1, but it always gave me 0. I thought that It was because I was not using proper submit button for the posting of form.
However when today I used checkbox in normal zend dojo form, it gave me the same result. Oh God, how to fix it.
However as I have played with dojo js files, so I got the solution.
Let’s tell you the trick.
First of all create check box as
$checkbox= $form->createElement(
'checkBox',
'checkbox',
array(
'label' => 'Tricky check box',
'checkedValue' => 1,
'uncheckedValue' => 0
)
);

Now add onclick event to this checkbox element as
$checkbox->setAttrib('onclick','changeValue()');

Now in your view, where you display this form, write the following javascript function
<script>
function changeValue()
{
if(dijit.byId('checkbox').checked){
dijit.byId('checkbox').setValue('1');
} else {
dijit.byId('checkbox').setValue('0');
dijit.byId('checkbox').checked=false;
}

}
</script>

The above code isn’t simple?

Hmm. First we see if we have checked the checkbox, if yes we assign value 1 to it, if not we assign it value 0 and set its checked status to false. This will uncheck the checkbox.

That’s it. Submit your form and you will get desired results.

Doesn’t it help you. Do you have smarter solution than this.

Tell me……

Zend Framework and dojo- Auto Complete Example

Creation of auto complete in Zend Framework using dojo is like a piece of cake. In one of my previous post I discuss how to create filtering select and populate that using dojo and ajax.
Here I am going to discuss how to create auto complete with combo box. The auto complete textbox will automatically/dynamically fetch data from the database.
So lets get started.
First of all create following functions in you controller.
public function getForm()
{
if (null === $this->_form) {
$this->_form = new Zend_Form();
$this->_form->setMethod('get')
->setAction($this->getRequest()->getBaseUrl() . '/test/process')
->addElements(array(
'test' => array(
'type' => 'text',
'options' => array(
'filters' => array('StringTrim'),
'dojoType' => array('dijit.form.ComboBox'),
'store' => 'testStore',
'autoComplete' => 'false',
'hasDownArrow' => 'true',
'label' => 'Your input:',
)),
'go' => array('type' => 'submit',
'options' => array('label' => 'Go!'))
));
}
return $this->_form;
}

In the above code define a function and create a form adding it a combo box.
The important attributes of the combo box are
dojoType, store and autocomplet.
Next create the following actions in your controller where you defined getForm() method.
class DojoController extends Zend_Controller_Action
{
public function autocompleteAction()
{
$this->view->form = $this->getForm();
}
public function recordsAction()
{
}
}

In the above code we get the form by calling the method defined and assign it to the view template.
Next in the view template file write.
Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
$this->dojo()->setLocalPath('http://localhost/z/js/dojo/dojo/dojo.js')
->addStyleSheetModule('dijit.themes.tundra')
->addStylesheet('http://localhost/z/js/dojo/dojox/grid/_grid/tundraGrid.css');
echo $this->dojo();
?>
<script type="text/javascript">
dojo.require("dojox.data.QueryReadStore");
dojo.require("dijit.form.ComboBox");
dojo.require("dojo.parser");
</script>
<div dojoType="dojox.data.QueryReadStore" jsId="testStore" url="records"></div>
<?
echo $this->form;
?>

In the code above we first tell zend that we want to use declarative mode than programmatic which is default.
Next we set local path to dojo.js and necessary css and theme.
Next we specify which dojo js files we require.
The most important thing in the above code is
<div dojoType=”dojox.data.QueryReadStore” jsId=”testStore” url=”records”></div>

Here we specify dojoType and jsId. Keep in mind that the jsId is same to the combobox store attribute.
Another important thing is url of the div. this url is used by dojo QueryReadStore to pull data from the action.
To populate our combo box in the records action of the Dojo Controller write the following.
public function recordsAction()
{
$db=Zend_Registry::get('db');
$sql = 'SELECT name FROM company';
$result = $db->fetchAll($sql);
$dojoData= new Zend_Dojo_Data('name',$result,'id');
echo $dojoData->toJson();
exit;
}

The above code is simple to understand. We first get reference to the db object store in the bootsrap.
Next we write sql. Keep in mind that I have company table with name field in my database.
Next I fetch all data(name). give it to Zend_Dojo_Data. And convert resultant data to Json.
That’s it.
Cheers.

Tuesday, June 30, 2009

Zend Framework and Dojo: configuration

Latest version of Zend framework- ZF 1.6.0 ship dojo toolkit. You can find it in ZendFramework-1.6.0/external/ directory when you download ZF.

Recently I come across a problem using third party “calendar”- Java script Calendar, and as Zend has now done collaboration with Dojo, so people using zend will definitely start using Dojo for java scripts and all other functionality Dojo is providing. And that’s why I decided to study and use Dojo and implement it in my application instead of fixing the issue in the third party calendar.

Although Zend has done excellent job and has made things quite easy for those who wish to use dojo in their applications, however novice and those with little experience may find it a bit difficult to configure Zend for Dojo.

It this post I will tell you how to configure Zend for working with Dojo.

The first and most important thing is to copy “external/dojo” into your js directory under www/public folder.

After you copy the dojo folder add the following code in your bootstrap.
    $view = new Zend_View();

$view->addHelperPath(’Zend/Dojo/View/Helper/’, ‘Zend_Dojo_View_Helper’);

$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();

$viewRenderer->setView($view);

Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);


Ignore the first line $view= new Zend_View() if you have already instantiated view object in your bootstrap file.

That’s it. You have now added path to Dojo view helpers. You can now access dojo view helpers in your template-phtml file as you access other helper files and their functionality.

You job has not done yet.

Next you would need to add path to dojo.js file and other module of the dojo toolkit.

So open your layout file and put following code.
    if($this->dojo()->isEnabled()) {

$this->dojo()->setDjConfigOption(’usePlainJson’,true)

->addStylesheetModule(’dijit.themes.tundra’)

->setLocalPath(”http://localhost/z/js/dojo/dojo/dojo.js”);

echo $this->dojo();

}


As you can see we are adding path to js and stylesheet files so put the above code in <head> tag of your layout file.

The only thing you need to do is to enable dojo in your template file.

Let suppose you have controller “IndexController”. So in the views/scripts/index/index.phtml file put the following code.
    $this->dojo()->enable()

->requireModule(”dijit.form.DateTextBox”);


I would like to explain the last line ->requireModule(’dijit.form.DateTextBox’). This line tells php to load DateTextBox. DateTextBox render textbox that display calendar when someone click on the field.

Your configuration is now completed. Now create your form as follows.
class MyForm extends Zend_Dojo_Form

{

public function init()

{
$this->setMethod('post');

$this->setName('myform');

$this->addElement(
'DateTextBox',
'foo',
array(

'label' => 'Date:',
'required' => true,
'invalidMessage' => 'Invalid date specified.',
'formatLength' => 'long',
)

);

}

}

In your controller, write
    $form= new MyForm();

$this->view->form=$form;


In your view template
 echo $this->form;


The last thing I’d mention is, In your layout file replace “body” tag with
body class=”tundra”