看到你的小提琴后,我尝试了一个不涉及 jQuery 的解决方案,使用 Knockout 将该 Javascript 对象映射到 HTML 表。
http://jsfiddle.net/EfrainReyes/r6Vke/1/
HTML:
<table>
<thead>
<tr>
<th>Caracteristicas</th>
<!-- ko foreach: planNames -->
<th data-bind="text: $data"></th>
<!-- /ko -->
</tr>
</thead>
<tbody data-bind="foreach: caracteristicas">
<tr>
<td data-bind="text: $data"></td>
<!-- ko foreach: { data: $root.rows($data) } -->
<td data-bind="text: $data ? $data : '...'">
</td>
<!-- /ko -->
</tr>
</tbody>
</table>
JS:
viewModel.planNames = ko.computed(function () {
return ko.utils.arrayMap(this.plans(), function (plan) {
return plan.PlanName();
});
}, viewModel);
viewModel.caracteristicas = ko.computed(function () {
var descripciones = ko.utils.arrayMap(this.plans(), function (plan) {
return ko.utils.arrayMap(plan.Caracteristicas(), function (carac) {
return carac.Descripcion();
});
});
return ko.utils.arrayGetDistinctValues(descripciones.reduce(function (x,y) {
return x.concat(y);
}));
}, viewModel);
viewModel.rows = function(caracteristica) {
var cars = ko.utils.arrayMap(this.plans(), function (plan) {
var valor = null;
var car = ko.utils.arrayFirst(plan.Caracteristicas(), function(carac) {
return carac.Descripcion() === caracteristica;
});
if (car && car.Valor) {
valor = car.Valor();
}
return valor;
});
return cars;
};
ko.applyBindings(viewModel);