我处理payment received
属性的行级版的方式如下所示:
<tr ng-repeat="item in Event.ParticipantPledges">
<div>
<span ng-if="item.Id != Event.ParticipantPledge.Id">{{item.PaymentReceived | currency}}</span>
<input type="number" ng-if="item.Id == Event.ParticipantPledge.Id" name="paymentReceived" ng-model="Event.ParticipantPledge.PaymentReceived" required />
</div>
</tr>
我有一个如上所示的网格。我知道如何编辑行级别,如上所示。但我的问题是如何一次为所有行执行此操作。换句话说,批量编辑。我可以给出一个按钮{ {1}}然后用户可以编辑任何行,之后他可以更新记录。但是当我提供这样的功能时,如果用户更改了一个项目的值,那么它将应用于该项目的所有行。知道这是由于双向数据绑定性质而发生的。但是你能告诉我如何避免这种情况吗?提前谢谢。
注意:简单的例子绰绰有余。
答案 0 :(得分:3)
我已经使用了很棒的Angular-xeditable指令。
使用它进行游戏:JSFiddle
HTML:
<form editable-form name="tableform" onaftersave="saveTable()" oncancel="cancel()">
<!-- table -->
<table class="table table-bordered table-hover table-condensed">
<tr style="font-weight: bold">
<td style="width:40%">Name</td>
<td style="width:30%">Status</td>
<td style="width:30%">Group</td>
<td style="width:30%"><span ng-show="tableform.$visible">Action</span></td>
</tr>
<tr ng-repeat="user in users | filter:filterUser">
<td>
<!-- editable username (text with validation) -->
<span editable-text="user.name" e-form="tableform" onbeforesave="checkName($data, user.id)">
{{ user.name || 'empty' }}
</span>
</td>
<td>
<!-- editable status (select-local) -->
<span editable-select="user.status" e-form="tableform" e-ng-options="s.value as s.text for s in statuses">
{{ showStatus(user) }}
</span>
</td>
<td>
<!-- editable group (select-remote) -->
<span editable-select="user.group" e-form="tableform" onshow="loadGroups()" e-ng-options="g.id as g.text for g in groups">
{{ showGroup(user) }}
</span>
</td>
<td><button type="button" ng-show="tableform.$visible" ng-click="deleteUser(user.id)" class="btn btn-danger pull-right">Del</button></td>
</tr>
</table>
<!-- buttons -->
<div class="btn-edit">
<button type="button" class="btn btn-default" ng-show="!tableform.$visible" ng-click="tableform.$show()">
edit
</button>
</div>
<div class="btn-form" ng-show="tableform.$visible">
<button type="button" ng-disabled="tableform.$waiting" ng-click="addUser()" class="btn btn-default pull-right">add row</button>
<button type="submit" ng-disabled="tableform.$waiting" class="btn btn-primary">save</button>
<button type="button" ng-disabled="tableform.$waiting" ng-click="tableform.$cancel()" class="btn btn-default">cancel</button>
</div>
</form>
</div>
JS:
app.controller('EditableTableCtrl', function($scope, $filter, $http, $q) {
$scope.users = [
{id: 1, name: 'awesome user1', status: 2, group: 4, groupName: 'admin'},
{id: 2, name: 'awesome user2', status: undefined, group: 3, groupName: 'vip'},
{id: 3, name: 'awesome user3', status: 2, group: null}
];
$scope.statuses = [
{value: 1, text: 'status1'},
{value: 2, text: 'status2'},
{value: 3, text: 'status3'},
{value: 4, text: 'status4'}
];
$scope.groups = [];
$scope.loadGroups = function() {
return $scope.groups.length ? null : $http.get('/groups').success(function(data) {
$scope.groups = data;
});
};
$scope.showGroup = function(user) {
if(user.group && $scope.groups.length) {
var selected = $filter('filter')($scope.groups, {id: user.group});
return selected.length ? selected[0].text : 'Not set';
} else {
return user.groupName || 'Not set';
}
};
$scope.showStatus = function(user) {
var selected = [];
if(user.status) {
selected = $filter('filter')($scope.statuses, {value: user.status});
}
return selected.length ? selected[0].text : 'Not set';
};
$scope.checkName = function(data, id) {
if (id === 2 && data !== 'awesome') {
return "Username 2 should be `awesome`";
}
};
// filter users to show
$scope.filterUser = function(user) {
return user.isDeleted !== true;
};
// mark user as deleted
$scope.deleteUser = function(id) {
var filtered = $filter('filter')($scope.users, {id: id});
if (filtered.length) {
filtered[0].isDeleted = true;
}
};
// add user
$scope.addUser = function() {
$scope.users.push({
id: $scope.users.length+1,
name: '',
status: null,
group: null,
isNew: true
});
};
// cancel all changes
$scope.cancel = function() {
for (var i = $scope.users.length; i--;) {
var user = $scope.users[i];
// undelete
if (user.isDeleted) {
delete user.isDeleted;
}
// remove new
if (user.isNew) {
$scope.users.splice(i, 1);
}
};
};
// save edits
$scope.saveTable = function() {
var results = [];
for (var i = $scope.users.length; i--;) {
var user = $scope.users[i];
// actually delete user
if (user.isDeleted) {
$scope.users.splice(i, 1);
}
// mark as not new
if (user.isNew) {
user.isNew = false;
}
// send on server
results.push($http.post('/saveUser', user));
}
return $q.all(results);
};
});
答案 1 :(得分:0)
据我所知,我认为您希望第一时间为所有人提供价值,然后根据需要为每一行提供单独的价值。
为此,请选择Edit all Button并在该按钮上打开弹出的onclick事件。 在弹出窗口中接受输入并使用foreach方法更新角度对象。所以它适用于所有对象。
我认为这会对你有帮助......
升级可能会对你有帮助......
public class Session
{
public String UserName { get; set; }
public String Token { get; set; }
}
public class SessionManager
{
private static Session _session;
public static Session CurrentSession
{
get
{
return _session;
}
}
public static void Login(string username, string password)
{
// login the user
_session = new Session();
}
}
答案 2 :(得分:0)
编辑时所有行具有相同值的问题是您将所有行的绑定输入到同一模型&#34; ng-model =&#34; Event.ParticipantPledge.PaymentReceived&#34;。相反,你应该绑定到那个行模型,即绑定输入,例如item.paymentReceived。然后,当您在外部更新所有按钮后,您可以遍历所有项目并获取值并更新数据库。
http://jsfiddle.net/twx2p7s9/1/
<div ng-repeat="line in lines">
<input ng-model="line.text"/>
</div>