AJAX實現(xiàn)產(chǎn)品多條件篩選,極大的提升了用戶的瀏覽體驗,避免了因為頁面加載時間多長導(dǎo)致用戶瀏覽等待時間過長的情況發(fā)生。開發(fā)功能的實現(xiàn)效果如下圖所示:
客戶的要求是搜索的主類名和搜索條件都需要可以編輯,所以這里需要從后臺調(diào)用數(shù)據(jù)后在頁面上輸出顯示出來。
前端代碼如下所示:
{$item['category_name']}:
{$itemv['category_name']}
接下來就是把用戶勾選的條件傳到后臺查詢到相應(yīng)的數(shù)據(jù)即可,獲取勾選條件的數(shù)據(jù),這里用JQ獲取
前端代碼如下所示:
var arr = [];
$(function(){
$('.crumb .left a.close i').off('click').on('click', function () {
if ($(this).hasClass('on')) {
$(this).removeClass('on');
$(this).find('input').removeAttr('checked');
Searchpro();
} else {
$(this).addClass('on');
$(this).find('input').attr('checked', 'checked');
Searchpro();
}
})
})
獲取到對應(yīng)數(shù)據(jù)之后,調(diào)用JQ方法Searchpro(),把數(shù)據(jù)傳到后臺進(jìn)行數(shù)據(jù)查詢
前端代碼如下所示:
function Searchpro(page){
var all = $(".filter").find("input");
var arr = [];
all.each(function(){
if($(this).is(':checked')){
var id = $(this).attr('id');
arr.push(id);
}
});
var cid = arr.join(','); //arr是以&分割的字符串
if(cid==''){
var cid = 0;
}
$.ajax({
type:'post',
url: "{:U('Products/index')}",
data:{'cid':cid,'page':page},
success:function(data) {
if (data.status == 1) {
$(".fadeInRight").html(data.html);
$(".pagetion").show();
$(".pagetion").html(data.page);
}else{
$(".fadeInRight").html(data.empty);
$(".pagetion").hide();
}
}
})
}
后臺獲取前端傳過來的數(shù)據(jù)進(jìn)行處理
if (IS_AJAX) {
$cid = I('cid');
$page = I('page');
$html = '';
$map['is_show']=array('eq',1);
$map['status']=array('eq',1);
if($cid!='0'){
$pid = M('keyword')->where(array('parent_id'=>$cid))->select();
if($pid){
$cid_list = array();
foreach($pid as $k=>$v){
$cid_list[] = $v['category_id'];
}
$count1=count($cid_list);
if ($count1>1) {
foreach ($cid_list as $k1 => $v1) {
if ($k1+1==$count1) {
$mapc.='find_in_set("'.$v1.'",relax_keyword)';
}else{
$mapc.='find_in_set("'.$v1.'",relax_keyword) or ';
}
}
}else{
$mapc.='find_in_set("'.$cid_list[0].'",relax_keyword)';
}
$map['_string']=$mapc;
}else{
$cid = explode(",",$cid);
$count1=count($cid);
if ($count1>1) {
foreach ($cid as $k1 => $v1) {
if ($k1+1==$count1) {
$mapc.='find_in_set("'.$v1.'",relax_keyword)';
}else{
$mapc.='find_in_set("'.$v1.'",relax_keyword) or ';
}
}
}else{
$mapc.='find_in_set("'.$cid[0].'",relax_keyword)';
}
$map['_string']=$mapc;
}
}
// print_r($map);die;
if(empty($page)){
$page=1;
}
$listRows=9;
$order = 'order_id asc,id asc';
$pageList = 4;
$now_cool_page = $pageList/2;
$now_cool_page_ceil = ceil($now_cool_page);
$count = M('products')->where($map)->count();
$firstRow = $listRows * ($page -1);
//分頁內(nèi)容
$manuals = M('products')->where($map)->order($order)->limit($firstRow . ',' . $listRows)->select();
foreach ($manuals as $key => $v) {
$v['picture'] = unserialize($v['picture']);
$content_html .='
'.$v['title'].'
'.$v['title2'].'
'.$v['detail'].'
View Details
';
$data['status'] = 1;
}
$maxPage = ceil($count / $listRows);
$page_html = '';
if ($manuals) {
//上一頁
$up_row = $page - 1;
$page_html .= $up_row > 0 ? '': '';
for ($i=1; $i <= $pageList ; $i++) {
if (($page-$now_cool_page) <= 0) {
$num = $i;
} elseif(($page+$now_cool_page -1) >= $maxPage) {
$num = $maxPage - $pageList + $i;
} else {
$num = $page - $now_cool_page_ceil + $i;
}
if($num > 0 && $num != $page){
if($num <= $maxPage){
$page_html .= '' . $num . '';
}else{
break;
}
}else{
if($num > 0 && $maxPage != 1){
$page_html .= '' . $num . '';
}
}
}
//下一頁
$down_row = $page + 1;
$page_html .= ($down_row <= $maxPage) ? '' : '';
}else{
$data['status'] = 0;
$data['empty'] = '
No record
';
}
$data['page'] = $page_html;
$data['html'] = $content_html;
$this->ajaxReturn($data);
}
文章名稱:PHP網(wǎng)站定制之AJAX實現(xiàn)產(chǎn)品多條件篩選二
URL標(biāo)題:http://jinyejixie.com/news/239134.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、虛擬主機(jī)、電子商務(wù)、搜索引擎優(yōu)化、外貿(mào)建站、關(guān)鍵詞優(yōu)化
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源:
創(chuàng)新互聯(lián)