Joomla组件 - 列表视图管理页面 - 搜索不起作用

时间:2015-04-10 12:41:23

标签: joomla components

在我的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>

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

我希望搜索仅适用于我的数据表中的一些(2个中的7个)列。

更新

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

中的这些部分发表评论

3 个答案:

答案 0 :(得分: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);

这会将变量设置为state。 只要做到这一点,你就完成了。

<强>更新 答案应该是

$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 );


        }
    }

<强>解释

通常&#34;搜索&#34;被认为是列表视图的公共搜索字段。通常,它适用于大多数列,因此建议不要为所有列创建单独的搜索框,而是建议只保留一个搜索框,以便搜索所有列。

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

在这个if&#34; id:3&#34;被搜索,它将立即进入if部分,因为&#34; id:&#34;提到,所以if部分变为true,然后它将按id搜索(将返回id为3的行)。在正常情况下,它会自动转到else部分。

最后,这些只是标准做法。即使您只编写条件,代码也会起作用。选择是你的。

答案 1 :(得分:0)

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

例如:

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

答案 2 :(得分:0)

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

在视图上

<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

这两个隐藏的字段实际上是这样做的。

在控制器上:

现在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;
    }

正如你在这里看到的,有三个要点:

  • 发布数据
  • 在db
  • 中进行查询
  • 将结果发送到视图

此视图中的视图将位于components/com_test/views/search/view.html.php中。