我正在嘗試使用淘汰賽創建電子表格類型的公式操作,但淘汰賽無效。淘汰賽失效:外部Json文件的價值
我的數據是從外部JSON文件
JSON文件
{
"info": [
{
"Name":"Noob Here",
"Major":"Language",
"Sex":"Male",
"English":"15",
"Japanese":"5",
"Calculus":"0",
"Geometry":"20"
}
]
}
代碼
<script type="text/javascript">
function loadData(fileName) {
// getting json from a remote file
// by returning the jqXHR object we can use the .done() function on it
// so the callback gets executed as soon as the request returns successfully
var data = $.getJSON(fileName + ".json");
return (data);
}
function fillDataTable(data) {
// iterate over each entry in the data.info array
$.each(data.info, function(index, element) {
// append it to the table
$("#div1").append("<tr><td>" + element.Name + "</td><td>" + element.Major + "</td><td>" + element.Sex + "</td><td>" + "<input data-bind='value: eng' value=" + element.English + "></td><td>" + "<input data-bind='value: jap' value=" + element.Japanese + "></td><td>" + "<input data-bind='value: cal' value=" + element.Calculus + "></td><td>" + "<input data-bind='value: geo' value=" + element.Geometry + "></td><td>" + "<strong data-bind='text: total'></td>")
});
}
$(document).ready(function() {
// the file's name. "Data" in this example.
var myFile = "Data2";
loadData(myFile).done(function(data) {
// check if we acutally get something back and if the data has the info property
if (data && data.info) {
// now fill the data table with our data
fillDataTable(data)
}
});
// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI
function AppViewModel() {
this.eng = ko.observable("0");
this.jap = ko.observable("0");
this.cal = ko.observable("0");
this.geo = ko.observable("0");
this.total = ko.computed(function() {
var tot = parseFloat(this.eng()) + parseFloat(this.jap()) + parseFloat(this.cal()) + parseFloat(this.geo());
return (tot);
}, this);
}
// Activates knockout.js
ko.applyBindings(new AppViewModel());
});
</script>
我的HTML
<table cellspacing="1" id="div1">
<thead>
<tr>
<th>Name</th>
<th>Major</th>
<th>Sex</th>
<th>English</th>
<th>Japanese</th>
<th>Calculus</th>
<th>Geometry</th>
<th>Total</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
我從JSON文件中獲取數據。但淘汰賽不起作用。
撥弄代碼:http://jsfiddle.net/KGKpw/
注:我加了小提琴只是整齊地顯示代碼。
你撥弄不起作用,因爲你還沒有得到jQuery的運行,而你正試圖從一個本地文件裝載數據。 – 2013-03-15 11:34:36
我加了小提琴就是爲了整齊地展示代碼。 – Okky 2013-03-15 11:35:45
當然,但人們通常會使用它們進行調試和幫助。 – 2013-03-15 11:38:21