按价格对产品排序JS + Laravel

时间:2019-04-22 08:46:28

标签: laravel-5.2

我正在尝试按价格对我的在线商店中的产品进行排序。我不明白问题出在哪里...它无法正常工作。

PRODUCTS.BLADE.PHP

<div class="sortProdText">
                <button id="sort-asc">Sort By Low Price </button>
                <button id="sort-desc">Sort By High Price </button>
            </div>

这些是产品视图:

<div class="row" id="sortBy">
           @foreach ($products as $data)
           <!-- Single Product -->
           <div class="col-12 col-sm-6 col-lg-4" data-price='{{$data['price']}}' data-name='{{$data['description']}}'>
               <div class="product-wrapper">
                   <a href="{{url('shop').'/'.$categorie_url.'/'.$data['product_url']}}">
                       <div class="single-product-wrapper" >
                           <!-- Product Image -->
                           <div class="product-img custom" >
                               <img src="{{url('img/'.$data['img'])}}" alt="">
                               <!-- Hover Thumb -->

                               <!-- Product Badge -->
                               <div class="product-badge offer-badge">
                                   <span>-30%</span>
                               </div>
                               <!-- Favourite -->
                               <div class="product-favourite">
                                   <a href="#" class="favme fa fa-heart"></a>
                               </div>
                           </div>
                           <!-- Add to Cart -->
                           <div class="add-to-cart">
                               <a href="#" class="btn btn-light add-to-cart-btn" data-id="{{$data['id']}}">Add to Cart</a>                      
                               <a href="{{url('shop').'/'.$categorie_url.'/'.$data['product_url']}}" class="btn btn-secondary more_details_btn">More Details</a>
                           </div>

                           <!-- Product Description -->
                           <div class="product-description custom" >
                               <span>{{$data['description']}}</span>
                               <p class="product-price" ><span class="old-price">$800.00</span>{{$data['price']}}$</p>
                           </div>
                       </div> 
                   </a>
               </div>
           </div>
           @endforeach
       </div>

JS代码:我认为这里是replacedNode的问题。

document.querySelector('#sort-asc').onclick = mySort;
document.querySelector('#sort-desc').onclick = mySortDesc;

function mySort() {
    let nav = document.querySelector('#sortBy');
    for (let i = 0; i < nav.children.length; i++) {
        for (let j = i; j < nav.children.length; j++) {
            if (+nav.children[i].getAttribute('data-price') > +nav.children[j].getAttribute('data-price')) {
                replacedNode = nav.replaceChild(nav.children[j], nav.children[i]);
                insertAfter(replacedNode, nav.children[i]);
            }
        }
    }
}
function mySortDesc() {
    let nav = document.querySelector('#sortBy');
    for (let i = 0; i < nav.children.length; i++) {
        for (let j = i; j < nav.children.length; j++) {
            if (+nav.children[i].getAttribute('data-price') < +nav.children[j].getAttribute('data-price')) {
                replacedNode = nav.replaceChild(nav.children[j], nav.children[i]);
                insertAfter(replacedNode, nav.children[i]);
            }
        }
    }
}

0 个答案:

没有答案