0
我正在使用Knockout.js。我有一個HTML表單,用戶可以在表格中添加一個條目。以下是我的代碼。Knockout.js:如何防止在表中添加重複項
問題是您可以創建重複的條目。我不想這樣做。
我該如何補救它?名稱和StaffNumber:
HTML
<div class="span12">
<button style="margin-bottom: 10px;" class="btn" data-bind="click: function() { ViewModel.AddIntMember() }"><i class="icon-plus"></i>Add</button>
</div>
<div class="span8">
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Staff No</th>
</tr>
</thead>
<tbody data-bind="foreach: ViewModel.RiskAssessment.IntTeam">
<tr>
<td>
<button class="btn btn-small" data-bind="click: function() { ViewModel.StaffViewModel.Remove($data) }">
<i class="icon-remove"></i>
Remove</button>
</td>
<td data-bind="text: Name"></td>
<td data-bind="text: StaffNo"></td>
</tr>
</tbody>
</table>
</div>
JS功能通過增加兩個可觀察性,以您的視圖模型asscociated
AddIntMember: function() {
LoadStaff("", 0);
$("#InternalStaffPopup").bPopup({ positionStyle: "fixed", scrollBar: true });
},
Select: function (staffMember) {
ViewModel.RiskAssessment.IntTeam.push({ Id: 0, RiskAssessmentId: 0, StaffNo: staffMember.StaffNo, Name: staffMember.Name });
},
Remove: function (staffMember) {
ViewModel.RiskAssessment.IntTeam.remove(staffMember);
},
謝謝你的回答。將嘗試一下,讓你知道... – DextrousDave
謝謝。欣賞你的答案 – DextrousDave