I was wondering if I could get a review of this bit of code I wrote up today. Basically I’m grabbing some data from a mysql table through php storing it in a json object and then sorting by rows and columns and displaying as necessary. Everything is working fine for me as I can tell but I’d like some input as I’m not that great at javascript and am just looking for some feed back. The first section of code is the script. Please note that I am just pasting the returned json object at the bottom of the post. Just in case you would like to give it a whirl.
<?
$test = file_get_contents('data.json');
?>
<!doctype html>
<html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$(function() {
var allCabinets = <?= $test ?>;
var current_height = 0;
var current_width = 0;
var uniqueHeights = getDistinct(allCabinets);
var defaultHeight = buildCabinets(allCabinets,uniqueHeights[current_height]);
var uniqueWidths = getDistinctColumns(defaultHeight);
var display = buildCabinets(allCabinets,uniqueHeights[current_height],uniqueWidths[current_width]);
createTable(display);
function increaseHeight(){
var maxHeight = uniqueHeights.length;
if(current_height < maxHeight-1){
current_height=current_height+1;
m = buildCabinets(allCabinets,uniqueHeights[current_height]);
uniqueWidths = getDistinctColumns(m);
current_width = 0;
display = buildCabinets(allCabinets,uniqueHeights[current_height], uniqueWidths[current_width]);
console.log(display);
createTable(display);
}else{
display = buildCabinets(allCabinets,uniqueHeights[current_height], uniqueWidths[current_width]);
console.log(display);
createTable(display);
}
}
function decreaseHeight(){
if(current_height > 0){
current_height=current_height-1;
m = buildCabinets(allCabinets,uniqueHeights[current_height]);
uniqueWidths = getDistinctColumns(m);
current_width = 0;
display = buildCabinets(allCabinets,uniqueHeights[current_height], uniqueWidths[current_width]);
console.log(display);
createTable(display);
}else{
display = buildCabinets(allCabinets,uniqueHeights[current_height], uniqueWidths[current_width]);
console.log(display);
createTable(display);
}
}
function increaseWidth(){
var maxWidth = uniqueWidths.length;
if(current_width < maxWidth-1){
current_width=current_width+1;
m = buildCabinets(allCabinets,uniqueHeights[current_height]);
uniqueWidths = getDistinctColumns(m);
display = buildCabinets(allCabinets,uniqueHeights[current_height], uniqueWidths[current_width]);
console.log(display);
createTable(display);
}else{
display = buildCabinets(allCabinets,uniqueHeights[current_height], uniqueWidths[current_width]);
console.log(display);
createTable(display);
}
}
function decreaseWidth(){
if(current_width > 0){
current_width=current_width-1;
m = buildCabinets(allCabinets,uniqueHeights[current_height]);
uniqueWidths = getDistinctColumns(m);
display = buildCabinets(allCabinets,uniqueHeights[current_height], uniqueWidths[current_width]);
console.log(display);
createTable(display);
}else{
display = buildCabinets(allCabinets,uniqueHeights[current_height], uniqueWidths[current_width]);
console.log(display);
createTable(display);
}
}
function buildCabinets(c, x, y) {
var res = c.filter(function(el) {
return typeof y !== 'undefined' ? el.signRows == x && el.signColumns == y : el.signRows == x;
});
return res;
}
function getDistinctColumns(data){
var arr = new Array();
$.each(data, function(i, sign){
if(jQuery.inArray(sign.signColumns,arr)===-1){
arr.push(sign.signColumns);
}
});
return arr.sort(function(a,b){return a-b});
}
function getDistinct(data) {
var arr = new Array();
$.each(data, function(i, sign) {
if (jQuery.inArray(sign.signRows, arr) === -1) {
arr.push(sign.signRows);
}
});
return arr.sort(function(a,b){return a-b});
}
function createTable(result) {
var data = result;
$("#placeholder").html("");
var tableHeader = "<table><thead><tr>";
var tableBody = "<tbody>";
var endTable = "</table>";
tableHeader += "<th>ID</th><th>Pitch</th><th>Matrix</th></tr></thead>";
$(tableHeader).appendTo("#placeholder");
$(data).each(function(index,item){
tableBody += "<tr>";
tableBody +="<td>" + item['id'] + "</td>";
tableBody +="<td>" + item['signPitch'] + "</td>";
tableBody +="<td>" + item['signRows'] + " x " + item['signColumns'] + "</td>";
tableBody += "</tr>";
});
tableBody += "</tbody>";
$(tableBody).appendTo("#placeholder");
$(endTable).appendTo("#placeholder");
}
$('#increaseHeight').click(function(e){
increaseHeight();
$("#pixelsTall").val(display[0]['signRows']);
$("#pixelsWide").val(display[0]['signColumns']);
e.preventDefault();
});
$('#decreaseHeight').click(function(e){
decreaseHeight();
$("#pixelsTall").val(display[0]['signRows']);
$("#pixelsWide").val(display[0]['signColumns']);
e.preventDefault();
});
$('#increaseWidth').click(function(e){
increaseWidth();
$("#pixelsWide").val(display[0]['signColumns']);
e.preventDefault();
});
$('#decreaseWidth').click(function(e){
decreaseWidth();
$("#pixelsWide").val(display[0]['signColumns']);
e.preventDefault();
});
});
</script>
<fieldset class="one-third">
<legend>Find by Matrix</legend>
<input type="text" name="pixelsTall" id="pixelsTall" value=""/>
<button name="increase-matrix-pixel-tall" id="increaseHeight">+</button>
<button name="decrease-matrix-pixel-tall" id="decreaseHeight">-</button>
<label>Pixels Tall</label>
<div class="clear"></div>
<input type="text" name="pixelsWide" id="pixelsWide"/>
<button name="increase-matrix-pixel-wide" id="increaseWidth">+</button>
<button name="decrease-matrix-pixel-wide" id="decreaseWidth">-</button>
<label>Pixels Wide</label>
</fieldset>
<div id="placeholder"></div>
</html>
[{"id":"1","signRows":"32","signColumns":"96","signModel":"16","signPitch":"16"},{"id":"2","signRows":"32","signColumns":"112","signModel":"16","signPitch":"16"},{"id":"3","signRows":"32","signColumns":"128","signModel":"16","signPitch":"16"},{"id":"4","signRows":"32","signColumns":"144","signModel":"16","signPitch":"16"},{"id":"5","signRows":"32","signColumns":"160","signModel":"16","signPitch":"16"},{"id":"6","signRows":"48","signColumns":"96","signModel":"16","signPitch":"16"},{"id":"7","signRows":"48","signColumns":"112","signModel":"16","signPitch":"16"},{"id":"8","signRows":"48","signColumns":"128","signModel":"16","signPitch":"16"},{"id":"9","signRows":"48","signColumns":"144","signModel":"16","signPitch":"16"},{"id":"10","signRows":"48","signColumns":"160","signModel":"16","signPitch":"16"},{"id":"11","signRows":"64","signColumns":"96","signModel":"16","signPitch":"16"},{"id":"12","signRows":"64","signColumns":"112","signModel":"16","signPitch":"16"},{"id":"13","signRows":"64","signColumns":"128","signModel":"16","signPitch":"16"},{"id":"14","signRows":"64","signColumns":"144","signModel":"16","signPitch":"16"},{"id":"15","signRows":"64","signColumns":"160","signModel":"16","signPitch":"16"},{"id":"16","signRows":"80","signColumns":"224","signModel":"16","signPitch":"16"},{"id":"17","signRows":"16","signColumns":"80","signModel":"20","signPitch":"20"},{"id":"18","signRows":"16","signColumns":"96","signModel":"20","signPitch":"20"},{"id":"19","signRows":"16","signColumns":"112","signModel":"20","signPitch":"20"},{"id":"20","signRows":"16","signColumns":"128","signModel":"20","signPitch":"20"},{"id":"21","signRows":"16","signColumns":"144","signModel":"20","signPitch":"20"},{"id":"22","signRows":"16","signColumns":"160","signModel":"20","signPitch":"20"},{"id":"23","signRows":"24","signColumns":"80","signModel":"20","signPitch":"20"},{"id":"24","signRows":"24","signColumns":"96","signModel":"20","signPitch":"20"},{"id":"25","signRows":"24","signColumns":"112","signModel":"20","signPitch":"20"},{"id":"26","signRows":"24","signColumns":"128","signModel":"20","signPitch":"20"},{"id":"27","signRows":"24","signColumns":"144","signModel":"20","signPitch":"20"},{"id":"28","signRows":"24","signColumns":"160","signModel":"20","signPitch":"20"},{"id":"29","signRows":"32","signColumns":"80","signModel":"20","signPitch":"20"},{"id":"30","signRows":"32","signColumns":"96","signModel":"20","signPitch":"20"},{"id":"31","signRows":"32","signColumns":"112","signModel":"20","signPitch":"20"},{"id":"32","signRows":"32","signColumns":"128","signModel":"20","signPitch":"20"},{"id":"33","signRows":"32","signColumns":"144","signModel":"20","signPitch":"20"},{"id":"34","signRows":"32","signColumns":"160","signModel":"20","signPitch":"20"},{"id":"35","signRows":"48","signColumns":"96","signModel":"20","signPitch":"20"},{"id":"36","signRows":"48","signColumns":"112","signModel":"20","signPitch":"20"},{"id":"37","signRows":"48","signColumns":"128","signModel":"20","signPitch":"20"},{"id":"38","signRows":"48","signColumns":"144","signModel":"20","signPitch":"20"},{"id":"39","signRows":"48","signColumns":"160","signModel":"20","signPitch":"20"},{"id":"40","signRows":"64","signColumns":"96","signModel":"20","signPitch":"20"},{"id":"41","signRows":"64","signColumns":"112","signModel":"20","signPitch":"20"},{"id":"42","signRows":"64","signColumns":"128","signModel":"20","signPitch":"20"},{"id":"43","signRows":"64","signColumns":"144","signModel":"20","signPitch":"20"},{"id":"44","signRows":"64","signColumns":"160","signModel":"20","signPitch":"20"},{"id":"45","signRows":"16","signColumns":"80","signModel":"25","signPitch":"25"},{"id":"46","signRows":"16","signColumns":"96","signModel":"25","signPitch":"25"},{"id":"47","signRows":"16","signColumns":"112","signModel":"25","signPitch":"25"},{"id":"48","signRows":"16","signColumns":"128","signModel":"25","signPitch":"25"},{"id":"49","signRows":"16","signColumns":"144","signModel":"25","signPitch":"25"},{"id":"50","signRows":"16","signColumns":"160","signModel":"25","signPitch":"25"},{"id":"51","signRows":"32","signColumns":"96","signModel":"25","signPitch":"25"},{"id":"52","signRows":"32","signColumns":"112","signModel":"25","signPitch":"25"},{"id":"53","signRows":"32","signColumns":"128","signModel":"25","signPitch":"25"},{"id":"54","signRows":"32","signColumns":"144","signModel":"25","signPitch":"25"},{"id":"55","signRows":"32","signColumns":"160","signModel":"25","signPitch":"25"},{"id":"56","signRows":"48","signColumns":"96","signModel":"25","signPitch":"25"},{"id":"57","signRows":"48","signColumns":"112","signModel":"25","signPitch":"25"},{"id":"58","signRows":"48","signColumns":"128","signModel":"25","signPitch":"25"},{"id":"59","signRows":"48","signColumns":"144","signModel":"25","signPitch":"25"},{"id":"60","signRows":"48","signColumns":"160","signModel":"25","signPitch":"25"},{"id":"61","signRows":"64","signColumns":"128","signModel":"25","signPitch":"25"},{"id":"62","signRows":"16","signColumns":"64","signModel":"34","signPitch":"34"},{"id":"63","signRows":"16","signColumns":"80","signModel":"34","signPitch":"34"},{"id":"64","signRows":"16","signColumns":"96","signModel":"34","signPitch":"34"},{"id":"66","signRows":"24","signColumns":"64","signModel":"34","signPitch":"34"},{"id":"67","signRows":"24","signColumns":"80","signModel":"34","signPitch":"34"},{"id":"68","signRows":"24","signColumns":"96","signModel":"34","signPitch":"34"},{"id":"69","signRows":"32","signColumns":"64","signModel":"34","signPitch":"34"},{"id":"70","signRows":"32","signColumns":"80","signModel":"34","signPitch":"34"},{"id":"71","signRows":"32","signColumns":"96","signModel":"34","signPitch":"34"},{"id":"72","signRows":"48","signColumns":"64","signModel":"34","signPitch":"34"},{"id":"73","signRows":"48","signColumns":"80","signModel":"34","signPitch":"34"},{"id":"74","signRows":"48","signColumns":"96","signModel":"34","signPitch":"34"}]