我想用搜索数据更新数据宾灵通-LI列表的内容。我正在使用以下ViewModel
function PatientsModel(data)
{
var self = this;
self.Patients = ko.observableArray([]);
self.Patients(data.Patients);
self.addPatient = function (model, event)
{
alert("Patients to Add: " + model.LastName + ',' + model.FirstName);
//Replace with AJAX Calls : self.people.push({ name: "New at " + new Date() });
};
self.removePatient = function (model, event)
{
alert("Patients to Delete:" + model.LastName + ',' + model.FirstName);
//Replace with AJAX calls : self.people.remove(this);
}
self.RefreshData = function (data)
{
self.Patients = ko.observableArray([]);
self.Patients(data.Patients);
}
}
要刷新我创建的RefreshData的内容,该方法将更新患者,它是data-bind " foreach:foreach:derta forth:dteral" ul
>我正在执行以下绑定:
AllPatientList.PatientsModel = null;
AllPatientList.PatientsModel = new PatientsModel(data);
ko.applyBindings(AllPatientList.PatientsModel, $('#AllPatientDiv>div>ul')[0]);
并随后更新视图模型的内容:
if (AllPatientList.PatientsModel != null && AllPatientList.PatientsModel != undefined)
{
AllPatientList.PatientsModel.RefreshData(data);
}
else
{
AllPatientList.PatientsModel = new PatientsModel(data);
ko.applyBindings(AllPatientList.PatientsModel, $('#AllPatientDiv>div>ul')[0]);
}
但是它不起作用,UL的内容不更改。
此外,我尝试进行以下操作:
ko.cleanNode($('#AllPatientDiv>div>ul')[0]);
AllPatientList.PatientsModel = new PatientsModel(data);
ko.applyBindings(AllPatientList.PatientsModel, $('#AllPatientDiv>div>ul')[0]);
它正在使用重复/重复的数据输入生产列表。它显示了9个列表项目而不是3。(每个重复3次)。
我无法弄清楚我在这里做了什么。ko.cleannode()也没有从列表中删除内容。请提供帮助,我如何重新用更新内容的UL -LI列表。
它们没有变化,因为您的刷新功能正在打破绑定。当您第一次调用applyBindings()
时,将创建对Patients
数组的订阅。当您调用RefreshData
时,您是覆盖带有新的数组。这个新数组没有绑定订阅。
如果要清除旧数组,请在添加新数据之前使用removeAll
。这将保持绑定完整。
编辑:
这是一个非常简单的小提琴,演示了此