Chris Nizzardini, Salt Lake City Utah, Web Developer Specializing in LAMP+Ajax Since 2006

My Blog

Here is my awesome blog. You can find information on programming, linux, documentation, tips for code and database optimization, my thoughts and rants, and whatever else I feel like sharing. Feel free to contribute to the blog by posting comments and asking questions.

Posts Tagged ‘mootools’

A Sneak Peak at Moo2Complete, MooTools Plugin For Auto Completion

Posted by chris on February 19th, 2011 Comments(0)

It’s been a while since I’ve written a community plugin for MooTools suitable for MooForge. My last one was FbModal about a year ago and before that was the MooTools TableSorter.  Well I started a new project earlier this month and was looking around for a suitable MooTools based auto completer.  I was thoroughly unimpressed with the options out in the wild.  Enter Moo2Complete!

Read the rest of this entry »

In JavaScript and Ajax ()

MooTools Table Sorter 0.9.6 released

Posted by chris on November 24th, 2010 Comments(0)

MooTools Table Sorter version 0.9.6 released on November 15, 2010. This post is just for reporting bugs and feature requests for the new version. For documentation please refer to the version 0.9.5 post. This documentation is still valid. 0.9.6 just fixes bugs in Internet Explorer 6 and Internet Explorer 7 as well as validating the code works in IE9.

In JavaScript and Ajax, Programming (, )

MooTools Form Snippet – Easy Ajax Form Submissions

Posted by chris on November 18th, 2010 Comments(0)

I wrote this function a long time ago when I had just begun using MooTools. They likely have native functionality for this sort of thing, but I’m posting it anyways. Maybe I’ll build a class out of this one day and submit it to mooforge.

This function just gets all the data in a form and returns it in a string fit for AJAX communication over either HTTP POST and HTTP GET. Just pass the form element as the parameter. Works great for MooTools Ajax form submissions. You don’t even have to modify the Ajaax post/get object when adding new input fields, just modify your server-side code.

Read the rest of this entry »

In JavaScript and Ajax (, , , , , )

FbModal 0.9.x – A MooTools Window Modal Overlay

Posted by chris on June 2nd, 2010 Comments (16)

This MooTools javascript window modal overlay window is designed to look similar to the modal used by a prominent social networking site. I had been using the awesome MochaUI for modals, but I decided to develop my own lightweight solution after reading this blog post by David Walsh. I based the CSS and functionality off of his original code, but I enhanced the javascript by making it object oriented and leveraging mootools to create the DOM elements on the fly. I also added a few other helpful methods. It uses mootools 1.2.4 and has not been tested with other versions of mootools. It works well enough in most browsers (tested in FireFox, Chrome, and IE 6/7/8). Click here to view the demo.

*Updated for version 0.9.3
Issues within Internet Explorer 6 and 7 have now been resolved thanks to the work done by K.J. Ye.

Read the rest of this entry »

In JavaScript and Ajax (, )

MooTools Table Sorter 0.9.5

Posted by chris on May 31st, 2010 Comments (16)

Well its been a long weekend and I’m starting to wrap it up. But I couldn’t go to bed without redoing the documentation for my baby the MooTools TableSorter and updating the demo page. The documentation will loosely follow what you see in the demo page.

MooTools Table Sorter

HTML Setup

You’ll need to include 3 files: MooTools 1.2 or higher, the CSS file, and the Table Sorter javascript class. You’ll also want to make sure all the images are there and update the CSS image paths to jive with the location on your web server.

1
2
3
<link rel="stylesheet" media="screen" href="/mootools/table-sorter/table-sorter.css" />
<script type="text/javascript" src="/js/mootools1-2.js"></script>
<script type="text/javascript" src="/mootools/table-sorter/table-sorter.js"></script>

PHP Setup

You’ll want to use the TableSorter php class I included to make things easy one you.

