...

View Full Version : urgent



kumar_mesa
12-09-2006, 10:50 PM
hi,
i have to sort the table in row wise,where i have images in first column,when i clicked first it shld sort in asending and if second time i clicked i have to sort desecding, it shld happen in row wise -please help-Urgent:mad: :mad:

david_kw
12-10-2006, 12:40 AM
In the future a title other than urgent would be more appropriate. In fact, I almost skipped trying it because the post felt pushy.

Anyway, I don't really like this answer but it sort of works. Turns out IE won't let you set innerHTML for rows in a table. So you have to recreate the entire table if you want to user innerHTML. Looks bad in FF too. But here it is anyway.

http://www.exfer.net/test/sort_table_test.html



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Junque</title>
<script type="text/javascript">
var bSortAscending = false;

function sortTest(a, b) {
var y = a.getElementsByTagName("td")[1].innerHTML;
var z = b.getElementsByTagName("td")[1].innerHTML;

if (y == z) { return(0); }
else if (y > z) { return((bSortAscending) ? 1 : -1); }
else if (y < z) { return((bSortAscending) ? -1 : 1); }
}

function doIt() {
var tdiv = document.getElementById("tablediv");
var t = document.getElementById("t");
var arrayLike = t.getElementsByTagName("tr");
var a = [];

for (var i = 0; i < arrayLike.length; i++) {
a[i] = arrayLike[i];
}

a.sort(sortTest);

var s = "<table id='t'>";
for (var i = 0; i < a.length; i++) {
s += "<tr>" + a[i].innerHTML + "</tr>";
}
s += "</table>";

tdiv.innerHTML = s;

bSortAscending = !bSortAscending;
}
</script>
</head>
<body>
<div id="tablediv">
<table id="t">

<tr><td><img src="images/mini_pizza.jpg" onclick="doIt();" /></td><td>Could it be?</td></tr>
<tr><td><img src="images/mini_pizza.jpg" onclick="doIt();" /></td><td>Anyone here?</td></tr>
<tr><td><img src="images/mini_pizza.jpg" onclick="doIt();" /></td><td>How do you know?</td></tr>
<tr><td><img src="images/mini_pizza.jpg" onclick="doIt();" /></td><td>What time is it?</td></tr>
</table>
</div>
</body>

</html>


david_kw



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum