I have two filters. First one is the filter by post category, it works well.
The second category filter is post by date (period = week, 2nd week) and work only on first click for this filter.
When I click on one category, I store the number of this one inside a span “data-catid”. Same for the date filter with “data-date” in the same span.
Ajax request for date filter use “data-catid” to get the category used by post category filter.
However from the second click on each date filter div, ajax request use always the same category.
That’s mean it’s no longer uses the “data-catid” only after first click.
the value wrote inside data-catid and data-date are always good !
Span “cat-id” is insert before the container refreshed by ajax request.
example : by default data-catid=”all” and data-date=”fweek”
First click (change category) : data-catid=”49″ (sport) and data-date=”fweek” (fisrt week) -> ok
Second click (change period): data-catid=”49″ and data-date=”fweek2″ (second week from “fweek”) -> ok
Third click (change category (2)): data-catid=”50″ (Culture) and data-date=”fweek2″ -> ok
Fourth click (change period (2)): data-catid=”50″ (Culture) and data-date=”fweek” -> problem = data-catid use by date filter is “49”.
After fourth click each click on period button get always category “49”.
it could be another category
I can’t find a solution
(function($){
$(document).ready(function(){
//Filtre par catégorie
$(document).on('click', '.js-filter-cat > a', function(e){
e.preventDefault();
e.stopPropagation();
var selected = $(this);
var category = $(this).data('category');
var idrangedate = $('.rangedate_selected').data('date');
$.ajax({
url: wpAjax.ajaxUrl,
data: { action:'filter_cat', category: category, idrangedate: idrangedate},
type: 'post',
beforeSend: function () {
$('.c_loader').removeClass('dnone');
$('.js-filter-cat > a').removeClass('category_selected');
},
success: function(result){
$('.c3-info').html(result);
$.getScript('https://website.fr/wp-content/themes/websiteTheme/script/publicites.js');
selected.addClass('category_selected');
if(selected.hasClass('all')){
$('#cat-id').attr('data-catid','all');
category = 'all';
alert(category);
}else{
$('#cat-id').attr('data-catid',category);
alert(category);
}
},
complete: function () {
$('.c_loader').addClass('dnone')
},
error: function(result){
console.warn(result);
}
});
});
//Filtre par date - en fonction de la catégorie choisie
$(document).on('click', '.topleftbar-options li', function(e){
e.preventDefault();
e.stopPropagation();
var selected = $(this);
var filtercategory = $('#cat-id').data('catid');
alert(filtercategory);
var idrangedate = selected.data('date');
$.ajax({
url: wpAjax.ajaxUrl,
data: { action:'filter_date', filtercategory: filtercategory, idrangedate: idrangedate },
type: 'post',
beforeSend: function () {
$('.c_loader').removeClass('dnone');
$('.topleftbar-options li').removeClass('rangedate_selected');
},
success: function(result){
//alert("error");
$('.c3-info').html(result);
$.getScript('https://website.fr/wp-content/themes/websiteTheme/script/publicites.js');
selected.addClass('rangedate_selected');
$('#cat-id').attr('data-catid',filtercategory);
$('#cat-id').attr('data-date',idrangedate);
},
complete: function () {
$('.c_loader').addClass('dnone')
},
error: function(result){
console.warn(result);
}
});
//reset();
});
});
})(jQuery);