We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1778
    • 659 Posts
    Hi all !

    I have a grid with a context menu (eg : when I right-click on an item in a row it shows a contextmenu)...
    The context menu items are "hard coded" is there a way to have a ’dynamic context menu’ ?

    Not sure I’m very clear on what I expect, I’ll try to explain it simply.

    In my schema I have an object Project (in a table "projects") and an object "ProjectStatus" (in a table projects_status). Each Project can have one status which can change along the time...

    In my grid "Projects" each row is a project. In my context menu I’d like to display a list of status (from the projects_status table) as menu items...

    Any idea if it is doable and how ?

    Thanks for your help
      • 28215
      • 4,149 Posts
      If you’re using MODx.grid.Grid, you have two ways of doing it:

      1. Added a JS method called ’getMenu’ that adds the menus manually.

      2. In your JSON that you return with the items in your grid, give the grid a field of ’menu’ and in your JSON, return an array of menu items with text/handler properties (handler being the JS method to run on menu item click), like so:

      $list = array();
      foreach ($users as $user) {
        $userArray = $user->toArray();
        $userArray['menu'] = array(
              array(
                  'text' => $modx->lexicon('user_update'),
                  'handler' => 'this.update',
              ),
              '-',
              array(
                  'text' => $modx->lexicon('user_remove'),
                  'handler' => 'this.remove',
              ),
        );
        $list[] = $userArray;
      }
      return $this->outputArray($list);
      


      This will allow you to define menus from PHP.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 1778
        • 659 Posts
        Hi Shaun,

        Thanks a lot for your answers...

        Shame on me, I didn’t think I could build the menu in the getMenu method... Although I did use this method before in another grid... I’m a bit tired I guess...

        Cheers