我有一个选择下拉菜单,显示对象列表,name
属性是显示的文本,id
属性是每个选项绑定的值,以及{{1来自另一个对象的单独属性的绑定。
value: user.id
当我从下拉列表中选择新的人物对象时,只会更新<td data-bind=""><select data-bind="options: peopleResponsible, optionsText: 'name', optionsValue: 'id', value: user.id"></select></td>
上的id
。所有其他属性(名称,用户名,年龄等)都没有更新。
我需要做的是当选择新的user
选项时,我希望它将所有属性从该对象复制到peopleResponsible
对象
我怀疑目前它不起作用,因为user
对象本身不可观察,只有它的属性。以下是我的数据映射方式:
user
ko.mapping.fromJS(taskOutlines, {}, mappedTaskOutlines);
包含多个TaskOutline
,每个Tasks
包含一个Task
。
有什么想法吗?
答案 0 :(得分:2)
你可以这样做:
var vm = {
peopleResponsible: ko.observableArray([{
id: ko.observable(1),
name: ko.observable("p1")
}, {
id: ko.observable(2),
name: ko.observable("p2")
}, {
id: ko.observable(3),
name: ko.observable("p3")
}]),
selectedUser: ko.observable()
}
vm.selectedUser(vm.peopleResponsible()[1]); // pre select a user
ko.applyBindings(vm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<select data-bind="options: peopleResponsible, optionsText: 'name', value: selectedUser"></select>
<div data-bind="with: selectedUser">
<p>Id:
<label data-bind="text: id"></label>
</p>
<p>Name:
<label data-bind="text: name"></label>
</p>
</div>
当选择时,选择将是对可观察数组,属性和所有对象的任意对象的引用。然后,此选定对象将被置于“selectedUser”可观察状态。
简而言之,删除“optionsValue”绑定将绑定整个对象而不是id属性。
答案 1 :(得分:0)
class Employee
{
public:
Employee(){}
Employee(int x, string y, string z) : Id(x), Name(y), Department(z) {}
~Employee(){}
void setId(int w) { Id = w; }
int getId() const
{
return Id;
}
void printId();
void printPoly();
void printName();
void printSalary();
void setSalary();
protected:
int Id;
string Name;
string Department;
};
void Employee::printId()
{
cout << "Employee's ID is: " << getId() << endl;
}
void Employee::printPoly()
{
cout << "Bu bir poly ornegidir" << endl;
}
class Salary : public Employee
{
public: //<== this was missing
Salary(int m) : mSalary(m){} //<== this was wrong
Salary(){}
~Salary(){}
void setSalary(int c){ mSalary = c; }
int getSalary() const
{
return mSalary;
}
void printSalary()
{
cout << "Aylık Maasi: " << getSalary() /*brackets were missing*/<< "Haftalik Maasi: " << getSalary() /*brackets were missing*// 4 << endl;
}
void printPoly()
{
cout << "Bu bir override ornegidir" << endl;
}
protected:
int mSalary;
};
int main()
{
Employee Fuat(130, "Fuat", "Beykent");
Fuat.setId(131);
Fuat.printName();
Fuat.printPoly(); //<== brakets were missing
Salary Ahmet;
Ahmet.setSalary(1200);
Ahmet.printSalary();
return 0;
}
绑定仅设置一个可观察对象。不知道如何使用东西,很难说你应该怎么做才能得到你想要的结果。一种可能性是创建一个执行复制的函数,并value
创建subscribe
。