【问题标题】:Joomla component - list view admin page - search is not workingJoomla 组件 - 列表视图管理页面 - 搜索不起作用
【发布时间】:2015-06-16 04:33:41
【问题描述】:

在我的 joomla 3.x 组件中,我有列表视图管理页面,我在其中添加了一些搜索工具:

        <div class="filter-search btn-group pull-left">
            <label for="filter_search" class="element-invisible"><?php echo JText::_('JSEARCH_FILTER');?></label>
            <input type="text" name="filter_search" id="filter_search" placeholder="<?php echo JText::_('JSEARCH_FILTER'); ?>" value="<?php echo $this->escape($this->state->get('filter.search')); ?>" title="<?php echo JText::_('JSEARCH_FILTER'); ?>" />
        </div>
        <div class="btn-group pull-left">
            <button class="btn hasTooltip" type="submit" title="<?php echo JText::_('JSEARCH_FILTER_SUBMIT'); ?>"><i class="icon-search"></i></button>
            <button class="btn hasTooltip" id="clear-search-button" type="button" title="<?php echo JText::_('JSEARCH_FILTER_CLEAR'); ?>"><i class="icon-remove"></i></button>
        </div>

但显然它不起作用。我应该如何以及在哪里添加对该搜索的支持?我想我应该在模型中添加一些动作?

我希望搜索仅在我的数据表的某些列(7 列中的 2 列)上起作用。

更新:

在我的populateState 方法中的模型文件中,我有:

    // Load the filter state.
    $search = $app->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
    $this->setState('filter.search', $search);

getListQuery 方法中:

    // Filter by search in title
    $search = $this->getState('filter.search');
    if (!empty($search)) {
        if (stripos($search, 'id:') === 0) {
            $query->where('a.email = ' . $search );
        } else {
            $search = $db->Quote('%' . $db->escape($search, true) . '%');

        }
    }

更新 2:

好的,我设法让搜索为我工作。我不知道为什么一些默认的 joomla 语法破坏了我的搜索。

实际上,在我的模型文件中注释掉 getListQuery 中的一些元素并添加适当的 where 子句就可以了:

    // Filter by search in title
    $search = $this->getState('filter.search');
    if (!empty($search)) {
        //if (stripos($search, 'email:') === 0) {
            $query->where('a.email LIKE "%' . $search .'%" OR a.imie LIKE "%'.$search.'%"' );
//            } else {
//                $search = $db->Quote('%' . $db->escape($search, true) . '%');
//                
//            }
    }

所以我坚持我的赏金来解释为什么我必须从getListQuery评论这些部分

