一个初学者级别的javascript问题...
我需要为我的 KOGrid 定义一个单元格模板,该模板依赖于我的 VM 中的值。如果关联字段为 True,我希望文本显示为绿色,否则显示为红色。
我有以下单元格模板:
var accountEditTemplate = `<div><input type=\"text\" data-bind=\"visible: $parent.selected(), value: $parent.entity[$data.field]" />
<span data-bind=\"visible: !$parent.selected(),
text: $parent.entity[$data.field],
css: $parent.entity.accountIsValid() === 'True' ? \'passed-validation\' : \'failed-validation\'">
</span>
</div>`;
var costCentreEditTemplate = `<div><input type=\"text\" data-bind=\"visible: $parent.selected(), value: $parent.entity[$data.field]" />
<span data-bind=\"visible: !$parent.selected(),
text: $parent.entity[$data.field],
css: $parent.entity.costCentreIsValid() === 'True' ? \'passed-validation\' : \'failed-validation\'">
</span>
</div>`;
这些在我的 columnDefs 中使用如下
self.columnDefs = [
{ width: 100, field: 'supplierNo', displayName: 'Supplier No', cellTemplate: supplierEditTemplate },
{ width: 150, field: 'invoiceNo', displayName: 'Invoice No' },
{ width: 150, field: 'costCentre', displayName: 'Cost Centre (Dim1)', cellTemplate: costCentreEditTemplate },
{ width: 200, field: 'glAccount', displayName: 'GL Account (Account)', cellTemplate: accountEditTemplate },
{ width: 100, field: 'amount', displayName: 'Amount' },
{ width: 300, field: 'invoiceDesc', displayName: 'Invoice Description' },
{ width: 150, field: 'accountIsValid', displayName: 'Valid Account' },
{ width: 150, field: 'costCentreIsValid', displayName: 'Valid Cost Centre' },
{ width: 150, field: 'supplierIsValid', displayName: 'Valid Supplier' },
];
这很好用,但我想通过一个返回单元格模板的辅助函数来减少代码重复。就像是:
function GetCellTemplate(fieldName, isValid)
{
var template = `<div><input type=\"text\" data-bind=\"visible: $parent.selected(), value: $parent.entity[fieldName]" />
<span data-bind=\"visible: !$parent.selected(),
text: $parent.entity[fieldName],
css: isValid === 'True' ? \'passed-validation\' : \'failed-validation\'">
</span>
</div>`;
switch(expression) {
case 'account':
return template
break;
default:
}
}
这可以通过以下方式调用:
var accountEditTemplate = GetCellTemplate('account', $parent.entity.accountIsValid());
我的问题是当我尝试这个时,我得到 ReferenceError:$parent is not defined
解决这种复制和粘贴代码增长的最佳方法是什么 - 我将为许多领域提供相同的功能?
根据 Jason 的建议更新了代码 - 尚未工作
function GetCellTemplate(fieldName, validationFieldName) {
var template = `<div><input type=\"text\" data-bind=\"visible: $parent.selected(), value: $parent.entity[{fieldName}]\" />
<span data-bind=\"visible: !$parent.selected(),
text: $parent.entity[{fieldName}],
css: $parent.entity.{validationFieldName} === 'True' ? \'passed-validation\' : \'failed-validation\'\">
</span>
</div>`;
}
var editBatchVm = function () {
var self = this;
var $loadingIndicator = $('#loading-indicator');
// Properties
self.recs = ko.observableArray([]);
self.selected = ko.observableArray();
var accountEditTemplate = GetCellTemplate('account', 'accountIsValid');
var costCentreEditTemplate = GetCellTemplate('costCentre', 'costCentreIsValid');
var supplierEditTemplate = GetCellTemplate('supplier', 'supplierIsValid');
self.columnDefs = [
{ width: 100, field: 'supplierNo', displayName: 'Supplier No', cellTemplate: supplierEditTemplate },
{ width: 150, field: 'invoiceNo', displayName: 'Invoice No' },
{ width: 150, field: 'costCentre', displayName: 'Cost Centre (Dim1)', cellTemplate: costCentreEditTemplate },
{ width: 200, field: 'glAccount', displayName: 'GL Account (Account)', cellTemplate: accountEditTemplate },
{ width: 100, field: 'amount', displayName: 'Amount' },
{ width: 300, field: 'invoiceDesc', displayName: 'Invoice Description' },
{ width: 150, field: 'accountIsValid', displayName: 'Valid Account' },
{ width: 150, field: 'costCentreIsValid', displayName: 'Valid Cost Centre' },
{ width: 150, field: 'supplierIsValid', displayName: 'Valid Supplier' },
];