-
Notifications
You must be signed in to change notification settings - Fork 1
/
example5.html
87 lines (84 loc) · 4.27 KB
/
example5.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<!--/**
*
* @link https://github.com/mzm-dev
* @created September 2014
* @fb https://www.facebook.com/zakimedia
* @email mohdzaki04@gmail.com
*/-->
<html>
<head>
<meta charset="utf-8">
<meta content="IE=edge" http-equiv="X-UA-Compatible">
<meta content="width=device-width, initial-scale=1" name="viewport">
<meta content="Getting started with JSON using JavaScript and jQuery" name="description">
<meta content="Mohamad Zaki" name="author">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="http://getbootstrap.com/examples/jumbotron-narrow/jumbotron-narrow.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="header">
<ul class="nav nav-pills pull-right">
<li><a href="/JSON-Example/"><span class="glyphicon glyphicon-chevron-left"></span> Back</a></li>
<li class="active"><a href="https://github.com/pesima/JSON-Example">Get the code on Github now HighCharts</a></li>
</ul>
<h3 class="text-muted">A JSON Tutorial | <span>Reading JSON from an external file</span></h3>
</div>
<h1>Use JavaScript</h1>
<div id="placeholder-js"></div>
<script>
$.getJSON('data.json', function(data) {//external data
var output = '<table class="table table-bordered table-hover">';
output += '<tr>' +
'<th>Nama</th>' +
'<th>Jawatan/Gred</th>' +
'<th>No Telefon/Sambungan</th>' +
'<th>Bahagian</th>' +
'<th>Unit</th>';
'<tr/>';
for (var i in data.pegawai) {
output += "<tr>";
output += "<td>" + data.pegawai[i].namaPenuh + "</td>";
output += "<td>" + data.pegawai[i].jawatan + "(" + data.pegawai[i].gred + ")</td>";
output += "<td>" + data.pegawai[i].noTelefon + "samb. " + data.pegawai[i].sambungan + "</td>";
output += "<td>" + data.pegawai[i].bahagian.nama + "</td>";
output += "<td>" + data.pegawai[i].bahagian.unit + "</td>";
output += "</tr>";
}
output += "</table>";
document.getElementById("placeholder-js").innerHTML = output;
});
</script>
<h1>Use Jquery</h1>
<div id="placeholder-jq"></div>
<script>
$.getJSON('data.json', function(data) {//external data
var appenddata = [];
appenddata += '<table class="table table-bordered table-hover">';
appenddata += '<tr>' +
'<th>Nama</th>' +
'<th>Jawatan/Gred</th>' +
'<th>No Telefon/Sambungan</th>' +
'<th>Bahagian</th>' +
'<th>Unit</th>';
'<tr/>';
$.each(data.pegawai, function(i, val) {
appenddata += "<tr>";
appenddata += "<td>" + val.namaPenuh + "</td>";
appenddata += "<td>" + val.jawatan + "(" + val.gred + ")</td>";
appenddata += "<td>" + val.noTelefon + "samb. " + val.sambungan + "</td>";
appenddata += "<td>" + val.bahagian.nama + "</td>";
appenddata += "<td>" + val.bahagian.unit + "</td>";
appenddata += "</tr>";
});
appenddata += "</table>";
$('#placeholder-jq').html(appenddata);
});
</script>
</div>
</body>
</html>