如何在 joomla 3.x 自定义组件中设置 POST 或 GET 值以过滤


how to set POST or GET value to filter in joomla 3.x custom component

如何将 HTTP 请求(GET 或 POST)值分配给自定义组件列表视图的过滤器

我已经创建了维护产品详细信息和库存详细信息的自定义组件,每当库存变低时,它都会向管理员发送电子邮件,因为我已使用查询字符串在URL链接中传递了产品和产品类别值的值。 当管理员单击电子邮件中的链接时,它将转到库存页面并显示产品列表中的产品详细信息。

我面临的问题是,单击邮件链接时,它不显示url查询字符串值的结果,它只显示以前的会话状态值。

注意: 否则过滤器在后端和前端运行良好,并正确显示结果。

我在编辑.php中使用以下代码。

    $jInput = JFactory::getApplication()->input;
      // From GET
     $qid = $jInput->get->get( 'qid', 0, 'INT' );
     $qcatid = $jInput->get->get( 'qcatid', 0, 'INT' );
     if(!empty($qid)){
     //$proname="id:".$qid;
     $proname =$model->getArticleDetails('name',$qid);
     $this->state->set('filter.search',$proname);
     if(!empty($qcatid))
     $this->state->set('filter.productcat',$qcatid); ?>
     <script type="text/javascript">
     jQuery(document).ready(function($) {
     document.id('filter_search').value='<?php echo $proname; ?>';
     document.id('productcatselect').value='<?php echo $qcatid; ?>';
     document.forms["adminForm"].submit();
     }
     </script>
    <?php } ?>

在模型文件构造函数

public function __construct($config = array())
{
    if (empty($config['filter_fields'])) {
        $config['filter_fields'] = array(
            'product', 'a.fk_product_code',
            'productcat', 'a.fk_productcat',
        );
    }
    parent::__construct($config);
}

在填充状态函数中

    //Filtering productname
    $search =trim($this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '', 'string'));
    $this->setState('filter.search', $search);
    //Filtering productcat
    $this->setState('filter.productcat', $app->getUserStateFromRequest($this->context.'.filter.productcat', 'filter_productcat', ''));

注意: 使用文本框筛选器的产品名称,使用选择列表框筛选器的产品类别。

查询字符串网址为

'<a target="_blank" href="index.php?option=com_mycomponent&view=my_view&qid=product_id&qcatid=product_id">link</a>'

找到了解决方案,而不是上面的链接。传递如下所示的电子邮件链接

<a target="_blank" href="index.php?option=com_mycomponent&view=my_view&filter_search=product_name&filter_productcat=product_id">link</a>

注意: filter_search,filter_productcat是过滤器名称,引用自 populateState 函数

我已经找到了使用 getUserStateFromRequest 通过引用 docs.joomla.org/How_to_use_user_state_variables 设置 HTTP 请求(GET 或 POST)值以搜索过滤器的解决方案

它将帮助一些面临相同问题的人,这是代码。

/**
 * Gets the value of a user state variable and sets it in the session
 *
 * This is the same as the method in JApplication except that this also can optionally
 * force you back to the first page when a filter has changed
 *
 * @param   string   $key        The key of the user state variable.
 * @param   string   $request    The name of the variable passed in a request.
 * @param   string   $default    The default value for the variable if not found. Optional.
 * @param   string   $type       Filter for the variable, for valid values see {@link JFilterInput::clean()}. Optional.
 * @param   boolean  $resetPage  If true, the limitstart in request is set to zero
 *
 * @return  The request user state.
 *
 * @since   12.2
 */
public function getUserStateFromRequest($key, $request, $default = null, $type = 'none', $resetPage = true);

参考上述语法$request指示 HTTP 请求(GET 或 POST)值

自定义组件模型文件modle popustate具有以下代码

    $search =trim($this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search', '', 'string'));
    $this->setState('filter.search', $search);
    //Filtering productcat
    $this->setState('filter.productcat', $app->getUserStateFromRequest($this->context.'.filter.productcat', 'filter_productcat', ''));

所以在电子邮件 URL 中,我传递查询字符串,例如

 '<a target="_blank" href="index.php?option=com_mycomponent&view=my_view&filter_search=product_name&filter_productcat=product_id">link</a>';

注意:"getUserStateFromRequest"方法将更新用户状态变量,如果HTTP请求(GET或POST)包含"filter_productcat"

不太确定你的意思,但如果你不介意 POST 和 GET 数组中的过滤器参数,例如 ?filter[productcat]=10 ,那么在您的列表模型中,您几乎不需要执行任何操作,因为所有过滤器都会自动填充(并为用户记住)。

构造 函数:

public function __construct($config = array())
{
    //Allow filtering (en ordering)
    $config['filter_fields']=array('search','productcat');
    parent::__construct($config);
}

getListQuery:

protected function getListQuery()
{       
    ...//some code
    $search     = $this->getState('filter.search');
    $productcat = $this->getState('filter.productcat');
    ...//Some query code
    // Filter search
    if (!empty($productcat)) {
        $query->where('productcat='.$productcat);
    }
    return $query;
}

链接或表单:确保使用数组:

<a href="<?php echo JRoute::_('index.php?option=com_custom&filter[productcat]=8');?>">
//or
<input ... name="filter[productcat]" ...>