This question has been flagged
2 Replies
10767 Views

I have been trying (for days) to pass the context from my XML view to my own JavaScript function as I need the active_id.

The JavaScript function gets data from an external source and populates the table with it, but I need to get the current active_id... Please help!!! This has been driving me insane, all I need is the active_id and nothing else. If there is a way to get this from a pre-existent variable within JavaScript, please let me know what it is.

If I need to use Python, I can, but I do not know how to actually populate my table (it can't be a field) from Python as the documentation does not actually say where returned values go...

Here's an example of my code:

<record id="my_module.example_form" model="ir.ui.view">
        <field name="name">my_module.example_form</field>
        <field name="model">res.partner</field>
        <field name="type">form</field>
        <field name="arch" type="xml">
            <form string="Example Form" version="7.0">
             <script type="text/javascript">
                 $('#My_Table').ready(function() {
                     buildAccountsTable(context);
                 });
             </script>
                <table id="My_Table">
                 ...
                </table>
           </form>
         </field>
</record>
Avatar
Discard

Hi did you succeed? I need help along the same lines

Best Answer

I encountered a similar situation while using Odoo 13. Specifically, I needed to hide the 'Create' button but retain the 'Import' button. Unfortunately, Odoo 13 does not directly support this functionality, as the 'Import' button is displayed only when the 'Create' button is visible. To work around this limitation, I implemented a solution by passing context from the view to my JS script, allowing me to dynamically determine whether the 'Create' button should be hidden.

XML:
`

            Page Name

            model.name

            tree

           

           

            current

            {                
"hide_add_button": 1

            }

           

        `

js
`odoo.define("module_name.button", function (require) {

  "use strict";


  var core = require("web.core");

  var ListController = require("web.ListController");


  var _t = core._t;


  ListController.include({

    renderButtons: function () {

      let context = this.model.get(this.handle, { raw: true }).getContext();


      this._super.apply(this, arguments);


      if (this.$buttons) {

        var addButton = this.$buttons.find(

          ".btn.btn-primary.o_list_button_add"

        );


        // add context hide_add_button : 1 in XML to hide the add button

        if (context.hide_add_button) {

          addButton.css("display", "none");

        }

      }

    },

  });

});

`


Avatar
Discard
Best Answer

Hi may you know, how get context?

Avatar
Discard