【发布时间】:2015-05-18 16:14:41
【问题描述】:
带有 knockout.js 绑定当前的表看起来像这样:
source total division
00234 4506 div1
30222 456 div2
63321 23 div2
40941 189 div1
期望的输出如下所示。数据需要按division分组。
source total
div1
00234 4506
40941 189
div2
30222 456
63321 23
这是我的 ViewModel:
var ReportingViewModel;
ReportingViewModel = { Results: ko.observableArray(null) }
ReportingViewModel 通过 ajax 请求填充:
ReportingViewModel.Results(data["Data"]["Report"]);
问:我怎样才能达到想要的输出?
编辑:
这是我的看法:
<table class="table table-condensed" id="reportData">
<thead>
<tr>
<th>source</th>
<th>total</th>
<th>division</th>
</tr>
</thead>
<tbody data-bind="foreach: Results">
<tr>
<td data-bind="text: source"></td>
<td data-bind="text: total"></td>
<td data-bind="text: division"></td>
</tr>
</tbody>
</table>
<script type="text/javascript">
$(document).ready(function () {
ReportingViewModel.Results(null);
e.preventDefault();
var numbers = null;
if ($('#numbersdd').find("option:selected").length > 0) {
numbers = $('#numbersdd').find("option:selected");}
if (numbers != null) {
$.ajax({
url: '/Reporting/ReportData.aspx',
type: 'POST',
data: numbers,
dataType: 'json',
contentType: "application/json",
success: function (data) {
ReportingViewModel.Results(data["Data"]["Report"]);
},
error: function () {
alert('Error Running Report');
}
});
}
else { alert('No Data!'); }
});
var ReportingViewModel;
ReportingViewModel = {
Results: ko.observableArray(null),
}
ko.applyBindings(ReportingViewModel);
});
</script>
【问题讨论】:
-
您在页面的哪个位置调用 ko.applybindings()?
标签: javascript html knockout.js html-table ko.observablearray