Change another element value based on value of dbjoin element

Discussion in 'Professional Support' started by juuser, Jan 12, 2018 at 8:07 AM.

  1. juuser

    juuser Member

    Level: Professional
    Hi,

    I have a scenario like this:

    Table Categories
    elements: category_id, default_revision_time

    Table Documents
    elements: Categories.category_id, revision_time

    If I change the databasejoin element (Categories.category_id) value on form Documents, I would need to update the "revision_time" element to "default_revision_time" from table Categories according to that particular document category.

    I could retrieve this value easily with Calc element, but that doesn't allow user to select the value which is different from default.

    With element Javascript I'm not capable of retrieving the needed value from Categories table.

    Thanks in advance!
    Martin
     
  2. troester

    troester Well-Known Member Staff Member

    Level: Standard
  3. juuser

    juuser Member

    Level: Professional
    Thanks Troester, but I think that doesn't help in my case. I can get the default value from other table to the selection of my cascadingdropdown element, but I also need the user to be able to select something else than default value.

    BR,
    Martin
     
  4. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    You'll probably need to roll your own AJAX code, using the user_ajax.php method.

    Assuming revision_time is a field element ...

    In this example, I'm getting the start date from an events table, and inserting it into a field element (fab_main_test___name), triggering it on a selection from the Events join dropdown ...

    Create a ./components/com_fabrik/user_ajax.php file, like this:

    Code (Text):

    class UserAjax
    {
        /**
        * This is the method that is run. You should echo out the result you which to return to the browser
        *
        * @return  void
        */

        public function getEventDate()
        {
            $db = FabrikWorker::getDbo();
            $query = $db->getQuery(true);
            $app = JFactory::getApplication();
            $input = $app->input;
            $eventId = $input->get('eventId', '');
            $query->select('start')->from('fab_events')->where('id = ' . $db->quote($eventId));
            $db->setQuery($query, 1, 0);
            $ret = $db->loadResult();
            echo $ret;
        }
    }
     
    ... and on my Event dropdown join element, I have a 'change' JS event that does this:

    Code (Text):

    jQuery.ajax({
      url: 'index.php?option=com_fabrik&format=raw&task=plugin.userAjax&method=getEventDate&eventId=' + this.getValue(),
      method: 'get',
      context: this
    }).done(function (r) {
      /*debugger;*/
      this.form.formElements.get('fab_main_test___name').update(r);
    });
     
    Here's it working:

    https://www.screencast.com/t/NiZ6crIc9s

    You'll obviously need to change the table / field / element names to suit.

    -- hugh
     
  5. juuser

    juuser Member

    Level: Professional
    Thanks, but I'm still struggling with this.

    The working code that I use to retrieve the default revision time in calc element is:
    PHP:
    $db =& JFactory::getDBO();

    $query = "SELECT a.rev_taajuus FROM prefix_laatudok_kategoriat AS a
    WHERE a.id = '{prefix_laatudok_rekisteri___dokumentin_kategoria_raw}'"
    ;

    $db->setQuery($query);
    return $db->loadResult();

    My user_ajax.php file looks like this:

    PHP:
    <?php
    class UserAjax
    {
       public function getEventDate()
        {
            $db = FabrikWorker::getDbo();
            $query = $db->getQuery(true);
            $app = JFactory::getApplication();
            $input = $app->input;
            $eventId = $input->get('dokumentin_kategoria', '');
            $query->select('rev_taajuus')->from('prefix_laatudok_kategoriat')->where('id = ' . $db->quote($eventId));
            $db->setQuery($query, 1, 0);
            $ret = $db->loadResult();
            echo $ret;
        }
    }
    ?>
    and main table category element Javscript onChange like this:

    Code (Javascript):
    jQuery.ajax({
      url: 'index.php?option=com_fabrik&format=raw&task=plugin.userAjax&method=getEventDate&eventId=' + this.getValue(),
      method: 'get',
      context: this
    }).done(function (r) {
      this.form.formElements.get('prefix_laatudok_rekisteri___field_element_to_get_updated').update(r);
    });
     
    But for some reason its not triggering :(

    BR,
    Martin
     

Share This Page