2
我正在尝试为角度应用程序创建一个迷你电子表格。我想重新创建 通用电子表格功能,该功能允许用户单击电子表格 单元格,然后使用表格顶部较大的输入更改该单元格的值。 理想情况下,我想在给用户点击其中一个单元格时将给定单元格的模型分配给大量输入即时输入 ,但我无法解决如何执行此操作。动态分配角度模型输入
有一些细节可以解决细胞模糊和焦点问题。另外,我给出的例子大大简化了;可以有任意数量的行和列。我的主要问题是:如何动态地将单元模型分配给大输入,以便它可以将 作为一种代理输入到单元?如果这不可行/实用,是否有更好的方法来处理这个问题?
这是我到目前为止。我甚至不知道这是否可行,特别是我在这个指令中采用的方法。有任何想法吗?
http://plnkr.co/edit/6tTsilCGSepYyCfbvidp?p=preview
的index.html
<table ng-controller="SpreadsheetCtrl" custom-spreadsheet>
<thead>
<tr>
<th colspan="3">
<input type="text" style="width: 100%" />
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in rows">
<td>
<input ng-model="row.A" type="text" />
</td>
<td>
<input ng-model="row.B" type="text" />
</td>
<td>
<input ng-model="row.C" type="text" />
</td>
</tr>
</tbody>
</table>
的script.js
var app = angular.module('app', []);
app.controller('SpreadsheetCtrl', function($scope) {
$scope.rows = [
{A: 'a', B: 'b', C: 'c'},
{A: 'a', B: 'b', C: 'c'},
{A: 'a', B: 'b', C: 'c'}
];
});
app.directive('customSpreadsheet', function() {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var primary = element.find('thead input');
element.on('focus', 'tbody input', function() {
// of course, this won't work! but it shows the (basic) idea
// of what I'm trying to do
primary.attr('ng-model', $(this).attr('ng-model'));
});
}
};
})
尼斯。试着永远这样做。 – 2014-08-28 20:53:56
是的,范围很难弄清楚,因此也需要保存属性。祝你好运! – 2014-08-28 20:55:27
谢谢@Zach。这工作得很好。 – Divey 2014-08-28 22:08:30