【发布时间】:2018-02-12 23:30:57
【问题描述】:
我是淘汰赛新手,对特定绑定有疑问。我正在使用 SharePoint 获取用户属性、显示它们并将其保存到共享点列表。我有其他绑定,它们运行良好。这是我所拥有的:
<div id="custom-new-form">
<a data-bind="click: $root.AddRow" href="javascript:void(0)">Add</a>
<table id="sales-returns-table" data-bind="visible: EntityRows().length > 0">
<thead>
<tr>
<th>Entity</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<tr>
<!--<td data-bind="text: ($index() + 1)"></td>-->
<td>
<select class="lookup-select" data-bind="options: $root.Entities, optionsText: 'Title', optionsValue: 'Title', value: entity"></select>
</td>
<td>
<select class="lookup-select" data-bind="options: $root.Roles, optionsText: 'Title', optionsValue: 'Title', value: role"></select>
</td>
<td>
<a data-bind="click: $root.RemoveRow" href="javascript:void(0)">X</a>
</td>
</tr>
</tbody>
</table>
</div>
这是我的 js:
var newUser;
(function (newUser) {
var NewForm;
(function (NewForm){
var LookupValue = (function () {
function LookupValue(id, title) {
this.Id = id;
this.Title = title;
}
return LookupValue;
}());
var EntityRow=(function(){
function EntityRow(parent) {
var _this=this;
this.Parent=parent;
//this.RowId=SP.Guid.newGuid().toString();
this.entity=ko.observable('');
this.role=ko.observable('');
}
return EntityRow;
}());
var Model=(function () {
function Model() {
var _this=this;
this.isid= ko.observable('');
//for user
this.firstName = ko.observable();
this.lastName=ko.observable();
this.posTitle=ko.observable();
this.email=ko.observable();
this.phone=ko.observable();
//for approver
this.approverISID= ko.observable('');
this.approverName=ko.observable();
this.approverposTitle=ko.observable();
//for row
this.EntityRows=ko.observableArray();
//for Enitities
//this.entity=ko.observable(0);
//for Roles
//this.role=ko.observable(0);
//for Entities and Roles
this.Entities=[];
this.Roles=[];
}
Model.prototype.AddRow=function(){
this.EntityRows.push(new EntityRow(this));
};
Model.prototype.RemoveRow = function (row) {
row.Parent.EntityRows.remove(row);
};
return Model;
}());
function GenerateLookupValuesArray(data) {
var itemEnumerator = data.getEnumerator();
var lookupValuesArray = [];
// Add empty row
lookupValuesArray.push(new LookupValue(0, ''));
while (itemEnumerator.moveNext()) {
var listItem = itemEnumerator.get_current();
var lookupValue = new LookupValue(listItem.get_id(), listItem.get_item('Title'));
lookupValuesArray.push(lookupValue);
}
return lookupValuesArray;
}
NewForm.GenerateLookupValuesArray = GenerateLookupValuesArray;
function Init() {
var entitiesPromise = RequestController.getItemsFromList('Entity', '<View><Query></Query></View>', 'Include(Id, Title)');
var rolesPromise = RequestController.getItemsFromList('Roles', '<View><Query></Query></View>', 'Include(Id, Title)');
$.when(entitiesPromise, rolesPromise).done(function (entityResult, roleResult) {
var model=new Model();
model.Entities = GenerateLookupValuesArray(entityResult);
model.Roles = GenerateLookupValuesArray(roleResult);
ko.applyBindings(model, $('#custom-new-form')[0]);
$('#custom-new-form').show();
});
}
NewForm.Init = Init;
})(NewForm = newUser.NewForm || (newUser.NewForm = {}));
})(newUser || (newUser = {}));
(function () {
SP.SOD.executeOrDelayUntilScriptLoaded(newUser.NewForm.Init, 'sp.js');
})();
我收到错误 Uncaught ReferenceError: Unable to process binding "value: function (){return entity }"。请注意,如果我将实体作为可观察对象放在模型中,它会很好地工作。但我需要它作为 SalesReturnRow 的一部分,以便我可以将它推入一个数组并供以后使用。
【问题讨论】:
-
选择的值必须是字符串。由于您使用的是 KO,因此您可以将其作为 observable 传递。但是函数应该被解析为什么?真的不清楚
-
我的问题是为什么我不能在 EntityRow 函数中将 select 的值作为 observable 传递,但是当我在 Model 函数中编写 this.entity=ko.observable(0) 时,它效果很好?
-
您是否打算让
foreach循环通过EntityRows来制作表格的行? -
感谢您的回复!我想要做的是有一个添加按钮,它将向我的表中添加行。我正在尝试将 observable 包装在 EntityRows 中,以便在按下添加按钮时创建另一个数组并将整行推入数组中。但我无法将它保存在 EntityRows 中。请让我知道这是否有意义
标签: javascript jquery knockout.js