Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

一个字段可以由用户从预先设置的一组值中选择来设置该字段的值



  • 需求:一个字段比如货物名称,其值每次总是"主板、电池、电阻、电容、发光二极管、三极管"中的任意几个,每次用户输入嫌麻烦,要求从一个列表中进行选择。
    实现:
        1、对象定义:

    <br />&nbsp; &nbsp; &nbsp; &nbsp;  _name=&quot;goods.name&quot;<br />&nbsp; &nbsp; &nbsp; &nbsp;  _columns={&#039;name&#039;:fields.char(&#039;Name&#039;,size=32)}<br /><br />&nbsp; &nbsp; &nbsp; &nbsp;  _name=&#039;products&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp;  _columns={<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;product_name&#039;:fields.char(&#039;货物名称&#039;,size=64),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#039;goods_name&#039;:fields.many2one(&#039;goods.name&#039;,&#039;Goods Name&#039;),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br />
    



        2、视图定义:

    <br />&nbsp; &nbsp; &nbsp; &nbsp;  &lt;form&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;field name=&quot;goods_name&quot; widget=&quot;many2many&quot; invisible=&quot;1&quot;&nbsp; on_change=&quot;goods_name_changed(goods_name,product_name)&quot;/&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;field name=&quot;product_name&quot;/&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;button name=&quot;selectgoods&quot; string=&quot;选择&quot; type=&quot;object&quot; icon=&quot;gtk-execute&quot; help=&quot;选择预先设定的货物名称&quot; /&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp;  &lt;/form&gt;<br />
    



        3、change事件:

    <br />&nbsp; &nbsp; &nbsp; &nbsp;  def goods_name_changed(self, cr, uid, ids, select_ids, product_name,context=None):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  name=self.pool.get(&#039;goods.name&#039;).read(cr,uid,select_ids[0][2],&#91;&#039;name&#039;])[0]&#91;&#039;name&#039;]<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  p_name=&#91;]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  if product_name:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  p_name=product_name.split(u&#039;,&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  if name not in p_name:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  p_name.append(name)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  else:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  p_name.append(name)<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  return {&#039;value&#039;: {&#039;goods_name&#039;:False,&#039;product_name&#039;:u&#039;,&#039;.join(p_name)}}<br />
    


        4、js:(FormView)

    <br />&nbsp; &nbsp; &nbsp; on_loaded: function&nbsp; (data) {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var result = this._super(data);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.model == &#039;products&#039;) {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var self = this;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.$element.find(&#039;button&#039;).each(function () {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($(this).find(&#039;span&#039;).text() == &#039;选择&#039;) {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(this).unbind().bind(&#039;click&#039;, { obj: self }, self.search_create_popup)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;<br />&nbsp; &nbsp; &nbsp; },<br />&nbsp; &nbsp; &nbsp; &nbsp; search_create_popup: function (ev) {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var self = ev.data.obj;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.fields.goods_name.list_view.do_add_record();<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />
    



          5、效果图:

           



  • 嗯, 虽然很不理解这种需求,但是还是顶一个...



  • good

    可惜还看不懂js 部分


  • 管理员

    谢谢楼主分享。目测是6.1的?