03.11.2022
22:01
03.11.2022 22:01:11
Задача: реализовать фильтр по заказам в ЛК клиента |
template.php часть с фильтром
<form action="/<?=$arResult["typePage"];?>/order/" name="orderFilters" id="orderFiltersForm" method="post">
<div class="row">
<div class="col-lg-2 col-md-6 mb-2" >
<div>
<input type="checkbox" id="orderStatusPaid" name="orderStatusPaid" class="orderStatus" value="y" <?=$arParams["filters"]["orderStatusPaid"]?"checked":""?>>
<label for="orderStatusPaid">Paid</label>
</div>
<div>
<input type="checkbox" id="orderStatusNotPaid" name="orderStatusNotPaid" class="orderStatus " value="y" <?=$arParams["filters"]["orderStatusNotPaid"]?"checked":""?>>
<label for="orderStatusNotPaid">Not paid</label>
</div>
</div>
<div class="col-lg-2 col-md-6 mb-2" >
<div>
<input type="checkbox" id="orderCancelled" name="orderCancelled" class="orderCancelled" value="y" <?=$arParams["filters"]["orderCancelled"]?"checked":""?>>
<label for="orderCancelled">Cancelled</label>
</div>
<div>
<input type="checkbox" id="orderNotCancelled" name="orderNotCancelled" class="orderCancelled" value="y" <?=$arParams["filters"]["orderNotCancelled"]?"checked":""?>>
<label for="orderNotCancelled">Not cancelled</label>
</div>
</div>
<div class="col-lg-3 col-md-6 mb-2">
<div>Creation date</div>
<div>
<input type="text" style="width: 85px;" id="dateCreateStart" name="dateCreateStart" class="form-control form-control-inline dateCreate" placeholder="dd.mm.yyyy" onclick="BX.calendar({node: this, field: this, bTime: false});" value="<?=$arParams["filters"]["dateCreateStart"]?>"> -
<input type="text"style="width: 85px;" id="dateCreateEnd" name="dateCreateEnd" class="form-control form-control-inline dateCreate" placeholder="dd.mm.yyyy" onclick="BX.calendar({node: this, field: this, bTime: false});" value="<?=$arParams["filters"]["dateCreateEnd"]?>">
</div>
</div>
<div class="col-lg-3 col-md-6 mb-2">
<div>Order number</div>
<div>
<input type="number" id="orderNumberStart" name="orderNumberStart" size="6" class="form-control form-control-inline orderNumber" min="1" value="<?=$arParams["filters"]["orderNumberStart"]?>"> -
<input type="number" id="orderNumberEnd" name="orderNumberEnd" size="6" class="form-control form-control-inline orderNumber" min="1" value="<?=$arParams["filters"]["orderNumberEnd"]?>">
</div>
</div>
<div class="col-lg-2 col-md-12 mt-4 mb-2 text-center">
<a href="#" class="btn btn-primary" id="setOrderFilter">Set filter</a>
<a href="/<?=$arResult["typePage"];?>/order/" class="btn btn-link">Cancel</a>
</div>
</div>
</form> |
script.js для фильтра
function GetDateFromString(dateString){
let result = false;
if(dateString){
let dateParts = dateString.split(".");
result = new Date(dateParts[2]*1,dateParts[1]-1, dateParts[0]*1);
}
return result;
}
$(document).ready(function(){
$('.orderStatus').on('change',function(e){
if($('.orderStatus:checked').length>1){
$(this).addClass("current");
$('.orderStatus:not(.current)').prop('checked',false);
$(this).removeClass("current");
}
});
$('.orderCancelled').on('change',function(e){
if($('.orderCancelled:checked').length>1){
$(this).addClass("current");
$('.orderCancelled:not(.current)').prop('checked',false);
$(this).removeClass("current");
}
});
$('.orderNumber').keydown(function(event){
var kc, num, rt = false;
kc = event.keyCode;
if(kc == 8 || ((kc > 47 && kc < 58) || (kc > 95 && kc < 106))) rt = true;
return rt;
})
.bind('blur', function(){
num = parseInt($(this).val());
num = isNaN(num) ? '' : num;
if(num && num < 0) num = num*-1;
$(this).val(num);
});
$('.orderNumber').on('change',function(){
if($(this).val()==0){
$(this).val(1);
}
});
$('#setOrderFilter').on('click',function(e){
e.preventDefault();
$('.form-control').removeClass('border-danger');
let bError = false;
if(($('#orderNumberStart').val()!='')&&($('#orderNumberEnd').val()!='')&&($('#orderNumberStart').val()>$('#orderNumberEnd').val())){
$('#orderNumberStart, #orderNumberEnd').addClass("border-danger");
bError = true;
}
let now = new Date();
let today = new Date(now.getFullYear(),now.getMonth(), now.getDate());
let dateStart = GetDateFromString($('#dateCreateStart').val());
let dateEnd = GetDateFromString($('#dateCreateEnd').val());
if(dateStart && (dateStart > today)) {
$('#dateCreateStart').addClass('border-danger');
bError = true;
}
if((dateStart && dateEnd)&&(dateStart>dateEnd)){
$('#dateCreateStart, #dateCreateEnd').addClass("border-danger");
bError = true;
}
let orderNumberStart = $('#orderNumberStart').val();
let orderNumberEnd = $('#orderNumberEnd').val();
if(orderNumberStart && orderNumberEnd && (orderNumberStart>orderNumberEnd)){
$('#orderNumberStart').addClass('border-danger');
$('#orderNumberEnd').addClass('border-danger');
bError = true;
}
if(!bError){
$('#orderFiltersForm').submit();
}
else{
console.log()
}
});
}); |
class.php часть с фильтром для getList
$this->arParams["filters"] = [
"orderStatusPaid" => $_REQUEST["orderStatusPaid"]??"",
"orderStatusNotPaid" => $_REQUEST["orderStatusNotPaid"]??"",
"orderCancelled" => $_REQUEST["orderCancelled"]??"",
"orderNotCancelled" => $_REQUEST["orderNotCancelled"]??"",
"dateCreateStart" => $_REQUEST["dateCreateStart"]??"",
"dateCreateEnd" => $_REQUEST["dateCreateEnd"]??"",
"orderNumberStart" => $_REQUEST["orderNumberStart"]??"",
"orderNumberEnd" => $_REQUEST["orderNumberEnd"]??""
];
$filterOrder = ["USER_ID" => $this->arParams["user"]->getID()];
if(!empty($_REQUEST["orderStatusPaid"]) && empty($_REQUEST["orderStatusNotPaid"])){
$filterOrder["PAYED"] = "Y";
}
elseif(empty($_REQUEST["orderStatusPaid"]) && !empty($_REQUEST["orderStatusNotPaid"])){
$filterOrder["PAYED"] = "N";
}
if(!empty($_REQUEST["orderCancelled"]) && empty($_REQUEST["orderNotCancelled"])){
$filterOrder["CANCELED"] = "Y";
}
elseif(empty($_REQUEST["orderCancelled"]) && !empty($_REQUEST["orderNotCancelled"])){
$filterOrder["CANCELED"] = "N";
}
if(!empty($_REQUEST["dateCreateStart"]) && !empty($_REQUEST["dateCreateEnd"])){
$filterOrder[] = [
"LOGIC" => "AND",
">=DATE_INSERT" => $_REQUEST["dateCreateStart"],
"<=DATE_INSERT" => $_REQUEST["dateCreateEnd"]
];
}
elseif(!empty($_REQUEST["dateCreateStart"])){
$filterOrder[">=DATE_INSERT"] = $_REQUEST["dateCreateStart"];
}
elseif(!empty($_REQUEST["dateCreateEnd"])){
$filterOrder["<=DATE_INSERT"] = $_REQUEST["dateCreateEnd"];
}
if(!empty($_REQUEST["orderNumberStart"]) && !empty($_REQUEST["orderNumberEnd"])) {
$filterOrder[] = [
"LOGIC" => "AND",
">=ID" => $_REQUEST["orderNumberStart"],
"<=ID" => $_REQUEST["orderNumberEnd"]
];
}
elseif(!empty($_REQUEST["orderNumberStart"])){
$filterOrder[">=ID"] = $_REQUEST["orderNumberStart"];
}
elseif(!empty($_REQUEST["orderNumberEnd"])){
$filterOrder["<=ID"] = $_REQUEST["orderNumberEnd"];
} |
03.11.202222:0103.11.2022 22:01:11
Если блог был полезным, можете угостить меня "чашечкой кофе" :)
Сбер по номеру телефона +7 (953) 585-13-09 Вероника.
Спасибо!