gpt4 book ai didi

javascript - 无法在 ViewModel Knockout 中执行函数

转载 作者:行者123 更新时间:2023-11-28 02:42:58 24 4
gpt4 key购买 nike

我有一个像这样的网格系统

<div class="section table_section">
<div class="section_inner" id="divTestKoRemoteGrid">
<div class="title_wrapper" data-bind="with: Grid">
<h2 data-bind="text: TotalCount">
Prices</h2>
</div>
<div class="section_content">
<div id="product_list" data-bind="with: Grid">
<!--[if !IE]>start table_wrapper<![endif]-->
<div class="table_wrapper">
<div class="table_wrapper_inner">
<table cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<th>
No.
</th>
<th>
Tier
</th>
<th>
</th>
</tr>
<!-- ko foreach: Data -->
<tr data-bind="css: { second: $index() % 2 }">
<td data-bind="text: Id" style="width: 26px;">
</td>
<td data-bind="text: Tier" style="width: 35px;">
</td>
<td style="width: 120px;">
<div class="actions_menu">
<ul>
<li><a class="edit" href="">edit</a></li>
<li><a class="delete" href="">deny</a></li>
</ul>
</div>
</td>
</tr>
<!-- /ko -->
</tbody>
</table>
</div>
</div>
<!--[if !IE]>end table_wrapper<![endif]-->
</div>
<!-- ko with: Grid -->
<div class="pagination" data-bind="with: Page">
<span class="page_no" data-bind="text: TotalCount"></span>
<ul class="pag_list" data-bind="foreach: ko.utils.range(1, TotalPage)">
<li><a href="" data-bind="click: $parent.Goto, css:{current_page: $data == $parent.CurrentPage}">
<span><span data-bind="text: $data"></span></span></a></li>
</ul>
</div>
<!-- /ko -->
</div>
</div>
</div>

和 knockout View 模型

var my = my || {};

$(function () {

my.PriceListViewModel = (function () {

var
grid = ko.observable(),
loadGrid = function () {
my.ListModel.LoadData();
grid(my.ListModel);
};
return {
Grid: grid,
LoadGrid: loadGrid
};
})();

my.ListModel = (function () {
var
data = ko.observableArray([]),
totalCount = ko.observable(),
page = ko.observable(),
goTo = function (d) {
$.getJSON("/prices/GetByFilterViaJSON?limit=6&page=" + d, function (list) {
data(list.Data);
page(list.Page);
});
},
loadData = function () {
$.ajax({
type: 'GET',
url: '/prices/GetByFilterViaJSON',
dataType: 'json',
success: function (list) {
data(list.Data);
page(list.Page);
totalCount(list.TotalCount);
},
data: { limit: 6 },
async: false
});
};
return {
Data: data,
TotalCount: totalCount,
Page: page,
GoTo: goTo,
LoadData: loadData
};
})();

my.PageModel = (function () {
var
currentPage = ko.observable(1),
totalCount = ko.observable(1),
limit = ko.observable(),
start = ko.observable(),
nextPage = ko.observable(1),
previousPage = ko.observable(1),
totalPage = ko.observable(1),
loadData = function (pageData) {
currentPage(pageData.CurrentPage);
totalCount(pageData.TotalCount);
limit(pageData.Limit);
start(pageData.Start);
nextPage(pageData.NextPage);
previousPage(pageData.PreviousPage);
totalPage(pageData.TotalPage);
};
return {
CurrentPage: currentPage,
TotalCount: totalCount,
Limit: limit,
Start: start,
NextPage: nextPage,
PreviousPage: previousPage,
TotalPage: totalPage,
LoadData: loadData
};

})();

my.PriceListViewModel.LoadGrid();
ko.applyBindings(my.PriceListViewModel);
});

当我尝试执行GoTo函数(在ListModel中)时,浏览器无法找到该函数,并且knockout无法在点击事件中绑定(bind)。

我尝试了 5 天来解决这个问题。您认为问题是什么?

最佳答案

问题是您正在访问父级。这将为您提供页面,而不是您需要的网格。要解决此问题,请使用 parents[]。它包含当前可用的所有父级,在您的例子中,您将需要使用 parents[1]

我有一个样本 fiddle http://jsfiddle.net/Rynan/fthFE/

另请参阅 knockout 网站 http://knockoutjs.com/documentation/binding-context.html 上的文档

关于javascript - 无法在 ViewModel Knockout 中执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12384377/

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