WME/aufgabe3/public/assets/js/ajax.js

174 lines
4.9 KiB
JavaScript
Raw Normal View History

2018-12-21 12:41:15 +00:00
$("#filter_countries").click(function () {
2018-12-26 09:16:22 +00:00
var url = "/items";
var country_filter_range = $("#country_filter_range").val();
var country_filter_id = $("#country_filter_id").val();
if (country_filter_range.length != 0) {
var id_range = country_filter_range.split("-");
// tests for wrong input
// wrong arguments
if (id_range.length != 2) {
return false;
}
// first argument is not a number
if (isNaN(id_range[0])) {
return false;
}
// second argument is not a number
if (isNaN(id_range[1])) {
return false;
}
var first_id = parseInt(id_range[0]);
var second_id = parseInt(id_range[1]);
// bounds checks
if (first_id < 1) {
return false;
}
if (first_id > second_id) {
return false;
}
// assemble url
url += "/" + first_id.toString() + "/" + second_id.toString();
} else if (country_filter_id.length != 0) {
// assemble url
url += "/" + country_filter_id;
}
$.ajax({
type: "GET",
url: url,
async: true,
success: function (data) {
if (data[0] == "Err") {
return;
}
// clear table
$("#table_body").empty();
// refill table
$(function () {
$.each(data, function (i, item) {
$('<tr>').append(
$('<td>').addClass("colID").text(item.id),
$('<td>').addClass("colName").text(item.name),
$('<td>').addClass("colBirth").text(item.birth_rate_per_1000),
$('<td>').addClass("colCell").text(item.cell_phones_per_100),
$('<td>').addClass("colChildren").text(item.children_per_woman),
$('<td>').addClass("colElectricity").text(item.electricity_consumption_per_capita),
$('<td>').addClass("colGDP").text(item.gdp_per_capita),
$('<td>').addClass("colGDPGrowth").text(item.gdp_per_capita_growth),
$('<td>').addClass("colInflation").text(item.inflation_annual),
$('<td>').addClass("colInternet").text(item.internet_user_per_100),
$('<td>').addClass("colLife").text(item.life_expectancy),
$('<td>').addClass("colMilitary").text(item.military_expenditure_percent_of_gdp),
$('<td>').addClass("colGPSLat").text(item.gps_lat),
$('<td>').addClass("colGPSLong").text(item.gps_long)
).appendTo('#table_body');
});
});
// show table
$(`td,th`).show();
},
error: function (jqXHR, text, err) {
alert(err);
}
})
2018-12-21 12:41:15 +00:00
return false;
});
$("#show_selected_prop").click(function () {
2018-12-26 09:16:22 +00:00
var property = $("#prop_selection").val();
var column = val_to_column(property);
2018-12-21 12:41:15 +00:00
2018-12-26 09:16:22 +00:00
$(`td:nth-child(${column}),th:nth-child(${column})`).show();
2018-12-21 12:41:15 +00:00
});
$("#hide_selected_prop").click(function () {
2018-12-26 09:16:22 +00:00
var property = $("#prop_selection").val();
var column = val_to_column(property);
2018-12-21 12:41:15 +00:00
2018-12-26 09:16:22 +00:00
$(`td:nth-child(${column}),th:nth-child(${column})`).hide();
2018-12-21 12:41:15 +00:00
});
$("#submit_country").click(function () {
2018-12-26 09:16:22 +00:00
var country_name = $("#country_name").val();
var birth_rate_per_1000 = $("#country_birth").val();
var cell_phones_per_100 = $("#country_cellphone").val();
$.ajax({
type: "POST",
url: "items",
async: true,
data: `{"name":"${country_name}", "birth_rate_per_1000":"${birth_rate_per_1000}", "cell_phones_per_100":"${cell_phones_per_100}"}`,
contentType: "application/json",
success: function (data) { },
error: function (jqXHR, text, err) {
alert(err);
}
});
2018-12-21 12:41:15 +00:00
return false;
});
$("#rm_submit").click(function () {
2018-12-26 09:16:22 +00:00
var url = "/items";
var id = $("#country_delete_id").val();
if (id.length != 0) {
url += "/" + id;
}
$.ajax({
type: "DELETE",
url: url,
async: true,
success: function (data) { },
error: function (jqXHR, text, err) { }
})
2018-12-21 12:41:15 +00:00
return false;
});
2018-12-26 09:16:22 +00:00
var props = [];
2018-12-21 12:41:15 +00:00
2018-12-26 09:16:22 +00:00
// fill properties
$("document").ready(function () {
$.ajax({
type: "GET",
url: "/properties",
async: true,
success: function (data) {
$(function () {
$.each(data, function (i, item) {
$('#prop_selection').append($("<option />").val(item).text(item));
});
});
props = data;
},
error: function (jqXHR, text, err) {
alert(err);
}
});
});
function val_to_column(value) {
for (var i = 0; i < props.length; i++) {
if (value == props[i]) {
return i + 1;
}
}
return -1;
}