Skip to content

Instantly share code, notes, and snippets.

@batazo
Created June 19, 2022 17:55
Show Gist options
  • Select an option

  • Save batazo/208803591c980d2c9e3782b35c8170f8 to your computer and use it in GitHub Desktop.

Select an option

Save batazo/208803591c980d2c9e3782b35c8170f8 to your computer and use it in GitHub Desktop.
Filter Boxes
<div id="container">
<div id="sidebar">
<div id="sidebarcontent">
<div class="sidebarbox">
<div class="slider-label">
<b>ÁR SZERINT</b>
</div>
<div id="slider"></div>
</div>
<div class="sidebarbox">
<div class="slider-label lowmarginbottom">
<b>GYÁRTÓ SZERINT</b>
</div>
<div id="brandsselect">
Loading...
</div>
</div>
</div>
</div>
<div id="contentcontainer">
<h1><b>TERMÉKEK</b></h1>
<hr />
<div id="products">
LOADING...
</div>
</div>
</div>
let sliderElement = document.getElementById("slider");
const moneyFormat = wNumb({ decimals: 0, thousand: " ", suffix: " Ft" })
noUiSlider.create(sliderElement, {
start: [15000, 1000000],
connect: true,
tooltips: [
moneyFormat,
moneyFormat
],
range: {
min: 15000,
max: 1000000
}
});
slider.noUiSlider.on("change", function (values) {
//const priceRange = values.map(Number);
renderFilteredDatas()
});
function data() {
return [
{
id: "sdknin389h3dob",
name: "Samu Galaxy 5S",
descr: "Nagyon jó telefon 5'GÉ'-vel",
brand: 1,
type: 1,
price: 120000,
img: "https://i.imgur.com/VXEKj93.jpg"
},
{
id: "j nsodbn39h3d93h7",
name: "Samu Galaxy 6S",
descr: "Nagyon jó telefon 6'GÉ'-vel",
brand: 1,
type: 1,
price: 130000,
img: "https://i.imgur.com/VXEKj93.jpg"
},
{
id: "noc3hb93hd93hndh98",
name: "Samu Galaxy 7S",
descr: "Nagyon jó telefon 7'GÉ'-vel",
brand: 1,
type: 1,
price: 140000,
img: "https://i.imgur.com/VXEKj93.jpg"
},
{
id: "ohd3983o9h8d93h",
name: "Xiaomi X",
descr: "Nagyon jó telefon 5'GÉ'-vel",
brand: 2,
type: 1,
price: 120000,
img: "https://i.imgur.com/kFS5eMT.jpg"
},
{
id: "xbiubx2892b8",
name: "Xiaomi XX",
descr: "Nagyon jó telefon 6'GÉ'-vel",
brand: 2,
type: 1,
price: 130000,
img: "https://i.imgur.com/kFS5eMT.jpg"
},
{
id: "wxyiubui3b38",
name: "Xiaomi XXL",
descr: "Nagyon jó telefon 7'GÉ'-vel",
brand: 2,
type: 1,
price: 180000,
img: "https://i.imgur.com/kFS5eMT.jpg"
},
{
id: "ohd3983o9h8d93h",
name: "iPhone Gut",
descr: "Nagyon jó telefon 5'GÉ'-vel",
brand: 5,
type: 2,
price: 160000,
img: "https://i.imgur.com/Dijr5xJ.jpg"
},
{
id: "xbiubx2892b8",
name: "iPhone Besser",
descr: "Nagyon jó telefon 6'GÉ'-vel",
brand: 5,
type: 2,
price: 300000,
img: "https://i.imgur.com/Dijr5xJ.jpg"
},
{
id: "wxyiubui3b38",
name: "iPhone Gösser",
descr: "Nagyon jó telefon 7'GÉ'-vel",
brand: 5,
type: 2,
price: 500000,
img: "https://i.imgur.com/Dijr5xJ.jpg"
}
];
}
function types() {
return { 1: "Android", 2: "iOS", 3: "Other" };
}
function brands() {
return { 1: "Samsung", 2: "Xiaomi", 3: "Huawei", 4: "OnePlus", 5: "iPhone" };
}
function ProductsComponent(data) {
return data.map((data) => {
return ProductComponent(data);
})
.join("");
}
products.innerHTML = ProductsComponent(data());
function ProductComponent({ id, name, descr, brand, type, price, img }) {
return `
<div class="productbox" data-prodid="${id}">
<div class="photo">
<img src="${img}" />
</div>
<div class="specs">
<h2>${name}</h2>
${descr}<br />
${brands()[brand]}<br />
${types()[type]}<br />
${moneyFormat.to(price)}<br />
</div>
</div>
`;
}
function BrandsComponent() {
return Object.keys(brands())
.map((brand) => {
return BrandComponent(brand);
})
.join("");
}
brandsselect.innerHTML = BrandsComponent();
document.querySelectorAll(".brandcheck").forEach((brandchk) =>
brandchk.addEventListener("click", renderFilteredDatas)
);
function BrandComponent(brandkey) {
return `
<label class="chkbox-container">${brands()[brandkey]}
<input class="brandcheck" data-brand="${brandkey}" type="checkbox">
<span class="checkmark"></span>
</label>
`;
}
function getPriceRange() {
return [...document.querySelectorAll("[data-handle]")]
.map((datahandle) => datahandle.getAttribute("aria-valuenow"))
.map(Number);
}
function getCheckedBrands() {
const checkedBrands = document.querySelectorAll(".brandcheck:checked");
const brandIdStrings = [...checkedBrands].map(
(checkedBrand) => checkedBrand.dataset.brand
);
return brandIdStrings.map(Number);
}
function isBrandIdChecked(brandid){
const checkedBrandIds = getCheckedBrands();
return checkedBrandIds.length === 0? true : checkedBrandIds.includes(brandid)
}
function filteredData() {
const priceRange = getPriceRange();
const minPrice = priceRange[0];
const maxPrice = priceRange[1];
const filteredObject = data().filter(
(item) =>
isBrandIdChecked(item.brand) &&
item.price >= minPrice &&
item.price <= maxPrice
);
//console.log(filteredObject);
return filteredObject;
}
function renderFilteredDatas(){
const filteredDataString = ProductsComponent(filteredData())
const result = filteredDataString === ""? "Nem található ilyen termék...": filteredDataString
products.innerHTML = result;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/15.6.0/nouislider.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/wnumb/1.2.0/wNumb.min.js"></script>
body {
margin: 0;
font-family: sans-serif;
}
#container {
display: flex;
}
#sidebar {
width: 54vw;
min-width: 15rem;
max-width: 19rem;
}
#sidebarcontent {
width: 35vw;
min-width: 16rem;
max-width: 20rem;
position: fixed;
}
.sidebarbox {
margin: 1rem;
padding-top: 1rem;
padding-left: 3.2rem;
padding-right: 3.5rem;
padding-bottom: 1rem;
border: solid 1px #e83a21;
box-shadow: 6px 5px 10px 5px #0000004d;
}
.slider-label {
color: #e83a21;
margin-bottom: 3rem;
margin-left: -2.1rem;
width: max-content;
}
.lowmarginbottom {
margin-bottom: 1rem;
}
#contentcontainer {
margin: 1rem;
padding-top: 1rem;
padding-left: 3.2rem;
padding-right: 3.5rem;
padding-bottom: 1rem;
border: solid 1px #e83a21;
max-width: 40rem;
min-width: 14em;
width: fill-available;
width: -webkit-fill-available;
width: -moz-available;
box-shadow: 6px 5px 10px 5px #0000004d;
}
#contentcontainer hr {
border: 0;
height: 1px;
background-image: linear-gradient(to right, rgba(0, 0, 0, 0), rgb(236 21 21 / 93%), rgba(0, 0, 0, 0));
}
.productbox {
display: flex;
padding: 1rem;
}
.productbox .photo img {
width: 12rem;
height:12rem;
}
@media screen and (max-width: 600px) {
/*body {
background-color: blue;
}*/
h2 {
font-size: 1rem;
}
#container {
display: block;
}
#sidebar {
width: 100%;
min-width: 15rem;
max-width: 100%;
}
#sidebarcontent {
width: 100%;
min-width: 15rem;
max-width: 100%;
position: relative;
}
#contentcontainer {
min-width: 6rem;
padding-left: 1.2rem;
}
.productbox .photo img {
width: 8rem;
height:8rem;
}
}
/* The container */
.chkbox-container {
color: #e83a21;
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
width: max-content;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default checkbox */
.chkbox-container input {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
/* Create a custom checkbox */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
}
/* On mouse-over, add a grey background color */
.chkbox-container:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the checkbox is checked, add a blue background */
.chkbox-container input:checked ~ .checkmark {
background-color: #e83a21;
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.chkbox-container input:checked ~ .checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.chkbox-container .checkmark:after {
left: 9px;
top: 5px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
/* Functional styling;
* These styles are required for noUiSlider to function.
* You don't need to change these rules to apply your design.
*/
.noUi-target,
.noUi-target * {
-webkit-touch-callout: none;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
-webkit-user-select: none;
-ms-touch-action: none;
touch-action: none;
-ms-user-select: none;
-moz-user-select: none;
user-select: none;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.noUi-target {
position: relative;
}
.noUi-base,
.noUi-connects {
width: 100%;
height: 100%;
position: relative;
z-index: 1;
}
/* Wrapper for all connect elements.
*/
.noUi-connects {
overflow: hidden;
z-index: 0;
}
.noUi-connect,
.noUi-origin {
will-change: transform;
position: absolute;
z-index: 1;
top: 0;
right: 0;
height: 100%;
width: 100%;
-ms-transform-origin: 0 0;
-webkit-transform-origin: 0 0;
-webkit-transform-style: preserve-3d;
transform-origin: 0 0;
transform-style: flat;
}
/* Offset direction
*/
.noUi-txt-dir-rtl.noUi-horizontal .noUi-origin {
left: 0;
right: auto;
}
/* Give origins 0 height/width so they don't interfere with clicking the
* connect elements.
*/
.noUi-vertical .noUi-origin {
top: -100%;
width: 0;
}
.noUi-horizontal .noUi-origin {
height: 0;
}
.noUi-handle {
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
position: absolute;
}
.noUi-touch-area {
height: 100%;
width: 100%;
}
.noUi-state-tap .noUi-connect,
.noUi-state-tap .noUi-origin {
-webkit-transition: transform 0.3s;
transition: transform 0.3s;
}
.noUi-state-drag * {
cursor: inherit !important;
}
/* Slider size and handle placement;
*/
.noUi-horizontal {
height: 18px;
}
.noUi-horizontal .noUi-handle {
width: 34px;
height: 28px;
right: -17px;
top: -6px;
}
.noUi-vertical {
width: 18px;
}
.noUi-vertical .noUi-handle {
width: 28px;
height: 34px;
right: -6px;
bottom: -17px;
}
.noUi-txt-dir-rtl.noUi-horizontal .noUi-handle {
left: -17px;
right: auto;
}
/* Styling;
* Giving the connect element a border radius causes issues with using transform: scale
*/
.noUi-target {
background: #fafafa;
border-radius: 4px;
border: 1px solid #d3d3d3;
box-shadow: inset 0 1px 1px #f0f0f0, 0 3px 6px -5px #bbb;
}
.noUi-connects {
border-radius: 3px;
}
.noUi-connect {
background: #e83a21;
}
/* Handles and cursors;
*/
.noUi-draggable {
cursor: ew-resize;
}
.noUi-vertical .noUi-draggable {
cursor: ns-resize;
}
.noUi-handle {
border: 1px solid #d9d9d9;
border-radius: 3px;
background: #fff;
cursor: default;
box-shadow: inset 0 0 1px #fff, inset 0 1px 7px #ebebeb, 0 3px 6px -3px #bbb;
}
.noUi-active {
box-shadow: inset 0 0 1px #fff, inset 0 1px 7px #ddd, 0 3px 6px -3px #bbb;
}
/* Handle stripes;
*/
.noUi-handle:before,
.noUi-handle:after {
content: "";
display: block;
position: absolute;
height: 14px;
width: 1px;
background: #e8e7e6;
left: 14px;
top: 6px;
}
.noUi-handle:after {
left: 17px;
}
.noUi-vertical .noUi-handle:before,
.noUi-vertical .noUi-handle:after {
width: 14px;
height: 1px;
left: 6px;
top: 14px;
}
.noUi-vertical .noUi-handle:after {
top: 17px;
}
/* Disabled state;
*/
[disabled] .noUi-connect {
background: #b8b8b8;
}
[disabled].noUi-target,
[disabled].noUi-handle,
[disabled] .noUi-handle {
cursor: not-allowed;
}
/* Base;
*
*/
.noUi-pips,
.noUi-pips * {
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.noUi-pips {
position: absolute;
color: #999;
}
/* Values;
*
*/
.noUi-value {
position: absolute;
white-space: nowrap;
text-align: center;
}
.noUi-value-sub {
color: #ccc;
font-size: 10px;
}
/* Markings;
*
*/
.noUi-marker {
position: absolute;
background: #ccc;
}
.noUi-marker-sub {
background: #aaa;
}
.noUi-marker-large {
background: #aaa;
}
/* Horizontal layout;
*
*/
.noUi-pips-horizontal {
padding: 10px 0;
height: 80px;
top: 100%;
left: 0;
width: 100%;
}
.noUi-value-horizontal {
-webkit-transform: translate(-50%, 50%);
transform: translate(-50%, 50%);
}
.noUi-rtl .noUi-value-horizontal {
-webkit-transform: translate(50%, 50%);
transform: translate(50%, 50%);
}
.noUi-marker-horizontal.noUi-marker {
margin-left: -1px;
width: 2px;
height: 5px;
}
.noUi-marker-horizontal.noUi-marker-sub {
height: 10px;
}
.noUi-marker-horizontal.noUi-marker-large {
height: 15px;
}
/* Vertical layout;
*
*/
.noUi-pips-vertical {
padding: 0 10px;
height: 100%;
top: 0;
left: 100%;
}
.noUi-value-vertical {
-webkit-transform: translate(0, -50%);
transform: translate(0, -50%);
padding-left: 25px;
}
.noUi-rtl .noUi-value-vertical {
-webkit-transform: translate(0, 50%);
transform: translate(0, 50%);
}
.noUi-marker-vertical.noUi-marker {
width: 5px;
height: 2px;
margin-top: -1px;
}
.noUi-marker-vertical.noUi-marker-sub {
width: 10px;
}
.noUi-marker-vertical.noUi-marker-large {
width: 15px;
}
.noUi-tooltip {
display: block;
position: absolute;
border: 1px solid #d9d9d9;
border-radius: 3px;
background: #e83a21;
color: #fff;
padding: 5px;
text-align: center;
white-space: nowrap;
}
.noUi-horizontal .noUi-tooltip {
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
left: 50%;
bottom: 120%;
}
.noUi-vertical .noUi-tooltip {
-webkit-transform: translate(0, -50%);
transform: translate(0, -50%);
top: 50%;
right: 120%;
}
.noUi-horizontal .noUi-origin > .noUi-tooltip {
-webkit-transform: translate(50%, 0);
transform: translate(50%, 0);
left: auto;
bottom: 10px;
}
.noUi-vertical .noUi-origin > .noUi-tooltip {
-webkit-transform: translate(0, -18px);
transform: translate(0, -18px);
top: auto;
right: 28px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment