【问题标题】:Multiple select list with multiple options selected in Joomla! plugin在 Joomla 中选择多个选项的多选列表!插入
【发布时间】:2012-10-20 13:53:19
【问题描述】:

我正在 Joomla 中创建自己的插件,我想在文章中添加一个多选列表。我还希望默认选择其中的一些选项。以下是我的代码。

public function onContentPrepareForm($form, $data)
    {
        if (!($form instanceof JForm))
        {
            $this->_subject->setError('JERROR_NOT_A_FORM');
            return false;
        }
        $id = $data->id;
        $catid = $data->catid;
        $db = JFactory::getDBO();
        $query = "SELECT ID, title FROM #__content WHERE catid = '" . SUBTOPIC_CATEGORY_ID . "'";
        $db->setQuery($query);
        $resultArray = $db->loadAssocList();
        $optionsString = '';
        foreach($resultArray as $result)
        {
            $optionsString .= '<option value="' . $result['ID'] . '"> ' . $result['title'] . '</option>';
        }
        if(isset($id) && isset($catid) && $catid == TOPIC_CATEGORY_ID)
        {
            $query = "SELECT subtopic FROM #__empd_topic WHERE ID = '$id'";
            $db->setQuery($query);
            $subtopicArray = $db->loadRow();
        }
        $xmlText = '<?xml version="1.0" encoding="utf-8"?>
<form>
    <fields name="subtopic" label="subtopic">
        <fieldset name="subtopic" label="subtopic">
            <field
                name="subtopic"
                type="list"
                id="subtopic"
                multiple ="true"
                label = "subtopic"
                message = "Message"
            >' . $optionsString . '</field>
        </fieldset>
    </fields>
</form>';
        $xmlObj = new SimpleXMLElement($xmlText);
        $form->setField($xmlObj);
        return true;
    }

现在 xml 中的 &lt;field&gt; 节点有一个默认属性,但它只能有一个值,我也在 xml 中的 &lt;option&gt; 节点中尝试了 selected="selected",但它不起作用。您可以在 xml 列表中获得参考 here

【问题讨论】:

    标签: joomla simplexml joomla2.5


    【解决方案1】:

    尝试在这样的字符串中添加默认值:

    default="[\"3\",\"5\",\"7\"]"
    

    我认为这是答案的原因是因为那是 JSON,我知道数据保存在 JSON 中。你必须转义 " 字符

    【讨论】:

    • 根据link,我们不能为 xml 属性设置多个值。我什至在 元素中尝试了 3,但也没有用。
    【解决方案2】:
    $selected = array("2","3","4");  //selected by default
    
    foreach($resultArray as $result)
            {
                if(in_array($result['ID'],$selected)) {
                   $select = "selected=\"selected\"";
                } else {
                   $select = "";
                 }
    
                $optionsString .= '<option value="' . $result['ID'] . '" '.$select.' > ' . $result['title'] . '</option>';
            }
    

    xml

    <field
     name="subtopic"
     type="list"
     id="subtopic"
     multiple ="true"
     label = "subtopic"
     message = "Message"
     size="5" //add this 
     >
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 2012-05-31
      • 2021-11-26
      • 2016-08-23
      • 1970-01-01
      相关资源
      最近更新 更多