【问题标题】:Cannot set value to select knockout无法设置值以选择淘汰赛
【发布时间】:2014-08-28 20:39:09
【问题描述】:

我刚开始使用 Knockout(KO),所以我不了解这个库的所有细节。 好的,这是我的 ViewModel:

function GetDrugs(ifOther, allDrugs, doctors, id, followUpItems, foundation) {
function PatientInfo1() {
    var self = this;
    self.PatientName = ko.observable("");
    self.PatientCode = ko.observable(0);
    self.PatientAdress = ko.observable("");
    self.PatientBirthday = ko.observable("");
    self.RecivedDate = ko.observable("");
    self.AssignedTo = ko.observable("");
    self.DateIniated = ko.observable("");
    self.IfOther = ko.observable("");
    self.IsInEnrollmentQueue = ko.observable("");
    self.IsInGrandTrackerQueue = ko.observable("");
    self.FollowUpItemsName = ko.observable("");
    self.FollowupId = ko.observable("");
    self.NotificationId = ko.observable("");
    self.GrantId = ko.observable("");
    self.FollowUpItemsDate = ko.observable("");
    self.FoundationName = ko.observable("");
    self.FoundationId = ko.observable("");
    self.GrantApprovedDate = ko.observable("");
    self.GrantExpirationDate = ko.observable("");
    self.GrantAmountApproved = ko.observable("");
    self.Notes = ko.observable("");


    self.NotificationEnrollment = ko.observable("");
    self.NotificationPatient = ko.observable("");
    self.NotificationNurse = ko.observable("");
    self.DoctorsName = ko.observable("");

    self.DrugsList = ko.observable("");

    self.ifOther = ko.observable(ifOther);

    self.allDrugs = ko.observableArray(allDrugs);
    self.AllDoctors = ko.observableArray(doctors);
    self.AllFollowUpItems = ko.observable(followUpItems);
    self.AllFoundations = ko.observable(foundation);

    self.allDrugs.push("If Other");

    self.isShown = ko.computed(function () {
        return self.PatientCode() == 0;
    });

    self.ApprovePatient =
        function () {
            var dataToApprove = {
                PatientName: self.PatientName,
                PatientCode: self.PatientCode,
                PatientAdress: self.PatientAdress,
                PatientBirthday: self.PatientBirthday,
                RecivedDate: self.RecivedDate,
                AssignedTo: self.AssignedTo,
                DateIniated: self.DateIniated,
                IfOther: self.IfOther,
                IsInEnrollmentQueue: self.IsInEnrollmentQueue,
                IsInGrandTrackerQueue: self.IsInGrandTrackerQueue,
                FollowUpItemsName: self.FollowUpItemsName,
                FollowupId: self.FollowupId,
                NotificationId: self.NotificationId,
                GrantId: self.GrantId,
                FollowUpItemsDate: self.FollowUpItemsDate,
                FoundationName: self.FoundationName,
                FoundationId: self.FoundationId,
                GrantApprovedDate: self.GrantApprovedDate,
                GrantExpirationDate: self.GrantExpirationDate,
                GrantAmountApproved: self.GrantAmountApproved,
                NotificationEnrollment: self.NotificationEnrollment,
                NotificationPatient: self.NotificationPatient,
                NotificationNurse: self.NotificationNurse,
                DoctorsName: self.DoctorsName,
                DrugsList: self.DrugsList,
                Notes: self.Notes
            };
            var dataToApproveJS = ko.toJS(dataToApprove);
            $.ajax({
                url: '/api/patient',
                type: 'POST',
                data:
                    dataToApproveJS,
                contentType: 'application/x-www-form-urlencoded; charset=utf-8',
                success: function (data) {

                }
            });
        };
    self.IsInEditingMode = ko.computed(function () {
        return self.PatientCode() == 0;
    });
};
function patientViewModel(modePatient) {
    var self = this;
    self.PatientInfo = modePatient;
    $.ajax({
        url: '/api/patient/' + id,
        cache: false,
        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        data: {},
        success: function (data) {
            self.PatientInfo.PatientName(data.PatientName);
            self.PatientInfo.PatientCode(data.PatientCode);
            self.PatientInfo.PatientAdress(data.PatientAdress);
            self.PatientInfo.PatientBirthday(data.PatientBirthday);
            self.PatientInfo.RecivedDate(data.RecivedDate);
            self.PatientInfo.AssignedTo(data.AssignedTo);
            self.PatientInfo.DateIniated(data.DateIniated);
            self.PatientInfo.IsInEnrollmentQueue(data.IsInEnrollmentQueue);
            self.PatientInfo.IsInGrandTrackerQueue(data.IsInGrandTrackerQueue);
            self.PatientInfo.FollowUpItemsName(data.FollowUpItemsName);
            self.PatientInfo.FollowupId(data.FollowupId);
            self.PatientInfo.NotificationId(data.NotificationId);
            self.PatientInfo.GrantId(data.GrantId);
            self.PatientInfo.FollowUpItemsDate(data.FollowUpItemsDate);
            self.PatientInfo.FoundationName(data.FoundationName);
            self.PatientInfo.FoundationId(data.FoundationId);
            self.PatientInfo.GrantApprovedDate(data.GrantApprovedDate);
            self.PatientInfo.GrantExpirationDate(data.GrantExpirationDate);
            self.PatientInfo.GrantAmountApproved(data.GrantAmountApproved);
            self.PatientInfo.NotificationEnrollment(data.NotificationEnrollment);
            self.PatientInfo.NotificationPatient(data.NotificationPatient);
            self.PatientInfo.NotificationNurse(data.NotificationNurse);
            self.PatientInfo.DoctorsName(data.DoctorsName);
            self.PatientInfo.DrugsList(data.DrugsList);
            self.PatientInfo.ifOther(data.ifOther);
            self.PatientInfo.Notes(data.Notes);
        }
    });
}
ko.applyBindings(new patientViewModel(new PatientInfo1()));

} 这个函数在我的 .cshtml 页面中被调用。 我创建选择:

 <div class="form-group">
    <div class="col-xs-2">
        <label class="control-label">Assigned To</label>
    </div>
    <div class="col-xs-4">
        <select class="form-control" data-bind="options: AllDoctors, optionsText: 'DoctorsName', value:alert(DoctorsName), optionsCaption:''"></select>
    </div>
