需求:一个字段比如货物名称,其值每次总是"主板、电池、电阻、电容、发光二极管、三极管"中的任意几个,每次用户输入嫌麻烦,要求从一个列表中进行选择。
实现:
1、对象定义:
<br /> _name="goods.name"<br /> _columns={'name':fields.char('Name',size=32)}<br /><br /> _name='products'<br /> _columns={<br /> 'product_name':fields.char('货物名称',size=64),<br /> 'goods_name':fields.many2one('goods.name','Goods Name'),<br /> }<br />
2、视图定义:
<br /> <form><br /> <field name="goods_name" widget="many2many" invisible="1" on_change="goods_name_changed(goods_name,product_name)"/><br /> <field name="product_name"/><br /> <button name="selectgoods" string="选择" type="object" icon="gtk-execute" help="选择预先设定的货物名称" /><br /> </form><br />
3、change事件:
<br /> def goods_name_changed(self, cr, uid, ids, select_ids, product_name,context=None):<br /> name=self.pool.get('goods.name').read(cr,uid,select_ids[0][2],['name'])[0]['name']<br /><br /> p_name=[]<br /> if product_name:<br /> p_name=product_name.split(u',')<br /> if name not in p_name:<br /> p_name.append(name)<br /> else:<br /> p_name.append(name)<br /><br /> return {'value': {'goods_name':False,'product_name':u','.join(p_name)}}<br />
4、js:(FormView)
<br /> on_loaded: function (data) {<br /> var result = this._super(data);<br /> if (this.model == 'products') {<br /> var self = this;<br /> this.$element.find('button').each(function () {<br /> if ($(this).find('span').text() == '选择') {<br /> $(this).unbind().bind('click', { obj: self }, self.search_create_popup)<br /> }<br /> })<br /><br /> }<br /> return result;<br /> },<br /> search_create_popup: function (ev) {<br /> var self = ev.data.obj;<br /> self.fields.goods_name.list_view.do_add_record();<br /> }<br />
5、效果图: