Hi Norbert,
Sorry it took a while to get back to you. I was quite busy
Anyway, it is quite simple to do what you want.
First thing to do is to remove the checked attribute from the checkboxes:
<h2>Filter options</h2>
<div>
<input type="checkbox" id="Samsung">
<label for="Samsung">Samsung</label>
</div>
<div>
<input type="checkbox" id="iPhone">
<label for="iPhone">iPhone</label>
</div>
<div>
<input type="checkbox" id="HTC">
<label for="HTC">HTC</label>
</div>
<div>
<input type="checkbox" id="LG">
<label for="LG">LG</label>
</div>
<div>
<input type="checkbox" id="Nokia">
<label for="Nokia">Nokia</label>
</div>
The JS that is responsible for fetching the records when the page loads is this:
$checkboxes.trigger("change");
Replace this with the following:
var allBrands = [];
$("input[type=checkbox]").each(function(){
allBrands.push($(this)[0].id)
})
updatePhones();
This gets a list of all the available brands that the PHP script can filter by.
Now we need to update the updatePhones
function with this:
if(!opts || !opts.length){
opts = allBrands;
}
which checks to see if the function was passed either nothing or an empty array as a parameter.
If this is the case, it resorts to using all available brands.
We leave the PHP file untouched. Here’s the new HTML:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>AJAX filter demo</title>
<style>
body {
padding: 10px;
}
h1 {
margin: 0 0 0.5em 0;
color: #343434;
font-weight: normal;
font-family: 'Ultra', sans-serif;
font-size: 36px;
line-height: 42px;
text-transform: uppercase;
text-shadow: 0 2px white, 0 3px #777;
}
h2 {
margin: 1em 0 0.3em 0;
color: #343434;
font-weight: normal;
font-size: 30px;
line-height: 40px;
font-family: 'Orienta', sans-serif;
}
#phones {
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
background: #fff;
margin: 15px 25px 0 0;
border-collapse: collapse;
text-align: center;
float: left;
}
#phones th {
font-size: 14px;
font-weight: normal;
color: #039;
padding: 10px 8px;
border-bottom: 2px solid #6678b1;
}
#phones td {
border-bottom: 1px solid #ccc;
color: #669;
padding: 8px 10px;
}
#phones tbody tr:hover td {
color: #009;
}
#filter {
float:left;
}
fieldset{
margin-top: 15px;
}
fieldset div{
padding:0 0 5px 0;
}
.amount{
width:50px;
}
</style>
</head>
<body>
<h1>Phones database</h1>
<table id="phones">
<thead>
<tr>
<th width="15">ID</th>
<th>Brand</th>
<th>Model</th>
<th>Price</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div id="filter">
<h2>Filter options</h2>
<div>
<input type="checkbox" id="Samsung">
<label for="Samsung">Samsung</label>
</div>
<div>
<input type="checkbox" id="iPhone">
<label for="iPhone">iPhone</label>
</div>
<div>
<input type="checkbox" id="HTC">
<label for="HTC">HTC</label>
</div>
<div>
<input type="checkbox" id="LG">
<label for="LG">LG</label>
</div>
<div>
<input type="checkbox" id="Nokia">
<label for="Nokia">Nokia</label>
</div>
<fieldset>
<legend>Subsidy</legend>
<div>
<label for="amount1">Amount 1:</label>
<input type="text" class="amount" id="amount1" />
</div>
<div>
<label for="amount2">Amount 2:</label>
<input type="text" class="amount" id="amount2" />
</div>
<div>Total: $<span id="total">0</span></div>
<button id="apply">Apply</button>
<button id="remove" disabled>Remove</button>
</fieldset>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function makeTable(data){
var tbl_body = "";
$.each(data, function() {
var tbl_row = "",
currRecord = this;
$.each(this, function(k , v) {
if(k==='model'){
v = "<a href='content.php?id=" + currRecord['id'] +"'>" + v + "</a>";
} else if (k==='price'){
v = "<span class='price'>" + v + "</span>";
}
tbl_row += "<td>"+v+"</td>";
})
tbl_body += "<tr>"+tbl_row+"</tr>";
})
return tbl_body;
}
function getPhoneFilterOptions(){
var opts = [];
$checkboxes.each(function(){
if(this.checked){
opts.push(this.id);
}
});
return opts;
}
function updatePhones(opts){
if(!opts || !opts.length){
opts = allBrands;
}
$.ajax({
type: "POST",
url: "submit.php",
dataType : 'json',
cache: false,
data: {filterOpts: opts},
success: function(records){
$('#phones tbody').html(makeTable(records));
updatePrices();
}
});
}
function subsidyIsValid(){
var amount1 = $("#amount1").val(),
amount2 = $("#amount2").val(),
regex = /^\\d+$/,
inputValid = false;
if(regex.test(amount1) && regex.test(amount2)){
var newTotal = Number(amount1) + Number(amount2)
$("#total").text(newTotal);
inputValid = true;
}
return inputValid
}
function updatePrices(){
var subsidyTotal = Number($("#total").text());
$(".price").each(function(){
var origVal = Number($(this).text())
$(this).text(origVal - subsidyTotal)
})
}
var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function(){
var opts = getPhoneFilterOptions();
updatePhones(opts);
});
$("#apply").on("click", function(){
if(subsidyIsValid()){
$(this).prop("disabled", true);
$(this).next().prop("disabled", false);
updatePrices();
} else {
alert("Subsidy invalid!")
}
});
$("#remove").on("click", function(){
$("#amount1").val("");
$("#amount2").val("");
$("#total").text("0");
$(this).prop("disabled", true);
$(this).prev().prop("disabled", false);
$checkboxes.trigger("change");
});
var allBrands = [];
$("input[type=checkbox]").each(function(){
allBrands.push($(this)[0].id)
})
updatePhones();
updatePrices();
</script>
</body>
</html>
DEMO
Hope that helps.