</div>

除了未设置 for select 的值外,一切正常。 在页面加载时,我有空选择,可以从我的分配给医生中进行选择,但我希望有之前设置的值并将其存储在 DoctrorsName KO 变量中。

【问题讨论】:

  • 你能在 jsfiddle.net 中创建一个有问题的例子吗?
  • Knockout 可以在 JSfidle 中使用吗?
  • 是的,您应该在左侧面板的下拉菜单中选择敲除库

标签: javascript model-view-controller knockout.js


【解决方案1】:

你需要在你的绑定中绑定选中的Doctor。

在你的模型中添加一个选定的 observable:

self.selectedDoctor = ko.observable();

然后将值绑定到可观察对象:

<select class="form-control" data-bind="options: AllDoctors, optionsText: 'DoctorsName', value: selectedDoctor,  optionsCaption:''"></select>

那么如果你想得到医生的名字,你可以使用self.selectedDoctor.DoctorsName

这是一个基本的 jsfiddle 演示:

http://jsfiddle.net/CZ95C/

【讨论】:

  • 如何设置 selectedDoctor 的值?
  • 它会自动设置,因为它是由值绑定设置的
  • 在我的回答中添加了一个 jsfiddle 显示它工作
  • 你能告诉我如何让例如“尼克约翰逊”成为第一吗?
  • 您只需将 Nick Johnson 放在医生数组的第一个元素中:jsfiddle.net/CZ95C/1 这就是您的意思吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
  • 2015-11-05
  • 2014-10-26
  • 2013-05-04
  • 1970-01-01
  • 2013-10-18
相关资源
最近更新 更多