[英]how to set POST or GET value to filter in joomla 3.x custom component
如何為自定義組件列表視圖的過濾器分配HTTP請求(GET或POST)值
我創建了一個自定義組件,用於維護產品詳細信息和庫存詳細信息,每當庫存變低時,它將向管理員發送電子郵件,因為我已使用查詢字符串在URL鏈接中傳遞了產品值和產品類別值。 當管理員單擊電子郵件中的鏈接時,它將轉到庫存頁面,並從產品列表中顯示產品詳細信息。
我面臨的問題是,單擊郵件鏈接時,它不顯示url查詢字符串值的結果,它僅顯示以前的會話狀態值。
注意:否則,篩選器在后端和前端均能正常工作,並正確顯示結果。
我正在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過濾器。
查詢字符串網址為
'<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功能參考
我已經找到了通過參考docs.joomla.org/How_to_use_user_state_variables使用getUserStateFromRequest來設置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)值
自定義組件模型文件模型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', ''));
所以在電子郵件網址中,我傳遞了查詢字符串,例如
'<a target="_blank" href="index.php?option=com_mycomponent&view=my_view&filter_search=product_name&filter_productcat=product_id">link</a>';
注意:如果HTTP請求(GET或POST)包含“ filter_productcat”,則“ getUserStateFromRequest”方法將更新用戶狀態變量。
不太清楚您的意思是什么,但是如果您不介意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]" ...>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.