Data Template using Mootools

by brunofig 9. March 2007 04:29

I needed to bind some data to a table. So I built a little script that does it. I simple create the rows template:

<table id="template_container" style="display:none;">	<tr><td id="row1"></td><td><span id="row2"></span></td><td id="row3"></td></tr></table>

The id inside the template identifies the column that I want to bind.

I also need to create the output container:

<table id="template_output" border="1"></table>

So my datasource looks like this:

var data = {result:[{row1:"Bruno", row2:"Ricardo", row3:"Figueiredo"}, {row1:"Sandra", row2:"Isabel", row3:"Lourenço"}]};

Each property name on the object is the id of the column.

Then, all i need to do is bind: 

var t = new Template($("template_container"));t.set_data(data.result);t.bind($("template_output"));

or inline:

new Template($("template_container"), data.result, $("template_output")).bind();

So there you have it.

 

Source code:

 

var Template = new Class({	initialize: function()	{		$each(arguments, function(argument, idx){			if (idx == 0) this._template = $(argument).getFirst();			if (idx == 1) this._data = argument;			if (idx == 2) this._container = $(argument);		}, this);	},		set_data: function(data)	{		this._data = data;	},		bind: function(htmlElement)	{		this._parseData(this._data, htmlElement || this._container);	},		_parseData: function(data, container)	{		if(data.forEach)		{			data.forEach(function(row){				var line = this._template.clone();				for (var column in row)				{					this._setDataContainer(line, column, row[column]);				}				line.removeAttribute("id");				line.style.display = "";				container.adopt(line);			}, this);		}	},		_setDataContainer:function(line, dataID, data)	{		$each(line.childNodes, function(element){				if (element.id && element.id == dataID)				{					element.appendText(data);					element.removeAttribute("id");					}				else if (element.childNodes)				{					this._setDataContainer(element, dataID, data);				}			}, this);	},		_isArray: function(object)	{		return (object.constructor.toString().indexOf("Array")> 0)	}});

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.7
Theme by Mads Kristensen

About the author

Hi!

My name is Bruno and I'am a senior consultant. When I'm not working, you can catch me playing with my daugther, taking photos and hanging out with my wife and friends. :)

You can also check my dark side or have fun with my vision of the world.

View Bruno Figueiredo's profile on LinkedIn

TwitterCounter for @brunoshine
 
Creative Commons License

 
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
 

Ads

    Page List

      Most comments

      Busby SEO Test Busby SEO Test
      2 comments
      kh Cambodia
      laptop cases laptop cases
      2 comments
      Busby SEO Test Busby SEO Test
      1 comments

      RecentComments

      Comment RSS

      Popuri.us

      My Popularity (by popuri.us)