Nitobi
Customer Login
Services
Products
About
Blogs
Contact
Home -> Product Knowledgebase Order Online | Free Trial Downloads

Nitobi Product Knowledgebase

Basic Grid V3 Tutorial


This simple walkthrough will show you how to put a grid on a webpage, and connect to a datasource. In this instance, the datasource is either an ASP, PHP, Java, or Coldfusion page that returns XML data.

index.html

First, you must always define the Nitobi XML namespace in the <HTML> tag.

<HTML xmlns:ntb>

Then, write out the head tag.

<head>

Be sure to include the CSS style sheet. We ship several different styles which you can choose from.

<link type="text/css" rel="stylesheet" class=BodyText rel=nofollow href="styles/nitobi/nitobi.grid.css">

Then, include the JavaScript file for the Grid control and the Toolkit framework.

<script language="JavaScript" src="nitobi.toolkit.js"></script>
<script language="JavaScript" src="nitobi.grid.js"></script>

Next, put a grid on the page by writing out the grid tag. The tag is an XML-like declarative markup that allows you to define all the attributes of the grid all in one concise, easy to use format. Here is an example of a very basic grid that binds to an on-page XML datasource which you will see.

<ntb:grid id="SimpleGrid"
  width="500"
  height="300"
  mode="locallivescrolling"
  datasourceid="data"
  toolbarenabled="true">
 <ntb:datasources>
 <ntb:datasource id="data">
  <ntb:datasourcestructure FieldNames="Name|FavColor|FavAnimal|Donation"></ntb:datasourcestructure>
  <ntb:data>
   <ntb:e xi="1" a="Tammara Farley" b="blue" c="cat" d="23" ></ntb:e>
   <ntb:e xi="2" a="Dwana Barton" b="red" c="dog" d="264563" ></ntb:e>
   <ntb:e xi="3" a="Lucas Blake" b="green" c="ferret" d="24563" ></ntb:e>
   <ntb:e xi="4" a="Lilli Bender" b="grey" c="squirrel" d="25723" ></ntb:e>
   <ntb:e xi="5" a="Emilia Foster" b="orange" c="pig" d="89823" ></ntb:e>
   <ntb:e xi="6" a="Steve Irwin" b="beige" c="crocodile" d="283" ></ntb:e>
   <ntb:e xi="7" a="Crystal House" b="brown" c="horse" d="235" ></ntb:e>
   <ntb:e xi="8" a="Lindsay Cohen" b="cyan" c="cow" d="63" ></ntb:e>
   <ntb:e xi="9" a="Blake Farley" b="blue" c="cat"  d="657" ></ntb:e>
   <ntb:e xi="10" a="Farley Barton" b="red" c="dog" d="34" ></ntb:e>
   <ntb:e xi="11" a="Bender Dwana" b="green" c="ferret" d="63" ></ntb:e>
   <ntb:e xi="12" a="Lindsay Bender" b="grey" c="squirrel" d="44634" ></ntb:e>
   <ntb:e xi="13" a="Emilia Foster" b="orange" c="pig" d="324" ></ntb:e>
   <ntb:e xi="14" a="Dwana Irwin" b="beige" c="crocodile" d="4325" ></ntb:e>
   <ntb:e xi="15" a="Steve Lilli" b="brown" c="horse" d="6546" ></ntb:e>
   <ntb:e xi="16" a="Lindsay Dwana" b="cyan" c="cow" d="456" ></ntb:e>     
  </ntb:data>
 </ntb:datasource>         
  </ntb:datasources> 
</ntb:grid>

This example will generate a grid of width 500 pixels, and height 350 pixels. After the opening ntb:grid tag we include a <ntb:datasources> tag which can be used to define any static datasources we may be using. We call our one datasource "data". In the grid tag we link to it with the datasourceid="data" attribute.

If we run the page now, the grid will not appear. This is because we still need to initialize it. We do this with a small piece of JavaScript code. You can do this in the BODY onLoad event, or some other time. The following code will initialize the grid named "SimpleGrid". Note that this code appears in the <BODY> tag of our page so that it runs when the page loads.

<body onLoad="nitobi.loadComponent('SimpleGrid');">

In this grid tag example we do not explicitly define any columns, so the grid will infer that every column it finds in the data should be drawn and that they are all text values. If we run the page now we will see a grid like this:

Customizing the Column Definitions

Now we would like to customize this grid a little for the data. For example, it would be nice to rearange the columns and add a currency mask on the donation field. To do this, we will explicitely define columndefinitions.

Somewhere inside our <ntb:grid> tag we must define a <ntb:columns> tag with column information in it. We begin by making them all text:

<ntb:columns>  
  <ntb:textcolumn  label="Donor name"  xdatafld="Name"></ntb:textcolumn>
  <ntb:textcolumn  label="Donation Amount" xdatafld="Donation" ></ntb:textcolumn>
  <ntb:textcolumn   label="Favourite Color" xdatafld="FavColor" ></ntb:textcolumn>
  <ntb:textcolumn  label="Favourite Animal" xdatafld="FavAnimal" ></ntb:textcolumn>    
</ntb:columns>

This won't do anything for our Donation Amount currency mask: it will still just look like text. Let's make it a number coloumn and add a mask like so:

<ntb:numbercolumn label="Donation Amount" xdatafld="Donation" mask="$#,##0" width="120"></ntb:numbercolumn>

Notice we changed textcolumn to be numbercolumn on both the opening and closing tags (don't forget this - common mistake). We also added a width attribute to make it a bit wider than the default (100px).

Now we have a grid that is  formatted the way we want.

Click here to download the file.

Related Articles

View Printable Version

Comments:


Poster: Aidan Taylor (Friday, June 22, 2007)

Basic sample does not work - line 4064 exception thrown and not caught. Using trail download PHP. I have followed tutorial instructions. Sample in sample direcory works o.k, but when in my project directory it does not work ?

Name:

Type the text you see above:

Comments:


Knowledgebase

To be notified of new articles when they're available, subscribe to our RSS feed.

Support Resources

Take advantage of our knowledgebase of product tutorials and tips, and our support forums!

Search Site


Sign up for our Newsletter:

Get industry articles and Nitobi company news in your inbox every couple of months — here's a sample!

Email:




Site RSS Feed  | All contents are (c) Copyright 2006, Nitobi Software Inc. All rights Reserved