1. If you update to Joomla 3.8.10, please make sure that you either do a full GitHub update of Fabrik, or disable caching for your lists (in the Advanced settings) or turn off Joomla's System Cache setting. A change in Joomla's cache code in 3.8.10 required a fix in Fabrik, which will be rolled in to the next release, but is currently only available in GitHub.
    Dismiss Notice
  2. We will be performing an update of XenForo (the forum software use on this site) this afternoon, Fri 8/17/2018. The forums will be unavailable for a (hopefully!) short time.
    Dismiss Notice

SOLVED: Change another element value based on value of dbjoin element

Discussion in 'Professional Support' started by juuser, Jan 12, 2018.

Thread Status:
Not open for further replies.
  1. juuser

    juuser Member

    Level: Community
    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: Community
    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: Community
    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 it returns empty value :(

    BR,
    Martin
     
    Last edited: Jan 18, 2018
  6. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Professional
    On the php side you are getting the wrong input. You are sending it from the JS as eventId and trying to get it in php as dokumentin_kategoria. Doesn't matter which name you use, but use the same name in both places.

    Hugh

    Sent from my HTC6545LVW using Tapatalk
     
  7. juuser

    juuser Member

    Level: Community
    Thanks Hugh,

    Works perfect!

    BR,
    Martin
     
Thread Status:
Not open for further replies.

Share This Page