如何在 Joomla 中动态重新加载管理表单字段

问题描述

自定义组件中,我正在开发一个包含 2 个字段的管理表单,一个用于 jobTitles 的组合框和复选框技术。这两个字段都是从数据库表中加载的。我已经

当我在组合框中选择不同的值时,checkBox 的值应从数据库中重新加载。

示例:

Job 1: Java Developer
Technologies: Java,Hibernate,Spring Boot

Job 2: C# Developer
Technologies: C#,MS sql

这些作业存储在一个表中

在我的管理表单中,我看到:

ComboBox: Java Developer
CheckBox:
Java
Hibernate
Spring Boot

当我在组合框中选择 C# Developer 时,我想获取从表中重新加载的复选框的可能值:

C#
MS sql

一个可能的解决方案可能是我在 JavaScript 中使用了一个在我更改 comboBox-value 时被触发的事件。

但我不知道如何从 JavaScript 重新加载 checkBox-values

jobTitle.PHP

<?PHP
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

JFormHelper::loadFieldClass('combo');

class JFormFieldJobTitle extends JFormFieldCombo {

    protected $type = 'JobTitle';

    public function getoptions() {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('jobtitle')->from('`#__jobs`');
        $rows = $db->setQuery($query)->loadobjectlist();
        foreach($rows as $row){
            $jobTitles[] = $row->jobtitle;
        }
        return $jobTitles;
    }

    protected function getinput()
    {
        // Get the field options.
        $options = $this->getoptions();
        $jobTitle = $this->form->getData()->toObject()->jobtitle;

        parent::getinput();
        $result = '<script>
            function logger() { console.log("Hello"); }
            function reload() { window.opener.location.reload(); }
            </script>
            <button type="button" onclick="logger()">Try it</button>';
        $result .= '<div class="comboBox input-append">';
        $result .= '<input type="text" name="jform[jobtitle]" id="jform_jobtitle" onchange=logger() ';
        $result .= 'value="' . $jobTitle . '" class="comboBox list" autocomplete="off"/>';
        $result .= '<div class="btn-group">';
        $result .= '<button type="button" class="btn dropdown-toggle">';
        $result .= '    <span class="caret"></span>';
        $result .= '</button>';
        $result .= '<ul class="dropdown-menu">';

        foreach ($options as $option) {
            $result .= '<li><a href = "#" onclick="reload()">' . $option . '</a></li>';
        }
        $result .= '</ul></div></div>';

        return $result;
    }
}

technologies.PHP

<?PHP
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

JFormHelper::loadFieldClass('checkBoxes');
JLoader::register('ActionlogsHelper',JPATH_ADMINISTRATOR . '/components/com_actionlogs/helpers/actionlogs.PHP');
class JFormFieldTechnologies extends JFormFieldCheckBoxes {

    protected $type = 'technologies';

    public function getoptions() {
        $jobTitle = $this->form->getData()->toObject()->jobtitle;
        $currentBackend = $this->form->getData()->toObject()->backend;
        $currentBackend = explode("\r\n",$currentBackend);
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('technologies')
            ->from('`#__jobs`')
            ->where($db->quoteName('jobtitle') . ' = ' . $db->quote($jobTitle));
        $backend = $db->setQuery($query)->loadRow()[0];
        $backend = explode("\r\n",$backend);

        // create the result array
        foreach ($backend as $b) {
            if (in_array($b,$currentBackend))
                $result[] = [$b,true];
            else
                $result[] = [$b,false];
        }

        return $result;
    }

    protected function getinput()
    {
        // Get the field options.
        $options = $this->getoptions();

        $html = '<fieldset id="jform_technologies" class="list checkBoxes">';
        $i = 0;
        foreach ($options as $option) {
            $backend = $option[0];
            $selected = $option[1];
            $name = 'jform_backend' . $i;
            $html .= '<label for="' . $name . '" class="checkBox">';
            if ($selected)
                $html .= '<input type="checkBox" id="' . $name . '" name="jform[technologies][]" value="' . $backend . '" checked>' . $backend . '</label>';
            else
                $html .= '<input type="checkBox" id="' . $name . '" name="jform[technologies][]" value="' . $backend . '">' . $backend . '</label>';
            $i++;
        }
        $html .= '</fieldset>';

        return $html;
    }
}

任何想法都受到高度赞赏。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)