Notice setting the table headers is important. For instance when a user clicks on city the TableSorter will send the ORDER BY stored in the TH Title. This is how your server side code will know what to order by. Reply to this post if you have any questions on the server-side PHP code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
include_once 'TableSorter.class.php';
function returnGeoHtmlTableStr($whereClause='',$startingFromRecord=0,$rowsPerPage=100,$orderBy='city ASC')
{
	mysql_connect('localhost','user','password','database');
	mysql_select_db('database');
 
	$startingFromRecord = (int) $startingFromRecord;
	$rowsPerPage = (int) $rowsPerPage;
 
	$sql = "SELECT 
				count(*) as count 
			FROM 
				cnizz_geo 
 
			$whereClause
 
			";
	$result = mysql_query($sql);
	$countArr = mysql_fetch_assoc($result);
	$totalRows = $countArr['count'];
 
	if($orderBy==''){
		$orderBy='city ASC';
	}
 
	$sql = "SELECT SQL_CALC_FOUND_ROWS
				* 
			FROM 
				cnizz_geo 
 
			$whereClause
 
			ORDER BY 
				$orderBy
			LIMIT 
				$startingFromRecord,$rowsPerPage
			";
	$result = mysql_query($sql);
	$arr = array();
	while($row=mysql_fetch_assoc($result)){
		$arr[]=$row;
	}
 
	$sql = "SELECT FOUND_ROWS() as totalRows";
	$result = mysql_query($sql);
	$foundArr=mysql_fetch_assoc($result);
 
	$str = '
		<table class="DefaultTable" style="width:500px;">
		'.TableSorter::returnMetaStr($startingFromRecord,$rowsPerPage,$foundArr['totalRows'],count($arr),$orderBy).'
		<tr style="background:#FFF;"><td colspan="10" style="height:5px;">&nbsp;</td></tr>
		<tr id="GeoHead" class="DefaultTableHeader">
			<th id="City" title="'.(($orderBy=='city ASC')?'city DESC':'city ASC').'">City</th>
			<th id="State" title="'.(($orderBy=='state ASC')?'state DESC':'state ASC').'">State</th>
			<th id="Abbrev" title="'.(($orderBy=='state_abbrev ASC')?'state_abbrev DESC':'state_abbrev ASC').'">State</th>
			<th id="County" title="'.(($orderBy=='county ASC')?'county DESC':'county ASC').'">County</th>
		</tr>
		';
	$x=1;
 
	foreach($arr as $i)
	{
		if($x%2){
			$class='';
		}
		else{
			$class='zebra';
		}
		$str.= '<tr class="'.$class.'">
							<td><a id="'.$i['city'].'" title="'.stripslashes($i['city_id']).'">'.stripslashes($i['city']).'</a></td>
							<td>'.$i['state'].'</td>
							<td>'.$i['state_abbrev'].'</td>
							<td>'.$i['county'].'</td>
						</tr>';
		$x++;
	}
 
	$str.= '<tr style="background:#FFF;"><td colspan="10" style="height:5px;">&nbsp;</td></tr>';
	$str.=$meta.'</table>';
	return $str;
}

Your Server Side Controller

1
2
3
if($_GET['action']=='returnGeoHtmlTableStr'){
	die(returnGeoHtmlTableStr('',$_GET['start'],$_GET['rows'],$_GET['orderBy']));
}

Now the JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
window.addEvent('domready',function(){
	sorter = new TableSorter({
		request: 'action', 
		action: 'returnGeoHtmlTableStr', 
		destination: 'XhrDump', 
		prev: 'PagePrev', 
		next: 'PageNext', 
		head: 'GeoHead',
		rows: 100,
		defaultStartEndWaitEnabled: 1,
		startWait: '',
		endWait: ''
	});
})

