Knockout.js knockoutjs和jQuery模板-将值传递给模板中的单击处理程序 设置:
我正在使用默认jQuery模板的knockout 我有一个下拉列表(组合)和一张桌子。该表包含项目列表。每行有一个带有项目名称的td和一个带有“删除”链接的td 如果项目在组合中,则它不在表中,反之亦然 如果我在组合中选择一个项目,会发生三件事:Knockout.js knockoutjs和jQuery模板-将值传递给模板中的单击处理程序 设置:,knockout.js,jquery-templates,Knockout.js,Jquery Templates,我正在使用默认jQuery模板的knockout 我有一个下拉列表(组合)和一张桌子。该表包含项目列表。每行有一个带有项目名称的td和一个带有“删除”链接的td 如果项目在组合中,则它不在表中,反之亦然 如果我在组合中选择一个项目,会发生三件事: 该值将写入我的模型(this.selectedCostCentre=ko.observable(“”);) 该项被添加到表中 该项将从组合中删除 到目前为止,一切顺利。问题是,当我从表中删除一个项目时,我想将其添加回组合: 我使用删除按钮删除该项目。我
<fieldset style="padding-top:10px;">
<legend>Actividades Asociadas</legend>
<table>
<thead>
<tr>
<th>
Actividad
</th>
<th> </th>
</tr>
</thead>
<tbody data-bind="template: {name:'actividadesAsociadas', foreach: viewModel.costCentres}"></tbody>
</table>
</fieldset>
<script type="text/x-jquery-tmpl" id="actividadesAsociadas">
<tr>
<td data-bind="text: NameCC"></td>
<td><a href="#" data-bind="click: function() { viewModel.removeCC('how to identify the item being deleted?') }">Delete</a></td>
</tr>
</script>
<fieldset>
<legend>Asociar Actividades a la Cuenta</legend>
<div class="editor-label">
Elija Actividad
</div>
<div class="editor-field">
<select id="All" data-bind="options: allCostCentres, value: selectedCostCentre, optionsValue: 'CostCentreId', optionsText: 'NameCC', optionsCaption: 'Choose...'"></select>
</div>
<input type="hidden" name="AccountId" id="AccountId" value="@Model.AccountId" />
</fieldset>
如何将表中删除的项的值传递到viewmodel上处理删除链接的数据绑定单击事件的方法中
代码:
表格:
<fieldset style="padding-top:10px;">
<legend>Actividades Asociadas</legend>
<table>
<thead>
<tr>
<th>
Actividad
</th>
<th> </th>
</tr>
</thead>
<tbody data-bind="template: {name:'actividadesAsociadas', foreach: viewModel.costCentres}"></tbody>
</table>
</fieldset>
<script type="text/x-jquery-tmpl" id="actividadesAsociadas">
<tr>
<td data-bind="text: NameCC"></td>
<td><a href="#" data-bind="click: function() { viewModel.removeCC('how to identify the item being deleted?') }">Delete</a></td>
</tr>
</script>
<fieldset>
<legend>Asociar Actividades a la Cuenta</legend>
<div class="editor-label">
Elija Actividad
</div>
<div class="editor-field">
<select id="All" data-bind="options: allCostCentres, value: selectedCostCentre, optionsValue: 'CostCentreId', optionsText: 'NameCC', optionsCaption: 'Choose...'"></select>
</div>
<input type="hidden" name="AccountId" id="AccountId" value="@Model.AccountId" />
</fieldset>
协会活动
激进分子
组合:
<fieldset style="padding-top:10px;">
<legend>Actividades Asociadas</legend>
<table>
<thead>
<tr>
<th>
Actividad
</th>
<th> </th>
</tr>
</thead>
<tbody data-bind="template: {name:'actividadesAsociadas', foreach: viewModel.costCentres}"></tbody>
</table>
</fieldset>
<script type="text/x-jquery-tmpl" id="actividadesAsociadas">
<tr>
<td data-bind="text: NameCC"></td>
<td><a href="#" data-bind="click: function() { viewModel.removeCC('how to identify the item being deleted?') }">Delete</a></td>
</tr>
</script>
<fieldset>
<legend>Asociar Actividades a la Cuenta</legend>
<div class="editor-label">
Elija Actividad
</div>
<div class="editor-field">
<select id="All" data-bind="options: allCostCentres, value: selectedCostCentre, optionsValue: 'CostCentreId', optionsText: 'NameCC', optionsCaption: 'Choose...'"></select>
</div>
<input type="hidden" name="AccountId" id="AccountId" value="@Model.AccountId" />
</fieldset>
一个拉昆塔协会的活动
艾莉娅·阿维达德
找出问题:
<fieldset style="padding-top:10px;">
<legend>Actividades Asociadas</legend>
<table>
<thead>
<tr>
<th>
Actividad
</th>
<th> </th>
</tr>
</thead>
<tbody data-bind="template: {name:'actividadesAsociadas', foreach: viewModel.costCentres}"></tbody>
</table>
</fieldset>
<script type="text/x-jquery-tmpl" id="actividadesAsociadas">
<tr>
<td data-bind="text: NameCC"></td>
<td><a href="#" data-bind="click: function() { viewModel.removeCC('how to identify the item being deleted?') }">Delete</a></td>
</tr>
</script>
<fieldset>
<legend>Asociar Actividades a la Cuenta</legend>
<div class="editor-label">
Elija Actividad
</div>
<div class="editor-field">
<select id="All" data-bind="options: allCostCentres, value: selectedCostCentre, optionsValue: 'CostCentreId', optionsText: 'NameCC', optionsCaption: 'Choose...'"></select>
</div>
<input type="hidden" name="AccountId" id="AccountId" value="@Model.AccountId" />
</fieldset>
我遇到的问题是:
<td><a href="#" data-bind="click: function() { viewModel.removeCC('how to identify the item being deleted?') }">Delete</a></td>
好的,刚才回答了我自己的问题:语法是:
<a href="#" data-bind="click: function() { viewModel.removeCC($data) }">Delete<a/>