Assignment 4 Task 2 Part 2

 avatar
user_1373341
html
7 months ago
2.1 kB
0
Indexable
Never
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Question 2</title>
</head>
<body>
	<button onclick="makeAjaxQuery()">Get Car service repository</button>
	<div id="display"></div>

	<script type="text/javascript">
		function makeAjaxQuery(){
			var xhttp = new XMLHttpRequest();
			xhttp.onreadystatechange = function(){
				readyStateChangeHandler(xhttp);
			};

			xhttp.open("GET", "question2.json", true);
			xhttp.send();
		}

		function readyStateChangeHandler(xhttp){
			if (xhttp.readyState == 4){
				if(xhttp.status == 200){
					handleStatusSuccess(xhttp);
				} else{
					handleStatusFailure(xhttp);
				}
			}
		}

		function handleStatusFailure(xhttp){
			var display = document.getElementById("display");
			display.innerHTML = "XMLHttpRequest failed: status " + xhttp.status;
		}

		function handleStatusSuccess(xhttp){
			var jsonText = xhttp.responseText;
			var carObj = JSON.parse(jsonText);
			displayRepository(carObj);
			console.log(carObj);
		}

		function displayRepository(carObj) {
			var html = "";

			html += "<h3> <u> Car service repository </u> </h3>";
			html += "<p> <i> <b> Car Service Station: </b>" + carObj.station + "</i> <br /> <br />";

			for(var i = 0; i < carObj.carList.length; i++) {
				var carListObj = carObj.carList[i];

				html += "<b style='padding-left: 40px'> Car plate: </b>" + carListObj.plate + "<br />";
				html += "<b style='padding-left: 40px'> Model: </b>" + carListObj.model + "<br />";
				html += "<b style='padding-left: 40px'> Year: </b>" + carListObj.year + "<br />";
				html += "<b style='padding-left: 40px'> Service Card: </b>";

				for (var j = 0; j < carListObj.serviceCardList.length; j++) {
					var serviceListObj = carListObj.serviceCardList[j];

					html += "<ul style='padding-left: 130px;'> <li>" + serviceListObj.date + ", service " + serviceListObj.service + "</li> </ul>";
				}

				html += "<br /></p>";
			}

			var display = document.getElementById("display");
			display.innerHTML = html;
		}
	</script>
</body>
</html>