点击下拉菜单,按价格排序

问题描述 投票:0回答:1

我有一个下拉菜单和属性列表。下拉菜单包含两个选项,从低到高和从高到低。如果任何用户点击任何一个下拉项目,列出的房产应该按其价格排序。我如何使用javascript实现这个功能?

属性.html

                <div class="col-sm-6">
                  <div class="pxp-sort-form form-inline float-right">
                      <div class="form-group">
                          <select class="type-regular custom-select" id="pxp-sort-results" name="price-sorting">
                              <option value="" selected="selected disabled">Default Sort</option>
                              <option class="price-sorting" value="l2h" id="l2h">Price (Lo-Hi)</option>
                              <option class="price-sorting" value="h2l">Price (Hi-Lo)</option>

                          </select>
                      </div>

                  </div>
              </div>

            <div class="row products-grid">
              {% for item in properties.all %}
              <div class="col-sm-12 col-md-6 col-xxxl-4 product">
                  <a href="{% pageurl item %}" class="pxp-results-card-1 rounded-lg" data-price="{{ item.price }}" data-prop="1">
                      <div id="property-{{item.id}}" class="carousel slide" data-ride="carousel" data-interval="false">
                          <div class="carousel-inner">
                              {% for j in item.prop_images.all %}
                                {% image j.prop_img original as property_img %}
                                <div class="carousel-item {% if forloop.first %} active {% endif %}" style="background-image: url('{{property_img.url}}')"></div>
                              {% endfor %}
                          </div>
                          <span class="carousel-control-prev" data-href="#{{item.prop_name}}" data-slide="prev">
                              <span class="fa fa-angle-left" aria-hidden="true"></span>
                          </span>
                          <span class="carousel-control-next" data-href="#property-{{item.id}}" data-slide="next">
                              <span class="fa fa-angle-right" aria-hidden="true"></span>
                          </span>
                      </div>
                      <div class="pxp-results-card-1-gradient"></div>
                      <div class="pxp-results-card-1-details" id="prop-dtls">
                          <div class="pxp-results-card-1-details-title">{{item.prop_name}}</div>
                          <div class="pxp-results-card-1-details-price price">{{item.price}}</div>
                      </div>
                      <div class="pxp-results-card-1-features">
                          <span>{{item.bedroom}} BD <span>|</span> {{item.bathroom}} BA <span>|</span> {{item.sqft}} SF</span>
                      </div>
                      <div class="pxp-results-card-1-save"><span class="fa fa-star-o"></span></div>
                  </a>
              </div>
              {% endfor %}

          </div>

值是来自后台动态的。

javascript django wagtail
1个回答
1
投票

你可以用Javascript(重新排序DOM元素)或从服务器得到的响应来实现。

你可以使用Javascript(重新排序DOM元素)或者使用你从服务器上得到的响应。

function reverseChildren(parent) {
    for (var i = 1; i < parent.childNodes.length; i++){
        parent.insertBefore(parent.childNodes[i], parent.firstChild);
    }
}

你可以设置添加一个 onclick JS事件处理程序对父DIV的元素进行反转所有子元素。

来源于:/stackoverflow.coma378606573345051 https:/stackoverflow.coma378606573345051

您可以使用下面的函数从服务器发送响应。.order_by() 过滤方法,并在请求中加入一个标志,以确定是否为反向。

例如

非逆向 - Item.objects.all().order_by('price')

逆转 - Item.objects.all().order_by('-price')

class ItemView(View):
    def get(self, request, *args, **kwargs):
        isReversed = '-price' if request.GET['reverse'] is True else 'price'
        items = Item.objects.all().order_by(isReversed)
        return JsonResponse(items)

0
投票

下面的代码按照我的要求完美地工作了。

$(document).on("change", ".price-sorting", function() {

var sortingMethod = $(this).val();

if(sortingMethod == 'l2h')
{
    sortProductsPriceAscending();
}
else if(sortingMethod == 'h2l')
{
    sortProductsPriceDescending();
}

});
function sortProductsPriceAscending()
{
    var products = $('.product');
    products.sort(function(a, b){
    return $(a).data("price") - $(b).data("price")});
    $(".products-grid").html(products);

}

function sortProductsPriceDescending()
{
    var products = $('.product');
    products.sort(function(a, b){ return $(b).data("price") - $(a).data("price")});
    $(".products-grid").html(products);

}

HTML代码

<div class="col-sm-6">
                  <div class="pxp-sort-form form-inline float-right">
                      <div class="form-group">
                          <select class="type-regular custom-select price-sorting" id="pxp-sort-results">
                              <option value="" selected="selected disabled">Default Sort</option>
                              <option value="l2h" id="l2h">Price (Lo-Hi)</option>
                              <option value="h2l">Price (Hi-Lo)</option>

                          </select>
                      </div>
                      <div class="form-group d-flex">
                          <a role="button" class="pxp-map-toggle"><span class="fa fa-map-o"></span></a>
                      </div>
                  </div>
              </div>

<div class="row products-grid">
              {% for item in properties.all %}
              <div class="col-sm-12 col-md-6 col-xxxl-4 product" data-price="{{ item.price }}">
                  <a href="{% pageurl item %}" class="pxp-results-card-1 rounded-lg"  data-prop="1">
                      <div id="property-{{item.id}}" class="carousel slide" data-ride="carousel" data-interval="false">
                          <div class="carousel-inner">
                              {% for j in item.prop_images.all %}
                                {% image j.prop_img original as property_img %}
                                <div class="carousel-item {% if forloop.first %} active {% endif %}" style="background-image: url('{{property_img.url}}')"></div>
                              {% endfor %}
                          </div>
                          <span class="carousel-control-prev" data-href="#{{item.prop_name}}" data-slide="prev">
                              <span class="fa fa-angle-left" aria-hidden="true"></span>
                          </span>
                          <span class="carousel-control-next" data-href="#property-{{item.id}}" data-slide="next">
                              <span class="fa fa-angle-right" aria-hidden="true"></span>
                          </span>
                      </div>
                      <div class="pxp-results-card-1-gradient"></div>
                      <div class="pxp-results-card-1-details" id="prop-dtls">
                          <div class="pxp-results-card-1-details-title">{{item.prop_name}}</div>
                          <div class="pxp-results-card-1-details-price price">&#8377 {{item.price}}</div>
                      </div>
                      <div class="pxp-results-card-1-features">
                          <span>{{item.bedroom}} BD <span>|</span> {{item.bathroom}} BA <span>|</span> {{item.sqft}} SF</span>
                      </div>
                      <div class="pxp-results-card-1-save"><span class="fa fa-star-o"></span></div>
                  </a>
              </div>
              {% endfor %}

          </div>
© www.soinside.com 2019 - 2024. All rights reserved.