ผมลองทำ localStorage โดยเรากดค้นหาสินค้า แล้วเอาค่าค้นหาเก็บใน div hidden และในไฟล์ js ผมก็ดึงค่าค้นหาเก็บใน div hidden มาเพื่อเก็บค่าใน localStorage
เพื่อทำประวัติค้นหา แต่เหมือน localStorage ไม่ขึ้นค่าค่าค้นหาให้ครับ ทำไมถึงทำค่าไม่ครับ
ผมกดค้นหาแล้ว ใน localStorage ไม่ขึ้นค่าค่าค้นหาให้
- Joomla Development-1.png (22.6 KiB) Viewed 1187 times
ไฟล์ helper.php
โค้ด: เลือกทั้งหมด
<?php
class ModMdClientHistory
{
public static function getArticleId()
{
$input = JFactory::getApplication()->input;
// var_dump($input);
if ($input->get('option') == 'com_content' && $input->get('view') == 'article')
{
return $input->get('id');
}
}
public static function getKeyword()
{
$input = JFactory::getApplication()->input;
if ($input->get('option') == 'com_search' && $input->get('view') == 'search')
{
return $input->get('searchword');
}
}
public static function getConfigData($params)
{
$arr_config = array ();
$arr_config[] = 'data-item-limit="' . $params->get('data_item_limit', 5) . '"';
return $arr_config;
}
}
ไฟล์ default.php
โค้ด: เลือกทั้งหมด
<?php if ($keyword = ModMZCHistorySearch::getKeyword()): ?>
<div class="mod-mzc-history-search-keyword hidden" id="keyword"><?php echo $keyword; ?></div>
<?php endif; ?>
ไฟล์ js
โค้ด: เลือกทั้งหมด
var s = new String(window.location.search);
if(s.substr(1).search(/start=[0-9]+/) > 0) {
return false;
}
var keyword = $('#keyword').text();
var link = window.location.href;
if(!a.hasOwnProperty(keyword)) {
a[keyword] = { 'keyword': keyword, 'link': link};
delete a[''];
localStorage.setItem("search_product_keywords", JSON.stringify(a));
} else {
a[keyword].count++;
localStorage.setItem("search_product_keywords", JSON.stringify(a));
}
if(JSON.parse(localStorage.getItem("search_product_keywords"))) {
$('.mod-mzc-history-search').append('<br /><h3 class="page-header">'+Joomla.JText._('MOD_MZC_HISTORY_SEARCH_KEYWORD_LASTEST')+'</h3>');
ul = $('<ul class="category-module"></ul>');
var limit = parseFloat($('.mod-mzc-history-search-config').attr('data-item-limit'));
var no = 0;
$.each(a, function() {
if(no == limit) {
return false;
}
var link = $('<a></a>');
link.prop('class', 'mod-product-category-title');
link.prop('href', this.link);
link.prop('title', this.keyword);
link.html(this.keyword);
ul.prepend('<li></li>');
ul.find('li:first').append(link);
no++;
});
if(typeof $('.mod-mzc-history-search').html() !== 'undefined') {
$('.mod-mzc-history-search').append(ul);
} else {
$('#history-keywords').append(ul);
}
} else if(typeof $('.mod-mzc-history-search').html() === 'undefined') {
$('#history-keywords').html($('.mzc-history-search-keyword-default').html());
}
ผมลองทำ localStorage โดยเรากดค้นหาสินค้า แล้วเอาค่าค้นหาเก็บใน div hidden และในไฟล์ js ผมก็ดึงค่าค้นหาเก็บใน div hidden มาเพื่อเก็บค่าใน localStorage
เพื่อทำประวัติค้นหา แต่เหมือน localStorage ไม่ขึ้นค่าค่าค้นหาให้ครับ ทำไมถึงทำค่าไม่ครับ
ผมกดค้นหาแล้ว ใน localStorage ไม่ขึ้นค่าค่าค้นหาให้
[attachment=0]Joomla Development-1.png[/attachment]
ไฟล์ helper.php
[code]
<?php
class ModMdClientHistory
{
public static function getArticleId()
{
$input = JFactory::getApplication()->input;
// var_dump($input);
if ($input->get('option') == 'com_content' && $input->get('view') == 'article')
{
return $input->get('id');
}
}
public static function getKeyword()
{
$input = JFactory::getApplication()->input;
if ($input->get('option') == 'com_search' && $input->get('view') == 'search')
{
return $input->get('searchword');
}
}
public static function getConfigData($params)
{
$arr_config = array ();
$arr_config[] = 'data-item-limit="' . $params->get('data_item_limit', 5) . '"';
return $arr_config;
}
}
[/code]
ไฟล์ default.php
[code]
<?php if ($keyword = ModMZCHistorySearch::getKeyword()): ?>
<div class="mod-mzc-history-search-keyword hidden" id="keyword"><?php echo $keyword; ?></div>
<?php endif; ?>
[/code]
ไฟล์ js
[code]
var s = new String(window.location.search);
if(s.substr(1).search(/start=[0-9]+/) > 0) {
return false;
}
var keyword = $('#keyword').text();
var link = window.location.href;
if(!a.hasOwnProperty(keyword)) {
a[keyword] = { 'keyword': keyword, 'link': link};
delete a[''];
localStorage.setItem("search_product_keywords", JSON.stringify(a));
} else {
a[keyword].count++;
localStorage.setItem("search_product_keywords", JSON.stringify(a));
}
if(JSON.parse(localStorage.getItem("search_product_keywords"))) {
$('.mod-mzc-history-search').append('<br /><h3 class="page-header">'+Joomla.JText._('MOD_MZC_HISTORY_SEARCH_KEYWORD_LASTEST')+'</h3>');
ul = $('<ul class="category-module"></ul>');
var limit = parseFloat($('.mod-mzc-history-search-config').attr('data-item-limit'));
var no = 0;
$.each(a, function() {
if(no == limit) {
return false;
}
var link = $('<a></a>');
link.prop('class', 'mod-product-category-title');
link.prop('href', this.link);
link.prop('title', this.keyword);
link.html(this.keyword);
ul.prepend('<li></li>');
ul.find('li:first').append(link);
no++;
});
if(typeof $('.mod-mzc-history-search').html() !== 'undefined') {
$('.mod-mzc-history-search').append(ul);
} else {
$('#history-keywords').append(ul);
}
} else if(typeof $('.mod-mzc-history-search').html() === 'undefined') {
$('#history-keywords').html($('.mzc-history-search-keyword-default').html());
}
[/code]