dppx9253 2014-11-24 05:31
浏览 42
已采纳

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

How to assign HTTP request (GET or POST) value to the filter for the custom component list view

I have create the custom component which maintain product details and stock details, whenever the stock become low it'll send email to admin, in that i have pass the value for product and product category value in URL link using query string. when the admin click link from the email,it will go to the stock page and show the product details from the list of product.

The problem i was facing is, it not showing result for url query string value when click the mail link, it only showing previous session state value.

Note: Otherwise filter working well in back-end and front-end,and showing the result properly.

am using below code in edit.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 } ?>

In the model file constructor

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

in populateState function

    //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', ''));

Note: Product name using textbox filter, Product category using selectListbox filter.

Query string url is

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

Found the solution, Instead of above link. Pass the email link like below

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

Note: filter_search,filter_productcat are filter name, Refer from populateState function

  • 写回答

2条回答 默认 最新

  • doucong1853 2014-11-24 07:22
    关注

    I have found the solution for set HTTP request (GET or POST) value to search filter by using getUserStateFromRequest by refer docs.joomla.org/How_to_use_user_state_variables

    It will help some one who face same Problem, Here is the code.

    /**
     * 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);
    

    Refer from the above syntax $request indicate HTTP request (GET or POST) value

    The custom component model file modle populateState have below code

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

    so in the email url i pass query string like

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

    Note: “getUserStateFromRequest” method will update the user state variable, if a HTTP request (GET or POST) contains the “filter_productcat”

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