编辑,删除和下拉显示正确的字段淘汰赛js

时间:2015-12-15 07:53:11

标签: javascript knockout.js

所以我有一个淘汰js模板,显示这样的字段 enter image description here,可以编辑,删除并添加新行。 因此,当我点击即时编辑时,它会显示"选择公司"而不是给我第一个选项作为Instantor进行编辑。enter image description here 最后,当我尝试添加一个新项目时,它没有显示选择公司它只是首先提供即时选项而不是选择公司'显示如下。 enter image description here

所以我要修复的是,虽然编辑它应该显示即时选择而不是选择公司,当我添加新项目时,它应该显示选择公司而不是复制顶部字段。

以下是模板的代码。

 <script id="RRitemTmpl" type="text/html">
    <tr>           
        <td class="" data-bind="text: Company"></td>
        <td class="text-center" data-bind="text: third_party_rr_value"></td>
        <td class="text-center" style="vertical-align:middle;">
            <div data-bind="if: status() > 0">
                <button type="button" class="btn btn-default btn-xs" data-bind="click: $parent.editItem">Edit</button>
                <button type="button" class="btn btn-default btn-xs" data-bind="click: $parent.deleteItem">Delete</button>
            </div>
            <div data-bind="if: status() < 1">
                <span data-bind="text: status_description"></span>
            </div>
        </td>
    </tr>
</script>

<script id="RReditTmpl" type="text/html">
    <tr>
      <td class="text-center">
            <select id="selected_currency_local" class="form-control input-sm" data-bind="options: $root.availableCompanies,
                    optionsText: 'text_value',
                    value: $parent.selectedCompanyLocal,
                    optionsCaption: 'Choose Company'">
            </select>
        </td> 
        <td class="text-center">
            <input type="text" class="form-control input-sm text-left" data-bind="value: third_party_rr_value" />
        </td>

        <td class="text-center" style="vertical-align:middle;">
            <button type="button" class="btn btn-default btn-xs" data-bind="click: $parent.acceptItemEdit">Accept</button>
            <button type="button" class="btn btn-default btn-xs" data-bind="click: $parent.cancelItemEdit">Cancel</button>
        </td>

    </tr>
</script>

如果有必要,我会给出js文件代码。

var ThirdPartyRRViewModel = function(parent, items) {

        var root = parent;
        var self = this;

        this.items = ko.observableArray(items);

        this.selectedItem = ko.observable();

        /*Observable for storing the data of the company selected */
        self.selectedCompanyLocal = ko.observable();

        this.addItem = function() {


             $.each(root.availableCompanies(), function (index, companyItem) {
                if (companyItem.ID == root.userRiskRate().Company) {
                    self.selectedCompanyLocal(companyItem);
                }
            });

            var newThird = {"ID":0, "ORIG_ID":0, "Company":self.selectedCompanyLocal().text_value, 
                            "third_party_rr_value":0, 
                            "status":1, "status_description":"Active"};

            var newItem = new ThirdPartyItem(newThird);
            self.items.push(newItem);
            self.selectedItem(newItem);
        };

        this.deleteItem = function(itemToDelete) {
            /* If ORIG_ID is greater than > 0, useropenloan row has been read from the database 
               and can not be removed, but cancelled. */
            if (itemToDelete.ORIG_ID() > 0) {
                itemToDelete.status(-1); /* Indicates to server that this item should be updated to cancelled. */ 
                itemToDelete.status_description('Will be cancelled');
                itemToDelete.itemIsEdited(1);
            } else {
                 self.items.remove(itemToDelete);
            }
            self.selectedItem(null);
        };

        this.editItem = function(ThirdPartyItem) {
            $.each(root.availableCompanies(), function (index, companyItem) {
                if (companyItem.ID == ThirdPartyItem.Company()) {
                    self.selectedCompanyLocal(companyItem);
                }
            });
            self.selectedItem(ThirdPartyItem);
        };

        this.acceptItemEdit = function() {
            self.selectedItem().third_party_rr_type(self.selectedCompanyLocal().text_code);  
            self.selectedItem().Company(self.selectedCompanyLocal().text_value);  
            self.selectedItem().third_party_rr_type.commit();
            self.selectedItem().Company.commit();
            self.selectedItem().third_party_rr_value.commit();
            self.selectedItem().itemIsEdited(1);
            self.selectedItem(null);
            self.selectedCompanyLocal(null);
        };

        this.cancelItemEdit = function() {
            self.selectedItem().Company.reset();
            self.selectedItem().third_party_rr_value.reset();
            self.selectedItem(null);
            self.selectedCompanyLocal(null);
        };

        this.templateToUse = function(item) {
            return self.selectedItem() === item ? "RReditTmpl" : "RRitemTmpl";
        };
    };

1 个答案:

答案 0 :(得分:1)

在代码更改"Company":self.selectedCompanyLocal().text_value中,转到"Company":self.selectedCompanyLocal() 它应该工作。