在这个页面上:http://www.kastenreus.nl/onze-kasten/我们在左侧有过滤器,我们在Verfijn uw wensen下遇到链接过滤器问题,如果你看到控制台你可以看到它发出2个请求,第一个是内容加载的内容,当它是我们实际需要的第二个内容时。
这是点击绑定事件:
jQuery(".codenegar_product_filter_wrap ul li a").live("click", function(e){
e.preventDefault();
var $this = jQuery(this);
if($this.attr("data-key") == "cat" && $this.attr("data-widget") == "cat" ){
$this.closest("li").siblings().removeClass("codenegar_applied_filter chosen");
}
$this.closest("li").toggleClass("codenegar_applied_filter chosen");
var widget = $this.attr("data-widget");
var type = $this.attr("data-type");
var key = $this.attr("data-key");
var value = $this.attr("data-value");
var new_key = widget + "_" + key; // key for url
var current_value = codenegar_get_parameter(new_key);
//current_value += "," + value;
if(current_value != "" && new_key != "cat_cat"){
current_value = current_value.split(",");
}else{
current_value =[];
}
var value_index = jQuery.inArray(value, current_value);
if(value_index > -1){ // if duplicated remove item to have toggle effect
current_value.splice(value_index, 1);
}else{
current_value.push(value);
}
//current_value = jQuery.unique(current_value);
new_value = current_value.join(",");
if(new_key == "cat_cat" && codenegar_get_parameter("cat_cat") == new_value){
new_value = ""; // toggle effect
}
codenegar_wcpf_add_parameter(widget, type, key, new_value);
});
这是codenegar_wcpf_add_parameter
的代码function codenegar_wcpf_add_parameter(widget, type, key, value){
var state = History.getState();
var current_url = state.cleanUrl;
var new_url = "";
var new_key = widget + "_" + key;
if(new_key=='orderby_orderby') new_key = 'orderby'; // use native WooCommerce ordering
new_url = codenegar_addParameter(current_url, "cnpf", "1", true); // at first
new_url = codenegar_addParameter(new_url, "cnep", "0"); // disable paging: shows first page
new_url = codenegar_addParameter(new_url, new_key, value);
// some themes bring shop at home, so make it possible to filter products at home
if((document.URL==codenegar_wcpf_config.home_url) || (document.URL==codenegar_wcpf_config.home_url+"/")){
new_url = codenegar_addParameter(new_url, "post_type", "product");
}
if(codenegar_page_title.length==0){
codenegar_page_title = jQuery("title").text();
}
History.pushState({state:1}, codenegar_page_title, new_url);
}
这是捕获状态变化并加载内容的函数:
(function(window,undefined){
// Prepare
var History = window.History; // Note: We are using a capital H instead of a lower h
if ( !History.enabled ) {
// History.js is disabled for this browser.
// This is because we can optionally choose to support HTML4 browsers or not.
return false;
}
// Bind to StateChange Event
History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
var State = History.getState(); // Note: We are using History.getState() instead of event.state
//History.log(State);
var $wrap = jQuery("span.codenegar-shop-loop-wrapper");
if($wrap.length==0){
// theme is not standard an missing WooCommerce actions/hooks
$wrap = jQuery("ul.products");
}
var height = $wrap.css("height");
$wrap.hide().html('<div id="codenegar_ajax_loader"> <img src="' + codenegar_wcpf_config.loader_img + '" class="codenegar-ajax-loader"/> </div>')
.css("height", height).fadeIn();
if (typeof wcsl_before_ajax == 'function') {
wcsl_before_ajax();
}
// now load filtered products
jQuery.get(decodeURI(State.cleanUrl), function(data) {
//console.log(data);
var $data = jQuery(data);
var shop_loop = jQuery('div.product-loop',jQuery(data));
var shop_ul = $data.find('ul.products');
if(shop_loop.length>0){
$wrap.hide().html(shop_loop).fadeIn();
}else if(shop_ul.length>0){
$wrap.hide().html(shop_ul).fadeIn();
}else{
if(codenegar_wcpf_config.display_no_products_message=='yes'){
$wrap.hide().html(codenegar_wcpf_config.no_products_message).fadeIn();
}
}
if($data.find('.codenegar-shop-loop-wrapper').children(".codenegar-shop-pagination-wrapper").length==0){
jQuery(".codenegar-shop-pagination-wrapper").hide().html($data.find(".codenegar-shop-pagination-wrapper")).fadeIn();
}
var selected_cat = 0;
var url = State.cleanUrl;
var last_parameter = url.substring(url.lastIndexOf('&') + 1);
if(last_parameter.length>0 && last_parameter.indexOf("=")>-1){
last_parameter = last_parameter.split("=");
if(last_parameter.length == 2 && last_parameter[0]== "cat_cat"){
selected_cat = last_parameter[1];
}
}
if(parseInt(selected_cat)>0 || selected_cat=="0"){
var clicked_widget = jQuery('.codenegar_product_filter_wrap ul li a[data-value='+ selected_cat +']').closest(".codenegar_product_filter_wrap");
var updated_widget = $data.find('.codenegar_product_filter_wrap ul li a[data-old-value='+ selected_cat +']').closest(".codenegar_product_filter_wrap").children();
if(updated_widget.length>0){
clicked_widget.hide().html(updated_widget).fadeIn();
}
}
// custom are update
if (typeof wcsl_on_update == 'function') {
var areas = wcsl_on_update();
var length = areas.length;
for (var i = 0; i < length; i++){
var element = areas[i];
// if(element.length==0){
// continue;
// }
var updated_area = $data.find(element);
if(updated_area.length>0){
jQuery(element).hide().html(updated_area).fadeIn();
}else{
jQuery(element).hide().html("");
}
}
}
}).done(function() {
if (typeof wcsl_ajax_done == 'function') {
wcsl_ajax_done();
}
})
.fail(function() {
if (typeof wcsl_ajax_fail == 'function') {
wcsl_ajax_fail();
}
//if(codenegar_wcpf_config.display_no_products_message=='yes'){
$wrap.hide().html(codenegar_wcpf_config.no_products_message).fadeIn();
//}
})
.always(function() {
if (typeof wcsl_after_ajax == 'function') {
wcsl_after_ajax();
}
});;
});
})(window);