Posted by: prajapatinilesh | September 14, 2007

Create Table Using Javascript and allow sort

<SCRIPT LANGUAGE=”JavaScript”><!–
function myObject() {
for (var i = 0; i<myObject.arguments.length; i++)
this[‘n’ + i] = myObject.arguments[i];
}

var objectArrayIndex = 0;
var myObjectArray = new Array();

function showObjectArray(object,length) {
var output = ‘<CENTER><TABLE BORDER=1>’;
output += ‘<TR>’;
for (var j=0; j<width; j++)
output += ‘<TH><A href=”tablejs1.php?n’ + j + ‘”>’ + eval(‘object[0].n’+j) + ‘<\/A><\/TH>’;
output += ‘<\/TR>’;
for (var i=1; i<length; i++) {
output += ‘<TR>’;
for (var j=0; j<width; j++)
output += ‘<TD>’ + eval(‘object[i].n’+j) + ‘<\/TD>’;
output += ‘<\/TR>’;
}
output += ‘<\/TABLE><\/CENTER>’;
document.write(output);
}

function myObjectBubbleSort(arrayName,length,property) {
for (var i=1; i<(length-1); i++)
for (var j=i+1; j<length; j++)
if (eval(‘arrayName[j].’ + property + ‘<arrayName[i].’ + property)) {
var dummy = arrayName[i];
arrayName[i] = arrayName[j];
arrayName[j] = dummy;
}
}

var width = 4;
myObjectArray[objectArrayIndex++] = new myObject(‘Number’,’Text’,’Date’,’Name’);
myObjectArray[objectArrayIndex++] = new myObject(100,’abc’,’19971104′,’Me’);
myObjectArray[objectArrayIndex++] = new myObject(400,’zzz’,’19961003′,’You’);
myObjectArray[objectArrayIndex++] = new myObject(300,’fox’,’19950902′,’Them’);
myObjectArray[objectArrayIndex++] = new myObject(50,’bad’,’19981201′,’Us’);

var sortProperty = window.location.search.substring(1);

if (sortProperty.length != 0)
myObjectBubbleSort(myObjectArray,objectArrayIndex,sortProperty);

showObjectArray(myObjectArray,objectArrayIndex);
//–>
</SCRIPT>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: