ODOO列表视图X对多字段显示(n 条记录)问题
考虑到性能和显示的原因,原生One2many多年来一直以条数内容简单显示于固定表格中,其中最为瑕疵的就是无相关数据时的0条记录,这个Many2many也一脉相承,不知道会不会成为洁癖爱好者们心中的痛:)
今天终于等到一个机会忍无可忍精炼代码一并解决名称显示和零记录问题:
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 |
/** * author: i@renjie.me */ var core = require('web.core'); var Model = require('web.DataModel'); var ListView = require('web.ListView'); var ColumnMany2Many = core.list_widget_registry.get('field.many2many'); ColumnMany2Many.include({ _format: function (row_data, options) { var self = this; if (_.isEmpty(row_data[this.id].value)) { return ''; }else{ row_data[this.id] = row_data[this.id + '__display']; } return self._super.apply(self, arguments); } }); core.list_widget_registry.add('field.one2many', ColumnMany2Many); ListView.List.include({ render_cell: function (record, column) { var self = this; if (column.type === 'one2many') { var value = record.get(column.id); if (value instanceof Array && !_.isEmpty(value) && !record.get(column.id + '__display')) { new Model(column.relation).call('name_get', [ value, self.dataset.get_context() ]).done(function (names) { record.set(column.id + '__display', _(names).pluck(1).join(', ')); record.set(column.id, value); }); record.set(column.id + '__display', false); } return column.format(record.toForm().data, { model: self.dataset.model, id: record.get('id') }); }else{ return self._super.apply(self, arguments); } } }); |