我正在尝试使用淘汰赛 inbuild WITH Binding 做简单的行为。有使用 ko.applyBinding 与 ViewModel 绑定的 UL-LI 列表。工作正常。现在,我正在尝试使用列表实现选择项行为。因此在 ViewModel 中实现了 selectPatient 行为,并使用 Click 将其与 LI 标记绑定。调用该方法并设置可观察的 selectedPatient 变量。
现在在 UL-LI 同一级别的父 div 内有一个 div,我正在尝试使用 With 与 selectedPatient 绑定。不工作。
参考代码:
<div data-role="content" id="MyPatientDiv">
<div>
<ul data-bind="foreach:Patients">
<li data-bind="click:$root.selectPatient">
<table>
<tr>
<td>
<a href="javascript:" id="pName" sortorder="none"><span data-bind="text:LastName"></span>
,<span data-bind="text:$data.FirstName"></span></a>
</td>
<td>
<a href="javascript:" id="pid" sortorder="none"><span data-bind="text:$data.DisplayId">
</span></a>
</td>
<td>
<a href="javascript:" id="pLocation" sortorder="none"><span data-bind="text:$data.UnitName">
</span></a>
</td>
</tr>
</table>
</li>
</ul>
</div>
<div id="dlgAction" databind="with:selectedPatient">
What would you like to do for <span databind="text:$data.LastName"></span>,<span databind="text:$data.FirstName"></span>
</div>
</div>
脚本代码:
function PatientsModel(data) {
var self = this;
self.Patients = ko.observableArray([]);
if (data.patientEncounters == null) self.Patients([]);
else self.Patients(data);
self.selectedPatient = ko.observable(null);
self.selectPatient = function(data, target) {
self.selectedPatient(data);
//$('#dlgAction').dialog('open');
};
}
MyPatientList = {
PatientsModel: null,
DOMElementId: 'MyPatientDiv'
}
$(function(){
var data = [{
AccountId: null,
AdmissionStatus: 1
AttendingPhysicians: null,
BedId: null,
DisplayId: "32456",
EncounterKey: "00000000000000000000000000000003",
FirstName: "John",
Gender: null,
LastName: "Rambo",
LocalPatient: true,
MiddleName: null,
MyPatient: true,
PatientId: null,
PatientKey: "00000000000000000000000000000003",
RoomName: null,
SameName: false,
TemporaryPatient: false,
UnitName: "IC1",
VisitId: null}];
data[0].LastName;
MyPatientList.PatientsModel = new PatientsModel(data);
ko.applyBindings(MyPatientList.PatientsModel, $('#MyPatientDiv')[0]);
});
难道我做错了什么?
问候苏梅特