Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Knockout.js knockoutjs和jQuery模板-将值传递给模板中的单击处理程序 设置:_Knockout.js_Jquery Templates - Fatal编程技术网

Knockout.js knockoutjs和jQuery模板-将值传递给模板中的单击处理程序 设置:

Knockout.js knockoutjs和jQuery模板-将值传递给模板中的单击处理程序 设置:,knockout.js,jquery-templates,Knockout.js,Jquery Templates,我正在使用默认jQuery模板的knockout 我有一个下拉列表(组合)和一张桌子。该表包含项目列表。每行有一个带有项目名称的td和一个带有“删除”链接的td 如果项目在组合中,则它不在表中,反之亦然 如果我在组合中选择一个项目,会发生三件事: 该值将写入我的模型(this.selectedCostCentre=ko.observable(“”);) 该项被添加到表中 该项将从组合中删除 到目前为止,一切顺利。问题是,当我从表中删除一个项目时,我想将其添加回组合: 我使用删除按钮删除该项目。我

我正在使用默认jQuery模板的knockout

我有一个下拉列表(组合)和一张桌子。该表包含项目列表。每行有一个带有项目名称的td和一个带有“删除”链接的td

如果项目在组合中,则它不在表中,反之亦然

如果我在组合中选择一个项目,会发生三件事:

  • 该值将写入我的模型(this.selectedCostCentre=ko.observable(“”);)

  • 该项被添加到表中

  • 该项将从组合中删除

  • 到目前为止,一切顺利。问题是,当我从表中删除一个项目时,我想将其添加回组合:

    我使用删除按钮删除该项目。我的问题是,我找不到一种方法来引用正在删除的项目

    所以,问题是:

    <fieldset style="padding-top:10px;">
        <legend>Actividades Asociadas</legend>
        <table>
            <thead>
                <tr>
                    <th>
                        Actividad
                    </th>
                    <th>&nbsp;&nbsp;</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>&nbsp;&nbsp;</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>&nbsp;&nbsp;</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>&nbsp;&nbsp;</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/>