简体   繁体   English

所选选项未通过淘汰更新

[英]Selected option is not updating with knockout

I can't make the ImageType(name) update when the select is changed and the data has already been created (obtained from the model), only when a new element is created I can see that when I change the select the ImageType(name) is updated ), why would this happen?当更改选择并且已经创建数据时(从模型获得)时,我不能进行图像类型(名称)更新,只有创建新元素时,我才能看到,当我更改选择ImageType(名称)时)已更新),为什么会发生这种情况?

I tried with change event (knockout) but without results我尝试了更改事件(淘汰赛)但没有结果

 function PatientInsuranceCarrierImage(plainInsuranceImage) { var self = this; self.ImageTypes = ko.observableArray(plainInsuranceImage.ImageTypes); self.ImageType = ko.observable(plainInsuranceImage.ImageType); } function PatientInsuranceCarrierImageViewModel(items) { var self = this; self.PatientInsuranceCarrierImages = ko.observableArray(items); var innerImageTypes = JSON.parse('[{\"ID\":\"BackImage\",\"Name\":\"Back Image\"},{\"ID\":\"FrontImage\",\"Name\":\"Front Image\"},{\"ID\":\"Other\",\"Name\":\"Other\"}]'); const newPatientInsuranceCarrierImage = { ImageType: "Other", ImageTypes: [], }; for (let i = 0; i < innerImageTypes.length; i++) { newPatientInsuranceCarrierImage.ImageTypes.push(innerImageTypes[i]); } // Operations self.addpatientInsuranceCarrierImage = function() { self.PatientInsuranceCarrierImages.push(new PatientInsuranceCarrierImage(newPatientInsuranceCarrierImage)); } } var plainImages = JSON.parse('[{\"ImageType\":\"Other\",\"ImageTypes\":[{\"ID\":\"BackImage\",\"Name\":\"Back Image\"},{\"ID\":\"FrontImage\",\"Name\":\"Front Image\"},{\"ID\":\"Other\",\"Name\":\"Other\"}]},{\"ImageType\":\"Other\",\"ImageTypes\":[{\"ID\":\"BackImage\",\"Name\":\"Back Image\"},{\"ID\":\"FrontImage\",\"Name\":\"Front Image\"},{\"ID\":\"Other\",\"Name\":\"Other\"}]},{\"ImageType\":\"Other\",\"ImageTypes\":[{\"ID\":\"BackImage\",\"Name\":\"Back Image\"},{\"ID\":\"FrontImage\",\"Name\":\"Front Image\"},{\"ID\":\"Other\",\"Name\":\"Other\"}]},{\"ImageType\":\"Other\",\"ImageTypes\":[{\"ID\":\"BackImage\",\"Name\":\"Back Image\"},{\"ID\":\"FrontImage\",\"Name\":\"Front Image\"},{\"ID\":\"Other\",\"Name\":\"Other\"}]}]'); var patientInsuranceCarrierImagesViewModel = new PatientInsuranceCarrierImageViewModel(plainImages); ko.applyBindings(patientInsuranceCarrierImagesViewModel);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div style="float: right; vertical-align: top; padding-bottom: 5px;"> <a data-bind="click: addpatientInsuranceCarrierImage" href="#" class="button">Add New Image</a> </div> <table id="patientInsuranceImages" style="width: 100%;" class="alt1"> <thead> <tr> <th class="Table_Top" style="width: 80px;"> Back/Front </th> </tr> </thead> <!-- ko if: PatientInsuranceCarrierImages().length > 0 --> <tbody data-bind="foreach: PatientInsuranceCarrierImages"> <tr> <td> <select data-bind="options: ImageTypes, value: ImageType, optionsValue: 'ID', optionsText:'Name'" class="txtinput" style="margin-top: 0px;"> </select> <input type="hidden" data-bind="value: ImageType, attr: {name: 'images[' + $index() + '].ImageType'}" /> <span data-bind="text: ImageType"></span> </td> </tr> </tbody> <!-- /ko --> </table>

It is because of this line:这是因为这条线:

self.PatientInsuranceCarrierImages = ko.observableArray(items);

You are just putting array of items into observable array.您只是将项目数组放入可观察数组中。 That way your ImageType property in each item is not observable so Knockout does not track changes of it.这样,您在每个项目中的ImageType属性是不可observable的,因此 Knockout 不会跟踪它的更改。 To fix this you need to map that items into collection of your defined objects at the top of your code snippet:要解决此问题,您需要将这些items映射到代码片段顶部的已定义对象的集合中:

self.PatientInsuranceCarrierImages = ko.observableArray(items.map(i => new PatientInsuranceCarrierImage(i)));

 function PatientInsuranceCarrierImage(plainInsuranceImage) { var self = this; self.ImageTypes = ko.observableArray(plainInsuranceImage.ImageTypes); self.ImageType = ko.observable(plainInsuranceImage.ImageType); } function PatientInsuranceCarrierImageViewModel(items) { var self = this; self.PatientInsuranceCarrierImages = ko.observableArray(items.map(i => new PatientInsuranceCarrierImage(i))); var innerImageTypes = JSON.parse('[{\"ID\":\"BackImage\",\"Name\":\"Back Image\"},{\"ID\":\"FrontImage\",\"Name\":\"Front Image\"},{\"ID\":\"Other\",\"Name\":\"Other\"}]'); const newPatientInsuranceCarrierImage = { ImageType: "Other", ImageTypes: [], }; for (let i = 0; i < innerImageTypes.length; i++) { newPatientInsuranceCarrierImage.ImageTypes.push(innerImageTypes[i]); } // Operations self.addpatientInsuranceCarrierImage = function() { self.PatientInsuranceCarrierImages.push(new PatientInsuranceCarrierImage(newPatientInsuranceCarrierImage)); } } var plainImages = JSON.parse('[{\"ImageType\":\"Other\",\"ImageTypes\":[{\"ID\":\"BackImage\",\"Name\":\"Back Image\"},{\"ID\":\"FrontImage\",\"Name\":\"Front Image\"},{\"ID\":\"Other\",\"Name\":\"Other\"}]},{\"ImageType\":\"Other\",\"ImageTypes\":[{\"ID\":\"BackImage\",\"Name\":\"Back Image\"},{\"ID\":\"FrontImage\",\"Name\":\"Front Image\"},{\"ID\":\"Other\",\"Name\":\"Other\"}]},{\"ImageType\":\"Other\",\"ImageTypes\":[{\"ID\":\"BackImage\",\"Name\":\"Back Image\"},{\"ID\":\"FrontImage\",\"Name\":\"Front Image\"},{\"ID\":\"Other\",\"Name\":\"Other\"}]},{\"ImageType\":\"Other\",\"ImageTypes\":[{\"ID\":\"BackImage\",\"Name\":\"Back Image\"},{\"ID\":\"FrontImage\",\"Name\":\"Front Image\"},{\"ID\":\"Other\",\"Name\":\"Other\"}]}]'); var patientInsuranceCarrierImagesViewModel = new PatientInsuranceCarrierImageViewModel(plainImages); ko.applyBindings(patientInsuranceCarrierImagesViewModel);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div style="float: right; vertical-align: top; padding-bottom: 5px;"> <a data-bind="click: addpatientInsuranceCarrierImage" href="#" class="button">Add New Image</a> </div> <table id="patientInsuranceImages" style="width: 100%;" class="alt1"> <thead> <tr> <th class="Table_Top" style="width: 80px;"> Back/Front </th> </tr> </thead> <!-- ko if: PatientInsuranceCarrierImages().length > 0 --> <tbody data-bind="foreach: PatientInsuranceCarrierImages"> <tr> <td> <select data-bind="options: ImageTypes, value: ImageType, optionsValue: 'ID', optionsText:'Name'" class="txtinput" style="margin-top: 0px;"> </select> <input type="hidden" data-bind="value: ImageType, attr: {name: 'images[' + $index() + '].ImageType'}" /> <span data-bind="text: ImageType"></span> </td> </tr> </tbody> <!-- /ko --> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM