I wrote the function using js+jQuery.
Here is a link to a jsFiddle which shows my problem: http://jsfiddle.net/anarnold/LpBaW/
The goal of this function is to scan a table and check for rows that have certain field (td) values matching. Rows then get assigned a class denoting whether or not they are unique, and the number of matching rows are printed into the final field (td) of each row.
It basically uses a nested loop of this structure:
For each row... scan the whole table for matches..
The way I identify rows is to concatenate the field (td) texts from each row into a rowid attribute on the final field (td) for each row.
The current funciton works fine, but it gets very slow with large tables ~2000 rows.
There must be a more efficient and elegant way to accomplish this. Any help would be greatly appreciated!
Here is an example of using an associative array to store the results and then iterate on that:
var rowIdCnt = {};
function unqOrMsgTest() {
// Store counts here
var rowIdCnt = {};
// loop through check tds
$("#binning tr td[col=check]").each(function() {
// grab row identifer to check against other rows
var rowId = $(this).attr("rowid");
if (rowId in rowIdCnt) {
rowIdCnt[rowId] ++;
} else {
rowIdCnt[rowId] = 1;
}
});
// Now iterate over each count and update the table appropriately:
$.each(rowIdCnt, function(rowId, cnt) {
//this bit of logic picks a class to assign rows
var resultClass = "notUnique";
if (cnt < 2) {
resultClass = "unique";
}
//apply the row class and print the redundancy number into td
$('#binning tr td[rowid='+rowId+']').text(cnt).parent().addClass(resultClass);
});
}