A short description :
01) I dynamically load data from a JSON url into an HTML table. The script is in the external .js file called in the header of the HTML file.
02) I filter the results using a filter at the top of the page for the first column (NAME). The script works if I include it in the HTML file under the <script> tag but it does not work when I put all functions in an external .js file.
The link is shown here : LINK
And the JS script is here :
//It loads the data from the JSON file
$.getJSON(
'http://apolosiskos.co.uk/TEB/MOCK_DATA.json',
function(data){
var tr;
//It loads data from the JSON file to the table
$.each (data, function (key, val) {
tr = $('<tr/>');
tr.append('<td class="name" rel="' + val.first_name + '">' + val.first_name + '</td>');
tr.append('<td >' + 'TEST' + '</td>');
tr.append('<td class="metric2" >' + val.id + '</td>');
tr.append('<td class="metric3"><span class="multTotal">' +'0.00'+ '</span></td>');
tr.append('<td class="metric3-100"><span class="metric3-100">' +'0.00'+ '</span></td>');
tr.append('<td class="metric1-100"><span class="metric1-100">' +'0.00'+ '</span></td>');
$('table').append(tr);
});
});
//The filter function for the first column (NAME)
$("input:checkbox").click(function filters() {
var showAll = true;
$('tr').not('.first').hide();
$('input[type=checkbox]').each(function () {
if ($(this)[0].checked) {
showAll = false;
var dimension1= $(this).attr('rel');
var value = $(this).val();
$('td.' + dimension1+ '[rel="' + value + '"]').parent('tr').show();
}
});
if(showAll){
$('tr').show();
}
});
$('body').on('input', 'input:checkbox', filters);
$("input:checkbox").click(function filters()I had to add a name in the function which is a click function. I tried that but did not work : stackoverflow.com/questions/5197207/…function filters()outside of theclickand remove the$("input:checkbox").click()altogether. The change the line below to$('body').on('input click', 'input:checkbox', filters);.