2013-04-01 67 views
2

過去3天,我一直堅持使用joomla 2.5下拉列表開發,我不得不從數據庫中檢索數據,並在下拉菜單中顯示這些數據。提到如下:問題在joomla自定義下拉列表開發

裏面的模型文件夾我創建了一個新的模式裏面的字段文件夾並命名該文件「fieldname.php」

現在文件「模型/場/ fieldname.php」包含以下代碼:

<?php 

    defined('JPATH_BASE') or die; 

    jimport('joomla.html.html'); 
    jimport('joomla.form.formfield'); 
    jimport('joomla.form.helper'); 
    JFormHelper::loadFieldClass('list'); 

    class JFormFieldMyCompany extends JFormFieldList 
    { 

      protected $type = 'MyCompany'; 

      public function getOptions() 
      { 
        // Initialize variables. 
        $options = array(); 

        $db  = JFactory::getDbo(); 
        $query = $db->getQuery(true); 

        $query->select('id As value,name As text'); 
        $query->from('#_k2_tags AS a'); 
        $query->order('a.name'); 
        $db = $this->getDbo(); 

        // Get the options. 
        $db->setQuery($query); 

        $options = $db->loadObjectList(); 

        // Check for a database error. 
        if ($db->getErrorNum()) { 
          JError::raiseWarning(500, $db->getErrorMsg()); 
        } 
        print_r($options);exit; 
        return $options; 
      } 
    } 

之後,在我的模型過濾器r.php我添加了下面的代碼。

型號/ filter.php

<?php 
defined('_JEXEC') or die; 

jimport('joomla.application.component.modeladmin'); 

class FiltersModelFilter extends JModelAdmin 
{ 

    //Add this handy array with database fields to search in 
     protected $searchInFields = array('text','a.name'); 

//Override construct to allow filtering and ordering on our fields 
     public function __construct($config = array()) { 
       $config['filter_fields']=array_merge($this->searchInFields,array('a.name')); 
       parent::__construct($config); 
     } 

    public function getTable($type = 'Filter', $prefix = 'FiltersTable', $config = array()) 
    { 
     return JTable::getInstance($type, $prefix, $config); 
    } 




    protected function loadFormData() 
    { 
     $data = JFactory::getApplication()->getUserState('com_filters.edit.filter.data', array()); 

     if (empty($data)) { 
      $data = $this->getItem(); 
     } 

     return $data; 
    } 
    public function getForm($data = array(), $loadData = true) 
    { 
     $form = $this->loadForm('com_filters.filter', 'filter', array('control' => 'jform', 'load_data' => $loadData)); 

     return $form; 
    } 
    protected function getListQuery(){ 
       $db = JFactory::getDBO(); 
       $query = $db->getQuery(true); 

       //CHANGE THIS QUERY AS YOU NEED... 
       $query->select('id As value, name As text') 
         ->from('#_k2_tags AS a'); 




       // Filter search // Extra: Search more than one fields and for multiple words 
       $regex = str_replace(' ', '|', $this->getState('filter.search')); 
       if (!empty($regex)) { 
         $regex=' REGEXP '.$db->quote($regex); 
         $query->where('('.implode($regex.' OR ',$this->searchInFields).$regex.')'); 
       } 

       // Filter company 
       $company= $db->escape($this->getState('filter.name')); 
       if (!empty($company)) { 
         $query->where('(a.name='.$company.')'); 
       } 

       // Filter by state (published, trashed, etc.) 
       $state = $db->escape($this->getState('filter.state')); 
       if (is_numeric($state)) { 
         $query->where('a.published = ' . (int) $state); 
       } 
       elseif ($state === '') { 
         $query->where('(a.published = 0 OR a.published = 1)'); 
       } 

       //echo $db->replacePrefix((string) $query);//debug 
       return $query; 
     } 

     /** 
     * Method to auto-populate the model state. 
     * 
     * Note. Calling getState in this method will result in recursion. 
     * 
     * @since  1.6 
     */ 
     protected function populateState($ordering = null, $direction = null) 
     { 
       // Initialise variables. 
       $app = JFactory::getApplication('administrator'); 

       // Load the filter state. 
       $search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search'); 
       //Omit double (white-)spaces and set state 
       $this->setState('filter.search', preg_replace('/\s+/',' ', $search)); 

       //Filter (dropdown) state 
       $state = $this->getUserStateFromRequest($this->context.'.filter.published', 'filter_state', '', 'string'); 
       $this->setState('filter.state', $state); 

       //Filter (dropdown) company 
       $state = $this->getUserStateFromRequest($this->context.'.filter.name', 'filter_company', '', 'string'); 
       $this->setState('filter.name', $state); 

       //Takes care of states: list. limit/start/ordering/direction 
       parent::populateState('a.name', 'asc'); 
     } 

} 

