July 21, 2010

Some useful array functions for JavaScript

JavaScript is a great language. There was a time when I was rather against it (mainly during the browser wars) but with modern libraries and the like, cross browser processing is exactly where it should be. However, there are a lot of times when you just need some simple processing done and most libraries – even their cores – are too bloated to be of any use. Below I’ve outlined three functions has(), indexOf() and remove() that extend the existing Array prototype to add some much needed features.

Array.prototype.indexOf = function(val) {
	for(i = 0; i < this.length; i++) {
		if(this[i] == val) {
			return i; 
		}
	}
	return -1;
};
 
Array.prototype.has = function(val) {
	return this.indexOf(val) > 0 ? true:false;
};
 
Array.prototype.remove = function(val) {
	var x = this.indexOf(val), y = 0;
	for(i = 0; i < this.length; i++) {
		if(i != x) {
			this[y]= this[i];
			y++;
		}
	}
	this.pop();
};

indexOf()
Often times you have a string and you need to figure out where in the array it appears. Since JavaScript doesn’t technically support associative arrays (array object conversion does occur implicitly) you can either loop through the array every time, or just extend array!

Example for indexOf()
var myArray = ["an","array","with","a","bunch","of","words"]; 
document.Write(myArray.indexOf("a"));   // returns 3
document.Write(myArray.indexOf("491"));  // returns -1

has()
Sometimes you just need to know if a value is present within an array. has() takes care of that returning a simple true:false boolean. Rather than re-inventing the wheel, it works in conjunction with indexOf()

Example for has()
var myArray = ["an","array","with","a","bunch","of","words"]; 
document.Write(myArray.has("with")); // returns true
document.Write(myArray.has("feet")); // returns false

remove()
Finally, sometimes removing a single element from an array is necessary.

Example for remove()
var myArray = ["an","array","with","a","bunch","of","words"]; 
myArray.remove("array"); // myArray now contains ["an","with","a","bunch","of","words"]

Hopefully the functions are of help! If there are a lot of people confused over Prototypal Inheritance, maybe that would be an interesting topic.

July 15, 2010

DataGrid Control V2

I’ve been working on a series of “Control Objects” to include in my usual projects, and I thought I’d post one of the completed ones up here. Basically it mimcs the use of the DataGrid control in Visual Studio but in a way I find a lot easier and more in-tune with PHP. After creating the DataGrid, you define a “source”. The Source is essentially a multi-dimensional array that you could assign with a simple

while($r[] = mysql_fetch_array($resource)){}

you would then pass $r as the source to the DataGrid. You have the ability to set some options (like specifying specific style sheets, JS libraries for AJAX editing or defining and renaming visible columns) and then a single build(); command will output the table. Of course, you can also build(true); to return the table incase you’re using some sort of theme system.

$data = array(
  array('id'=>0,'f_name'=>'Angelo','l_name'=>'Rodrigues'), 
  array('id'=>1,'f_name'=>'Darth','l_name'=>'Vader'),
  array('id'=>2,'f_name'=>'Luke','l_name'=>'Skywalker'),
  array('id'=>3,'f_name'=>'Han','l_name'=>'Solo'),
);
 
$DataGrid = new DataGrid(); 
$DataGrid->configure(
  array(
    'primary_key' => 'id', 
    'sortable' => true, 
    'striped' => true,
    'even_row' => null, 
    'odd_row' => 'striped'
    )
);
 
// This not only defines display fields, but the order as well 
$fields = array( 
  'f_name' => 'First Name',
  'l_name' => 'Last Name',
);
$DataGrid->setDisplayFields($fields);
$headers = $DataGrid->Headers();
echo $headers['css']; 
echo $headers['js']['object'];
echo $headers['js']['function'];
 
 
$DataGrid->source($data); 
$DataGrid->build();

The code can be further simplified by using a new class called ControlObjectManager (or COM for short). COM can be used to instantiate multiple Controls at once and even assign shortcuts to commonly used Controls. For example, if you need DataGrids a lot, you can initiate them via a simple call like $COM->init(array('dg')) and as long as you set up that dg means DataGrid in the configuration for COM, you’re good to go.

I’ll post up some more on the COM when it’s done, but for now, here’s a link to the latest iteration of DataGrid v2.

View on Pastebin – Just note that this is the debug version of the script, so there are tons of comments and explanations.

The original DataGrid actually tied in to MySql directly and required the MySqlAdapter control to even function. This latest iteration removes all ties to any database instead focusing on a simple multi-dimensional array of data. As well it drops the code-base down quite a bit and condenses a lot of functionality. For example: originally you would have had to set the display columns and then order them, but by compressing them into a single function it not only makes MORE sense, but is easier to read.

July 14, 2010

A quick fix

Fixing computer problems is no longer the quick task it used to be. Now, when you’re dealing with custom built pc’s averaging about 6 separate major parts, with each part comprised of its own little pieces, it starts to border impossible – or at least impossible in a timely manner. And that’s just hardware too. Counting all the possible software issues and incompatibilities, fixing a problem can border on days.

And days is simply too long. Often when a customer comes to me with computer problems it goes a little like this:

  1. Sit down and get the customer to explain what happened
  2. Keep pressing for more details. More often than not the customers description is very high level. “I clicked ok and it just showed up” isn’t very accurate. I need to know every detail leading up to the error. Most times that alone can identify a potential set of problems, or at least rule some things out.
  3. Research. Contrary to popular belief, IT professionals don’t know every problem that could occur. We know the common ones and we know ones that we’ve experienced before, and we know some of the stranger ones. 1 We need to look into other cases of your problem, we need to find patterns between occurrences and then apply them back to your case. We are not just Googling hoping to stumble across an answer. Although, we do use Google.
  4. Come up with a solution

Sadly, a lot of times the solution we come up with is simple: Reformat.

Yes I could spend 8 or 9 hours for the next 3 days removing all traces of the virus from your computer. Yes I could find the exact file that is corrupted on both your CD and your computer. Yes I could bake a pizza from scratch. But it’s not worth it. Not when there’s a solution that is fairly easy to follow and can get you back and computing faster than ever. Not to mention reformatting not only saves you time, it also saves you money, because I don’t charge you for solutions, I just charge for implementation.

That being said, every so often one of those really strange problems pop up and you can’t help but spend countless hours fixing it – but hey, we’re geeks.

Notes:

  1. I have been known to spend a lot of time on http://www.thedailywtf.com reading some of the stranger issues people have had