2014-11-24 33 views
1

如何分配HTTP請求(GET或POST)值過濾器來過濾用於定製組件列表視圖如何設置POST或GET值到的Joomla 3.x的定製組件

我已創建自定義組件,其保持產品詳細信息和庫存詳細信息,只要庫存變低,它就會向管理員發送電子郵件,因爲我使用查詢字符串在URL鏈接中傳遞了產品和產品類別值的值。當管理員點擊電子郵件中的鏈接時,它將轉到庫存頁面並從產品列表中顯示產品詳細信息。

我面臨的,它沒有顯示結果爲URL查詢字符串值後點擊郵件鏈接的問題,它只顯示以前會話狀態值。

注意: 否則過濾器在後端和前端運行良好,並正確顯示結果。

am使用以下代碼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 } ?> 

在模型文件構造

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

在populateState功能

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

注:使用文本過濾器,使用selectListbox過濾器產品類別 產品名稱。

查詢字符串的URL是

'<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_searchfilter_productcat是過濾器的名稱,從populateState功能參考

回答

1

我已經找到了一套HTTP請求中的溶液(GET或POST )的值通過使用getUserStateFromRequest參考docs.joomla.org/How_to_use_user_state_variables

它會幫助一些面臨同樣問題的人,這裏是代碼。

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

從上述語法$請求參考指示HTTP請求(GET或POST)值

定製組件模型文件MODLE populateState具有低於代碼

$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」

0

不太清楚你的意思,但如果你不介意您在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]" ...> 
+0

感謝您答案:)我已更新我的帖子,並找到解決方案。以前的過濾器在後端和前端工作正常,只是它不響應查詢字符串值形式的電子郵件鏈接。我正在使用* getUserStateFromRequest *過濾器名稱作爲值持有者並將其傳遞到查詢字符串url中,現在所有工作都正常了。 – Anitha 2014-12-17 03:05:16

相關問題