We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27519
    • 275 Posts
    I managed to extend the MODExt combo box that dynamically loads a list of available templates (it’s used in Batcher, in the Resources panel).

    However I want to load data from a custom table.

    Looking at the original source of MODExt combo, am I correct to assume that the data is loaded by the PHP file pointed to by the "url" config property?
    url: MODx.config.connectors_url+'element/template.php'
    


    So, if I wanted data from another table I would need my own PHP file, retrieve the relevant objects, convert them to JSON and return them?
    As for the location of that PHP, would it be correct to place it with all the other PHP CRUD files (processors/mgr/myproject/)?

    Looking at the template.php file used by MODx combo box, its contents do not make much sense to me:
    <?php
    require_once dirname(dirname(__FILE__)).'/index.php';
    $modx->request->handleRequest(array('location' => 'element/template'));
    


    Thanks.
      MODx Revolution / MAMP / OS X
      • 27519
      • 275 Posts
      I found anso’s post about the same topic here:

      http://modxcms.com/forums/index.php/topic,54686.msg315224.html#msg315224

      My combo appears to get to my PHP snippet (I see the "loading" message pop up quickly), I see data is being retrieved but nothing shows up in the combo.

      My combo:
      Tariffs.combo.Cottage = function(config) {
          config = config || {};
          Ext.applyIf(config,{
              name: 'cottage'
              ,hiddenName: 'id'
              ,displayField: 'name'
              ,valueField: 'id'
              ,fields: ['id','name']
              ,tpl: new Ext.XTemplate('<tpl for="."><div class="x-combo-list-item"><span style="font-weight: bold">{name}</span>')
              ,url: Tariffs.config.connector_url
      		,baseParams:{
      			action: 'mgr/cottage/getcombolist'
      		}
              ,listWidth: 300
              ,allowBlank: false
          });
          Tariffs.combo.Cottage.superclass.constructor.call(this,config);
      };
      


      ... and my PHP snippet to retrieve my data:
      <?php
      $list = array();
      foreach ($cottages as $cottage) {
         $cottageArray = $cottage->toArray();
          $list[]= $cottageArray;
      }
      return $this->outputArray($list);
      


      Any clues?
        MODx Revolution / MAMP / OS X
        • 27519
        • 275 Posts
        Solved it.

        The trick is the way the JSON output is built.

        MODx.combo expects a root element named ’result’ in the JSON results:

        ...some query with a $results array ...
        $list = array('results' => array());
        foreach ($results as $result) {
            $resultArray = $result->toArray();
            $list['results'][]= $resultArray;
        }
        return $this->toJSON($list);
        


        ... and voila, a nicely filled combo box.
          MODx Revolution / MAMP / OS X
          • 28215
          • 4,149 Posts
          You could also use $this->outputArray($list,$count) where you don’t have to add the ’results’ param (it does it for you):

          $list = array();
          foreach ($results as $result) {
              $resultArray = $result->toArray();
              $list[]= $resultArray;
          }
          return $this->outputArray($list);
          


          The 2nd param, $count, is used if you’re using pagination in your processor, to tell Ext what the total count of all results is (rather than just the size of the returned array).
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 27519
            • 275 Posts
            Thanks for the info, Shaun.

            I decided to use the extended combo (the one with the pagination and refresh button) as this will allow the user to refresh contents of the combo. Without it the list is only built as the combo is initially clicked. Any changes to the underlying data will not be reflected in the combo.
              MODx Revolution / MAMP / OS X