gpt4 book ai didi

knockout.js - 通过分页改进 CodeCamper

转载 作者:行者123 更新时间:2023-12-02 22:08:02 25 4
gpt4 key购买 nike

我一直在观看 John Papa 在 Pluralsight 上关于构建 SPA 的精彩视频。

现在,我正在尝试为 session 页面实现分页,但我不确定我所做的是否是正确的方法。如果有人尝试过类似的事情,我将不胜感激。

这是我所做的。

session .html


  <section>
<footer>
<ul class="pager">
<li>
<button class="btn btn-info btn-small" data-bind="click: previous, enable:canPrev"><i class="icon-angle-left"></i>Previous</button>
</li>
<li>
<button class="btn btn-info btn-small" data-bind="click: next, enable: canPrev">Next<i class="icon-angle-right"></i></button>

</li>
</ul>
</footer>
</section>

session .js


   var  currentPage = ko.observable(), 

activate = function () {
currentPage(0);
return datacontext.getSessionPartials(sessions);
},

previous = function () {
currentPage(currentPage() - 1);
return datacontext.getSessionPartials(sessions, true, currentPage());
},

canPrev = ko.computed(function () {
return currentPage() > 0;
}),

canNext = ko.computed(function () {
//return currentPage > 0;
}),

next = function () {
currentPage(currentPage() +1);
return datacontext.getSessionPartials(sessions, true, currentPage());
},

var vm = {
//existing stuff plus the following:
previous: previous,
next: next,
currentPage: currentPage,
canPrev: canPrev,
canNext: canNext,
};

datacontext.js


 var query = EntityQuery.from('Sessions')
.select('id, title, code, speakerId, trackId, timeSlotId, roomId, level, tags')
.skip(currentPage * 5).take(5)
.orderBy('timeSlotId, level, speaker.firstName')
.inlineCount(true);

这行得通,除了 canNext,因为我不知道如何将 inlineCount 的结果添加到 session viewModel。什么是最好的方法?

最佳答案

要获取 inlineCount,请在 datacontext.js 中添加此行

   var getSessionPartials = function (sessionsObservable, totalpages,totalrecords, currentPage) {

var query = EnityQuery.from('Sessions')
.select('id, title, code, speakerId, trackId, timeSlotId, roomId, level, tags')
.skip(currentPage * 5).take(5)
.where("title","startsWith","A")
.orderBy('timeSlotId, level, speaker.firstName')
.inlineCount(true);


return manager.executeQuery(query)
.then(querySucceeded)
.fail(queryFailed);

function querySucceeded(data) {
var list = partialMapper.mapDtosToEntities(
manager, data.results, entityNames.session, 'id'
);

if (sessionsObservable) {
sessionsObservable(list);
totalpages(Math.ceil(data.inlineCount / 10));
totalrecords(data.inlineCount);
}

log('Retireved Sessions from remote datasource', data, true);
}
};

在session.js中添加

var currentPage = ko.observable();
var totalPages = ko.observable();
var totalrecords = ko.observable();


canNext = ko.computed(function () {
return currentPage() < totalPages();
//return true;
});

关于knockout.js - 通过分页改进 CodeCamper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15828919/

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