2012-10-20 123 views
1

我在Joomla創建我自己的插件,我想在文章中添加一個多選列表。我也想在它默認選擇一些選項。以下是我的代碼。在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的<field>節點的默認屬性,但它只能有一個值,我也以XML <option>節點內試圖selected="selected"但它不工作。您可以在xml列表here上獲得參考。

回答

0

嘗試增加默認值的字符串是這樣的:

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

我想這就是答案的原因是因爲這是JSON和我所知道的數據被保存在JSON。而且你要逃脫「字

+0

根據[鏈接](http://www.w3schools.com/xml/xml_attributes.asp),我們不能有多個我甚至試過內元素,這並沒有工作。 – mantish

0
$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 
> 
+0

我試過這個,沒有,它沒有工作。 – mantish