我正在使用敲除将行和列动态添加到表中,但进一步我需要通过拖放来重新排序列。下面是我的视图和模型。
看法:
<table id="gridTable">
<tr data-bind="foreach: activeColumns">
<th data-bind="text: display"></th>
</tr>
<tbody id="sortable" data-bind="foreach: rows">
<tr data-bind="foreach: $root.activeColumns">
<td>
<span data-bind="visible: readonly, text: $parent[property]"></span>
<input data-bind="visible: !readonly, value: $parent[property]"/>
</td>
</tr>
</tbody>
模型:
rows: ko.observableArray([
new Person(1, "Bob", 44),
new Person(2, "Ted", 22),
new Person(3, "Jane", 55),
new Person(4, "Sue", 11)
]),
activeColumns: ko.observableArray([{property: 'id', display: 'ID', readonly: true},
{property: 'name', display: 'Name', readonly: false}, {property: 'age', display: 'Age',
readonly: false}]),
addColumn: function() {
var newProperty = this.newAttribute();
var newProperty1= this.selectedCategory();
this.activeColumns.push({property: newProperty, display: newProperty, readonly:
false});
ko.utils.arrayForEach(this.rows(), function(row) {
if (!row[newProperty]) {
row[newProperty] = ko.observable();
}
});