在 Foreach 循环中使用 IF 条件来比较数据绑定值
Use IF condition in Foreach loop for comparision of data-bind values
我需要比较一个 ForEach 循环中从数据库返回的值。我不确定如何提取该值(即将其存储在变量中)并将其用于比较。
<tbody data-bind="foreach: ContractorList">
<tr>
<td>
<a href="#" data-bind="text: Contractor_Name, click: $root.listContractorDetails"></a>
<span data-bind="text:Contractor_Status"></span>
<span data-bind="if: some-syntax?? ">1</span>
<span data-bind="if: same-syntaxt??">2</span>
</td>
</tr>
</tbody>
</table>
The data-bind="text:Contractor_Status" returns "Enrolled" or "Rejected", If Enrolled I want to show span 1 else span 2. 请你能帮我吗语法。
ContractorList
中的每个承包商都应该拥有您稍后在 if
绑定中想要的全部属性。
// where 'x' is a property of each contractor:
<span data-bind="if: x == 1">1</span>
<span data-bind="if: x == 2">2</span>
可以使用 Array.prototype.forEach
在您的 视图模型 源代码中添加这些属性,如下所示:
var myViewModel = function() {
// I'm sure you're populating this from a JSON source or who knows...
this.ContractorList = ko.observableArray(); // or ko.observableArray([{}, {}, {}})...
this.ContractorList.forEach(function(contractor) {
// It's just a sample, you should set whatever you want in a property that you want
// to use in an 'if' binding
contractor.x = 1;
});
};
我在我的 HTML 文件中使用了 IF 条件而不是 .js 文件,这解决了问题。
请不要以下(以避免我犯的错误)
结束语
双等于(与 === 相比)
获取参数()
<tbody data-bind="foreach: ContractorList">
<tr>
<td>
<!-- ko if: (ContractorStatusList() == 'Enrolled')-->
<span>1</span>
<!--/ko -->
<!-- ko if: (ContractorStatusList() == 'Rejected')-->
<span>2</span>
<!--/ko -->
我需要比较一个 ForEach 循环中从数据库返回的值。我不确定如何提取该值(即将其存储在变量中)并将其用于比较。
<tbody data-bind="foreach: ContractorList">
<tr>
<td>
<a href="#" data-bind="text: Contractor_Name, click: $root.listContractorDetails"></a>
<span data-bind="text:Contractor_Status"></span>
<span data-bind="if: some-syntax?? ">1</span>
<span data-bind="if: same-syntaxt??">2</span>
</td>
</tr>
</tbody>
</table>
The data-bind="text:Contractor_Status" returns "Enrolled" or "Rejected", If Enrolled I want to show span 1 else span 2. 请你能帮我吗语法。
ContractorList
中的每个承包商都应该拥有您稍后在 if
绑定中想要的全部属性。
// where 'x' is a property of each contractor:
<span data-bind="if: x == 1">1</span>
<span data-bind="if: x == 2">2</span>
可以使用 Array.prototype.forEach
在您的 视图模型 源代码中添加这些属性,如下所示:
var myViewModel = function() {
// I'm sure you're populating this from a JSON source or who knows...
this.ContractorList = ko.observableArray(); // or ko.observableArray([{}, {}, {}})...
this.ContractorList.forEach(function(contractor) {
// It's just a sample, you should set whatever you want in a property that you want
// to use in an 'if' binding
contractor.x = 1;
});
};
我在我的 HTML 文件中使用了 IF 条件而不是 .js 文件,这解决了问题。 请不要以下(以避免我犯的错误) 结束语 双等于(与 === 相比) 获取参数()
<tbody data-bind="foreach: ContractorList">
<tr>
<td>
<!-- ko if: (ContractorStatusList() == 'Enrolled')-->
<span>1</span>
<!--/ko -->
<!-- ko if: (ContractorStatusList() == 'Rejected')-->
<span>2</span>
<!--/ko -->