【问题讨论】:

    标签: joomla components


    【解决方案1】:

    因为它是 joomla 中的列表视图。填充列表的主要内容来自对应的模型文件。

    如果是标准的 Joomla 组件,里面会有一个名为 getListQuery() 的函数。

    继续并在那里添加代码。例如:

    $search = $this->getState('filter.search');
            if (!empty($search)) {
                if (stripos($search, 'id:') === 0) {
                    $query->where('a.id = ' . (int) substr($search, 3));
                } else {
                    $search = $db->Quote('%' . $db->escape($search, true) . '%');
    //The previous line or directly $query->where('somecolumn like ' . $db->Quote('%' . $db->escape($search, true) . '%'));
    
    
    
                }
            }
    

    在 populateState 中也添加这个

    // Load the filter state.
            $search = $app->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
            $this->setState('filter.search', $search);
    

    这会将变量设置为状态。 只要这样做,你就完成了。

    更新: 答案应该是

    $search = $this->getState('filter.search');
        if (!empty($search)) {
            if (stripos($search, 'id:') === 0) {
                $query->where('a.id = ' . (int) substr($search, 3));
            } else {
                $search = $db->Quote('%' . $db->escape($search, true) . '%');
        $query->where('a.email LIKE ' . $search .' OR a.imie LIKE '.$search );
    
    
            }
        }
    

    说明:

    通常,“搜索”被认为是列表视图的常用搜索字段。通常,它适用于大多数列,因此建议不要为所有列创建单独的搜索框,而是只保留一个用于搜索所有列的共同目的。

    现在,它还取决于用户希望搜索的内容。所以默认情况下,Joomla 用户使用上面的代码。

    这里如果搜索“id:3”,由于提到了“id:”,它会立即进入if部分,所以if部分变为真,然后它会按id搜索(这将返回带有id的行3)。一般情况下会自动转到else部分。

    最后,这些只是标准做法。即使你只写条件,代码也可以工作。选择权在你。

    【讨论】:

    • 请查看我的更新 - 我已经有了它,但仍然无法正常工作。
    • 然后执行此操作。在$search = $this-&gt;getState('filter.search'); 之后执行此var_dump($search);exit。只要让我知道这是否返回 null 以外的任何内容。当您使用它时,只需检查您的表单中是否有 2 个名称为“filter_search”的字段。
    • 我设法解决了我的问题,但仍有赏金 - 请查看我的编辑。
    • 另外一段代码$query-&gt;where('a.email = ' . $search );应该在else部分
    • 很好,但是 if 部分允许按 id 搜索,else 部分用于 id 以外的字段。如果您搜索“id:1”,它将返回 id 为 1 的行,else 部分处理所有其他情况。建议将代码写在前面我的“答案”中的代码注释中提到的else部分
    【解决方案2】:

    您必须在组件视图文件中设置表单“id”和“名称”“adminForm”。 它将解决问题。

    例如:

    <form action="index.php?option=com_test&view=test" method="post" id="adminForm" name="adminForm">
    .....
    </form>
    

    【讨论】:

      【解决方案3】:

      好的,你需要这样的东西: - 更新您查看的表单以发布到控制器 - 更新控制器以处理帖子并显示结果

      在视图中:

      <form action="<?php echo JRoute::_('index.php?option=com_test&view=search'); ?>" method="post" name="adminForm" id="adminForm">
      
          <div class="filter-search btn-group pull-left">
              <label for="filter_search" class="element-invisible"><?php echo JText::_('JSEARCH_FILTER');?></label>
              <input type="text" name="filter_search" id="filter_search" placeholder="<?php echo JText::_('JSEARCH_FILTER'); ?>" value="<?php echo $this->escape($this->state->get('filter.search')); ?>" title="<?php echo JText::_('JSEARCH_FILTER'); ?>" />
          </div>
          <div class="btn-group pull-left">
              <button class="btn hasTooltip" type="submit" title="<?php echo JText::_('JSEARCH_FILTER_SUBMIT'); ?>"><i class="icon-search"></i></button>
              <button class="btn hasTooltip" id="clear-search-button" type="button" title="<?php echo JText::_('JSEARCH_FILTER_CLEAR'); ?>"><i class="icon-remove"></i></button>
          </div>
      
          <input type="hidden" name="option" value="com_test" />
          <input type="hidden" name="task" value="search"/>
      </form>
      

      所以实际上,当您提交时,您正在发布到控制器 TestController 调用方法 search

      这 2 个隐藏字段实际上是在做这个。

      在控制器上:

      现在在components/com_test/controller.php 上,您可以添加search 方法。

      class TestController extends JControllerLegacy 
      {
          /**
           * Other methods here 
           *    .
           *    .
           *    .
           */
      
          /**
           * Pseudo code for search
           */
          public function search()
          {
              // get the data posted
              $jinput = JFactory::getApplication()->input;
              $jform  = $jinput->post->get('jform', null, null);
      
              // make a query in the db
              $db = JFactory::getDbo();
      
              // Create a new query object.
              $query = $db->getQuery(true);
              $query
                  ->select($db->quoteName(array('col_2', 'col_7')))
                  ->from($db->quoteName('#__your_table'))
                  ->where($db->quoteName('filter_search') . ' LIKE '. $db->quote($jform['filter_search']))
      
              // Reset the query using our newly populated query object.
              $db->setQuery($query);
      
              // Load the results as a list of stdClass objects (see later for more options on retrieving data).
              $results = $db->loadObjectList();
      
              // send the result to a view 
              $view   = $this->getView('search', 'html'); //get the view
              $view->assignRef('data', $results); // assign data from the model
              $view->display(); // display the view
      
              return $this;
          }
      

      正如您在此处看到的,有 3 个要点:

      • 获取发布的数据
      • 在数据库中查询
      • 将结果发送到视图

      这个例子中的视图应该是components/com_test/views/search/view.html.php

      【讨论】:

      • 如果我已经在其他视图中操作,如何添加另一个&lt;form&gt; 标签?更改表单的操作视图会破坏我的其他功能,不是吗?
      • 在同一个页面中,您可以拥有多个表单。实际上,您可以随心所欲地拥有,只需在启动另一个之前关闭一个。考虑到您必须根据您的情况修改操作。
      猜你喜欢
      • 1970-01-01
      • 2012-04-28
      • 2013-06-22
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 2020-02-21
      相关资源
      最近更新 更多