2012-10-20 3 views
1

Joomla에서 내 자신의 플러그인을 만들고 있는데 다중 선택 목록을 기사에 추가하고 싶습니다. 또한 기본적으로 일부 옵션을 선택해야합니다. 다음은 내 코드입니다.Joomla!에서 여러 옵션을 선택한 다중 선택 목록! plugin

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

봅니다 : 그 JSON과 나는 데이터가 JSON에 저장됩니다 알고 있기 때문에

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

나는이 답이라고 생각하는 이유입니다. 그리고 당신은에 따르면 "문자

+0

을 탈출해야 [링크] (http://www.w3schools.com/xml/xml_attributes.asp) 우리는 여러 가질 수 없습니다 xml 특성에 대한 값. 심지어 요소 내에도을 시도했는데 작동하지 않았습니다. – 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

관련 문제