Lets explain this line by line.

  1. request: ‘action’, This is how you name the GET parameter. So in this instance the GET looks like $_GET['action']
  2. action: ‘returnGeoHtmlTableStr’, This is the value of GET parameter defined in step 1, so $_GET['action'] = ‘returnGeoHtmlTableStr’
  3. destination: ‘XhrDump’, This is the element id where TableSorter will write results too
  4. prev: ‘PagePrev’, This is the element that the class will listen on for Previous Page requests.
  5. next: ‘PageNext’, Same as above except it goes to the next page when this element is clicked
  6. head: ‘GeoHead’, This tells the class where to look for all those TH tags, which it uses to do sorts on if a Title is defined on the TH tag.
  7. rows: 100, How many rows should the request return.
  8. page: location.href, Where to send the GET request
  9. method: ‘get’, The type of method (GET or POST). By default the method is get, but you can change it to post.
  10. defaultStartEndWaitEnabled: ’1′, this is set to 1 by default. Setting this to 0 disables the built-in visuals (fades, image etc.) you see when performing a sort.
  11. startWait: ”, empty by default, will do a callback function only if defaultStartEndWaitEnabled is set to 0. This is for doing your own custom visuals.
  12. endWait: ”, empty by default, will do a callback function only if defaultStartEndWaitEnabled is set to 0.

Other cool stuff you can do

You can add additional parameters to the GET request in the form of parameter name => parameter value.

1
sorter.addParameter('zipcode','84101');

You can remove these user-defined parameters by calling removeAllParameters.

1
sorter.removeAllParameters();

You can remove a specific parameter by calling removeParameter.

1
sorter.removeParameter('zipcode');

You can initiate a sort anytime you want by calling sort.

1
sorter.sort();

There are some known issues in IE where the up/down arrows will not appear on sorts. Also the visual-effects are a bit degraded in IE as well. When posting questions or bugs you can use the HTML pre tag and set language=”javascript” to get syntax highlighting (wp-syntax plugin). Just makes things easier for me and other readers. Take a look at the source on my syntax highlighting and you’ll see what I mean.

Please reply to this blog post with any comments, bugs, or questions. Enjoy.

In JavaScript and Ajax, Programming (, )

MooTools Framework Javascript Key Listener

Posted by chris on December 12th, 2008 Comment(1)

Expanding off this article on JavaScript key listeners I’ll be showing you how to create a key listener in mootools 1.2 today. You will need to use mootools event functionality for this.

1
2
3
4
5
6
7
8
9
10
11
window.addEvent('domready', function() {
 
	MochaUI.Modal = new MochaUI.Modal(); 
 
    $('last_name').addEvent('keydown',function(event){
    	if(event.key=='enter'){
			searchCustomer();
    	}
    });
 
});

In this example we add an event to the $(‘last_name’) element and tell mootools we want the framework too look for all keydown events on this element. If a keydown event occurs using the enter key we execute the searchCustomer function. Be sure to iniate this on dom ready or whenever the element you are adding the event to is loaded. On DOM ready simply means when the page is completed loading much like body onload. Key press events are a great way to minimize the amount of clicks in your user interface. Many studies show that using the keyboard is up to three times faster than using a mouse!

In JavaScript and Ajax (, , , , , , )

How to Create a JavaScript Key Listener

Posted by chris on October 27th, 2008 Comments(0)

Need help with JavaScript key listeners? Using a key listener in JavaScript is great way to make your web application react like a desktop application. If you’re not using a javascript framework such as MooTools or JQuery (I’ll go over these after covering generic javascript) then the first thing you need to do is place this code in your body onload event. This will call the returnKey function every time the browser detects a key press.

1
document.onkeypress = returnKey;

We will listen specifically for key code 13, the return key, where a text node has focus. Next we create a switch statement that looks at the text nodes id. We will put in a case to look for the text node id of search. This will call a getProduct function and also highlight the search field.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function returnKey(evt)
{
	var evt  = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
 
	if ((evt.keyCode == 13) && (node.type == "text")) 
	{
		switch(node.id)
		{
			case 'search':
				getProduct();
				$('search').select();
				break;
		}
	}
}

To do the same thing using the JQuery framework is a bit more simple. This example JavaScript code will create keyup event:

$(document).ready(function(){
    $('#IDofTextInput').bind('keyup',function(e){
        // your function/code to execute goes here
    })
});

Here is some example code for creating a keypress event using the MooTools Framework for JavaScript:

window.addEvent('domready',function(){
	$('IDofTextInput').addEvent('keypress',function(event){
		// your function/code to execute
	})
})

Drop me a comment if this helped your or if you feel I missed something, thanks for reading.

In JavaScript and Ajax (, , , , , , )