1
回答
act框架 怎么集成分页
终于搞明白,存储TCO原来是这样算的>>>   

@罗格林 你好,想跟你请教个问题:act框架 怎么集成分页

<无标签>
举报
zm520msj
发帖于1个月前 1回/19阅

通过使用 offset 和 limit. 下面这段控制器代码来自实际项目:

    @GetAction
    public QueryResult q(int offset, int limit, String volumeId, boolean redundantOnly) {
        MorphiaQuery<SnapshotAdaptor> q = adaptorDao.q().offset(offset).limit(limit).orderBy("startTime");
        MorphiaQuery<SnapshotAdaptor> countQ = adaptorDao.q();
        if (S.notBlank(volumeId)) {
            q.filter("volumeId", volumeId);
            countQ.filter("volumeId", volumeId);
        }
        if (redundantOnly) {
            q.filter("redundant", true);
            countQ.filter("redundant", true);
        }
        long total = countQ.count();
        long totalRedundant = adaptorDao.q("redundant", true).count();
        return new QueryResult(q.fetch(), total, totalRedundant);
    }

前端相关代码:

    <script>
        var backup
        var self = this
        self.snapshots = []
        self.total = -1
        self.page = 0
        self.pageSize = 25
        self.pageLimit = -1
        self.selectedVolumeId = false
        self.on('mount', function () {
            self.fetchSnapshots()
        })
        fetchSnapshots()
        {
            var url = '/aws/snapshot?offset=' + self.page * self.pageSize + '&limit=' + self.pageSize
            if (self.selectedVolumeId) {
                url = url + '&volumeId=' + self.selectedVolumeId
            }
            $.getJSON(url,
                function (result) {
                    self.snapshots = result.snapshots
                    self.updateTotal(result.total, result.totalRedundant)
                    self.update()
                })
        }
        updateTotal(total, totalRedundant)
        {
            self.pageLimit = Math.round(total / self.pageSize)
            self.total = total
            if (0 < total % self.pageSize) {
                self.pageLimit++
            }
            self.totalRedundant = totalRedundant
        }
        goTo(page)
        {
            if (page < 0) {
                page = 0
            }
            if (page >= self.pageLimit) {
                page = self.pageLimit
            }
            if (page !== self.page) {
                self.page = page;
                self.fetchSnapshots()
            }
        }
        goFirst()
        {
            self.goTo(0)
        }
        goPrevious()
        {
            self.goTo(self.page - 1)
        }
        goNext()
        {
            self.goTo(self.page + 1)
        }
        goLast()
        {
            self.goTo(self.pageLimit - 1)
        }
        selectVolume(event) {
            backup = $.extend({}, self)
            self.selectedVolumeId = event.item.volumeId
            self.page = 0
            self.fetchSnapshots()
        }
        unselectVolume() {
            self = $.extend(self, backup)
            self.update()
        }
    </script>

 

顶部