gpt4 book ai didi

javascript - foreach 仅迭代数组的最后一个条目

转载 作者:行者123 更新时间:2023-11-28 19:13:07 25 4
gpt4 key购买 nike

我正在尝试使用 knockout foreach 绑定(bind)加载我的表,它可以读取我的数组中有多少项,但循环数据只是我的数组中的最后一个条目。

描绘我的问题,而不是(仅示例):

<td> 1st </td>
<td> 2nd </td>
<td> 3rd </td>

它输出:

<td> 3rd </td>
<td> 3rd </td>
<td> 3rd </td>

这是我的观点:

<table id="clientsTable" class="table table-bordered">
<thead>
<tr>
<th style="width: 10px; text-align:center">#</th>
<th>Full Name</th>
<th>Location</th>
<th>Address</th>
<th>Member Date</th>
<th>Mobile No</th>
<th>Alternate Mobile No</th>
<th style="width: 70px">Actions</th>
</tr>
</thead>
<tbody data-bind="foreach: clientsList">
<tr>
<td> <strong data-bind='text: clientID'></strong></td>
<td> <span data-bind="text: fullName"> </span></td>
<td> <span data-bind="text: locationID"> </span></td>
<td> <span data-bind="text: address"> </span></td>
<td> <span data-bind="text: memberDate"> </span></td>
<td> <span data-bind="text: mobileNo"> </span></td>
<td> <span data-bind="text: mobileNoAlternate"> </span></td>
</tr>
</tbody>
</table>

这是脚本部分:

var clientLine = function (cid, lid, fname, lname, mname, fullname, fullname2, memdate, address, mobileno, mobilenoalt, cpid) {
self.clientID = ko.observable(cid),
self.locationID = ko.observable(lid),
self.firstName = ko.observable(fname),
self.lastName = ko.observable(lname),
self.middleName = ko.observable(mname),
self.fullName = ko.observable(fullname),
self.fullName2 = ko.observable(fullname2),
self.memberDate = ko.observable(memdate),
self.address = ko.observable(address),
self.mobileNo = ko.observable(mobileno),
self.mobileNoAlternate = ko.observable(mobilenoalt)
};

function Clients(data) {
var self = this;
self.clientsList = ko.observableArray(ko.utils.arrayMap(data, function (client) {
return new clientLine(client.ClientID, client.LocationID, client.FirstName, client.LastName, client.MiddleName, client.FullName, client.FullName2, client.MemberDate, client.Address, client.MobileNo, client.MobileNoAlternate)
}));
};

var viewModel = new Clients(@Html.Raw(JsonConvert.SerializeObject(Model)));
$(document).ready(function () {
ko.applyBindings(viewModel);
});

我的 View 接收并转换我的模型,我可以看到它正确地将我的模型转换为 json 数组。如果有人能指出我的问题的原因,那将真正帮助我并非常感激。

最佳答案

您没有在 clientLine 中声明 self,因此它会进入全局范围。

要么

  • clientLine中声明self
  • 请改用this

关于javascript - foreach 仅迭代数组的最后一个条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30407728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com