odoo列表视图分页默认80条记录,但可以点击切换选择200、500、2000、无限制,相关源码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
/** * Render the pager according to the ListView.pager template and add listeners on it. * Set this.$pager with the produced jQuery element * @param {jQuery} [$node] a jQuery node where the rendered pager should be inserted * $node may be undefined, in which case the ListView inserts the pager into this.options.$pager * or into a div of its template */ render_pager: function($node) { if (!this.$pager && this.options.pager) { var self = this; this.$pager = $(QWeb.render("ListView.pager", {'widget': self})); this.$pager .on('click', 'a[data-pager-action]', function () { var $this = $(this); var max_page_index = Math.ceil(self.dataset.size() / self._limit) - 1; switch ($this.data('pager-action')) { case 'first': self.page = 0; break; case 'last': self.page = max_page_index; break; case 'next': self.page += 1; break; case 'previous': self.page -= 1; break; } if (self.page < 0) { self.page = max_page_index; } else if (self.page > max_page_index) { self.page = 0; } self.reload_content(); }).find('.oe_list_pager_state') .click(function (e) { e.stopPropagation(); var $this = $(this); var $select = $('<select>') .appendTo($this.empty()) .click(function (e) {e.stopPropagation();}) .append('<option value="80">80</option>' + '<option value="200">200</option>' + '<option value="500">500</option>' + '<option value="2000">2000</option>' + '<option value="NaN">' + _t("Unlimited") + '</option>') .change(function () { var val = parseInt($select.val(), 10); self._limit = (isNaN(val) ? null : val); self.page = 0; self.reload_content(); }).blur(function() { $(this).trigger('change'); }) .val(self._limit || 'NaN'); }); this.configure_pager(this.dataset); $node = $node || this.options.$pager; if ($node) { this.$pager.appendTo($node); } else { this.$('.oe_list_pager').replaceWith(this.$pager); } } } |
如果系统运行许久积累了大量数据,特别是互联网海量应用,这个无限制就造成了一个性能上的隐患
1 2 3 4 5 6 7 8 9 10 11 12 13 |
var ListView = require('web.ListView'); ListView.include({ render_pager: function ($node) { var self = this; self._super.apply(self, arguments); if (self.$pager && self.options.pager) { self.$pager.find('.oe_list_pager_state').click(function(e){ e.stopPropagation(); $(this).find("select").children("option:last").remove(); }); } } }); |
这里通过继承渲染分页方法,在点击分页信息动态生成记录条数选择的事件上优雅的移除掉最后一个无限制选项