仍在尝试对 Knockout 映射插件进行微调。我很接近,但无法从 Json(谷歌图书 api)获取图像链接来显示图像。不确定我是否应该尝试深入了解 HTML 上的 Json 或重组映射数据。无论哪种方式我都会很高兴。在此先感谢您的帮助。
HTML...
<body>
<h2>Find Cat in the Hat</h2>
<div>
<input id="booksearch" /><input id="btnTest" type="button" value="button" />
</div>
<div>
<table id="bookresults">
<thead>
<tr>
<th colspan="6">Books</th>
</tr>
</thead>
<tbody data-bind="foreach: model.items">
<tr>
<td><input type="button" title="Select" value="Select" /></td>
<td data-bind="text: volumeInfo.title"></td>
<td data-bind="text: volumeInfo.description"></td>
<td><table><tbody><!-- ko foreach: volumeInfo.industryIdentifiers --><tr><td data-bind="text: identifier"></td></tr><!-- /ko --></tbody></table></td>
<td><table><tbody><!-- ko foreach: volumeInfo.categories --><tr><td data-bind="text: $data"></td></tr><!-- /ko --></tbody></table></td>
<td><table><tbody><!-- ko foreach: items.volumeInfo --><tr><td><img data-bind="attr: {src: imageLinks.thumbnail}" /></td></tr><!-- /ko --></tbody></table></td>
</tr>
<tr>
</tr>
</tbody>
</table>
</div>
<script src="/Scripts/jquery-1.8.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="/Scripts/knockout-2.2.0.js"></script>
<script src="/Scripts/knockout.mapping-latest.js"></script>
<script src="/Scripts/ireadabook.js"></script>
</body>
.js 文件....
$(document).ready(function () {
//Knockout Test
$('#btnTest').click(function () {
var url = "https://www.googleapis.com/books/v1/volumes?q=the+Cat+In+The+Hat";
var viewModel = {};
$.getJSON(url, function (data) {
viewModel.model = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
//Show the results after selected
$('#bookresults').css("display", "block");
});
});