內部 「查看/過濾/ view.html.php」

<?php 
defined('_JEXEC') or die; 

jimport('joomla.application.component.view'); 

class FiltersViewFilter extends JView 
{ 
    protected $item; 
    protected $form; 
    protected $state; 
    protected $sortColumn; 
    protected $sortDirection; 
    protected $searchterms; 

    public function display($tpl = null) 
    { 

     $this->item = $this->get('Item'); 
     $this->state = $this->get('State'); 
     $this->form = $this->get('Form'); 
     $this->state= $this->get('State'); 

       //Following variables used more than once 
       $this->sortColumn = $this->state->get('list.ordering'); 
       $this->sortDirection= $this->state->get('list.direction'); 
       $this->searchterms= $this->state->get('filter.search'); 
     $this->addToolbar(); 

     parent::display($tpl); 
    } 

    public function addToolbar() 
    { 
     if ($this->item->ID) { 
      JToolBarHelper::title(JText::_('Filter Title')); 
     } else { 
      JToolBarHelper::title(JText::_('Add Filter Title')); 
     } 

     JToolBarHelper::apply('filter.apply', 'JTOOLBAR_APPLY'); 
     JToolBarHelper::save('filter.save', 'JTOOLBAR_SAVE'); 
     JToolBarHelper::save2new('filter.save2new', 'JTOOLBAR_SAVE_AND_NEW'); 

     JToolBarHelper::cancel('filter.cancel'); 
    } 
} 

的意見/過濾/ TMPL /默認內。 php

<?php defined('_JEXEC') or die; 
//Get companie options 
JFormHelper::addFieldPath(JPATH_COMPONENT . '/models/fields'); 
$companies = JFormHelper::loadFieldType('MyCompany', false); 
$companyOptions=$companies->getOptions(); // works only if you set your field getOptions on public!! 
//Get companie options 
?> 
<form action="index.php?option=com_filters&amp;ID=<?php echo $this->item->ID ?>" 
    method="post" name="adminForm" class="form-validate"> 

    <fieldset id="filter-bar"> 
       <div class="filter-search fltlft"> 
         <input type="text" name="filter_search" id="filter_search" value="<?php echo $this->escape($this->searchterms); ?>" title="<?php echo JText::_('Search in Names, etc.'); ?>" /> 
         <button type="submit"> 
           <?php echo JText::_('JSEARCH_FILTER_SUBMIT'); ?> 
         </button> 
         <button type="button" onclick="document.id('filter_search').value='';this.form.submit();"> 
           <?php echo JText::_('JSEARCH_FILTER_CLEAR'); ?> 
         </button> 
       </div> 
       <div class="filter-select fltrt"> 
         <select name="filter_state" class="inputbox" onchange="this.form.submit()"> 
           <option value=""> 
             <?php echo JText::_('JOPTION_SELECT_PUBLISHED');?> 
           </option> 
           <?php echo JHtml::_('select.options', JHtml::_('jgrid.publishedOptions', array('archived'=>false)), 'value', 'text', $this->state->get('filter.published'), true);?> 
         </select> 

         <select name="filter_type" class="inputbox" onchange="this.form.submit()"> 
           <option value=""> - Select Company - </option> 
           <?php echo JHtml::_('select.options', $companyOptions, 'value', 'text', $this->state->get('filter.name'));?> 
         </select> 

       </div> 
     </fieldset> 
<div class="width-60 fltlft"> 
     <fieldset class="adminform"> 
      <ul class="adminformlist"> 
       <?php foreach ($this->form->getFieldset() as $field): ?> 
        <li><?php echo $field->label; ?> 
        <?php echo $field->input; ?></li> 
       <?php endforeach ?> 
      </ul> 

     </fieldset> 
    </div> 

    <input type="hidden" name="task" value="" /> 
    <?php echo JHtml::_('form.token'); ?> 
</form> 

請幫忙我在識別我的錯誤時需要儘快解決問題。

+0

該文件實際上叫做fieldname.php還是mycompany.php,因爲它應該是基於代碼中類的名稱的後者?此外,窗體的xml文件中是什麼概述了表單的不同輸入字段? –

回答

0

改變這個模型/場/ fieldname.php到模型/場/ mycompany.php

還改變從JFormFieldMyCompany到JFormFieldMycompany


和保護$類型= 'MyCompany的';保護$ type ='mycompany';