update backend search js

This commit is contained in:
manson 2014-08-07 14:20:22 +08:00
parent c656720132
commit 68e0fbcd92
1 changed files with 16 additions and 10 deletions

View File

@ -46,6 +46,7 @@
var datas = window.location.search ? window.location.search.replace('?','').split('&') : [] var datas = window.location.search ? window.location.search.replace('?','').split('&') : []
datas.map(function(data){ datas.map(function(data){
tmp = data.split("="); tmp = data.split("=");
tmp[1] = decodeURIComponent(tmp[1]);
if(key){ if(key){
if(tmp[0]==key){ if(tmp[0]==key){
if(tmp[0].indexOf('[]')>=0){ if(tmp[0].indexOf('[]')>=0){
@ -80,14 +81,15 @@
searchLock = true; searchLock = true;
if(interval==0){ if(interval==0){
url = document.URL; url = document.URL;
url = url.replace('#','');
if(url.indexOf("keywords=")>=0){ if(url.indexOf("keywords=")>=0){
console.log("keywords="+params("keywords")); url = url.replace("keywords="+encodeURIComponent(params("keywords")),"keywords="+keyword)
url = url.replace("keywords="+params("keywords"),"keywords="+keyword)
}else{ }else{
url = (url.indexOf("?")>=0) ? url+'&'+"&keywords="+keyword : url+"?keywords="+keyword url = (url.indexOf("?")>=0) ? url+"&keywords="+keyword : url+"?keywords="+keyword
} }
url = url.replace("page="+params("page"),"page=1");
history.replaceState(null, null, decodeURIComponent(url)); history.pushState(null, null, url);
$.get(url,function(data){ $.get(url,function(data){
searchLock = false; searchLock = false;
$("#<%= search_dom_id %>").html(data); $("#<%= search_dom_id %>").html(data);
@ -103,7 +105,7 @@
var bindPagination = function(){ var bindPagination = function(){
$(".pagination a").click(function(){ $(".pagination a").click(function(){
filter.updateTable($(this).attr('href')); filter.updateTable($(this).attr('href'),false,true);
return false; return false;
}); });
} }
@ -117,7 +119,7 @@
var filters = makeFilters(), var filters = makeFilters(),
dom = $(dom), dom = $(dom),
mainUrl = window.location.pathname; mainUrl = window.location.pathname;
this.updateTable = function(url, goback){ this.updateTable = function(url, goback, is_pagination){
update = true; update = true;
xurl = (url == null ? ( filters.length ? mainUrl + "?" + filters.join('&') : mainUrl ) : url); xurl = (url == null ? ( filters.length ? mainUrl + "?" + filters.join('&') : mainUrl ) : url);
$.ajax({ $.ajax({
@ -125,13 +127,15 @@
type : "get", type : "get",
dataType : "html" dataType : "html"
}).done(function(data){ }).done(function(data){
if(!goback){ if(is_pagination){
history.replaceState(null, null, decodeURIComponent(xurl)); history.pushState(null, null, decodeURIComponent(xurl));
}else{
if(!goback) history.replaceState(null, null, decodeURIComponent(xurl));
} }
filters = makeFilters(); filters = makeFilters();
dom.html(data); dom.html(data);
bindPagination(); bindPagination();
update = false;
}) })
} }
this.addFilter = function(filter){ this.addFilter = function(filter){
@ -160,9 +164,11 @@
window.onpopstate = function(event){ window.onpopstate = function(event){
if(!update){ if(!update){
self.updateTable(document.location.href, true); $("#filter-input").val( $.isEmptyObject(params('keywords')) ? "" : params('keywords') );
self.updateTable(document.location, true);
$(".filter-group .btn-small").removeClass('active'); $(".filter-group .btn-small").removeClass('active');
$.each(document.location.search.split('&'),function(key,filter){ $.each(document.location.search.split('&'),function(key,filter){
if(filter.split('=')[0].indexOf("keywords")>=0) return true;
$('#filter_'+filter.split('=')[1]).addClass('active'); $('#filter_'+filter.split('=')[1]).addClass('active');
}); });
} }