我有这样的代码,我在使用列表生成记录归入一年,让系列,体的样式,颜色的车辆。我想来自这个还是这样的:

  • 今年,我希望只有到2004年是个人...其余的将在下降的其他即2009, 2008, 2007, 2006, 2005, 2004, 其他。
  • 为使,我想显示的六个使用最受欢迎...有一个现场模型中,我使用的分配的普及使用价值的初级(最高)、二级或三级。其余的将在下降。
  • 体型和颜色,我想要有该项具有少于3的记录下降。

我的代码如下:

year_count = vehicle_query.order_by(
  '-common_vehicle__year__year').values('common_vehicle__year__year').
  annotate(count=Count('id'))
make_count = vehicle_query.order_by(
  'common_vehicle__series__model__manufacturer__manufacturer').
  values('common_vehicle__series__model__manufacturer__manufacturer').
  annotate(count=Count('id'))
style_count = vehicle_query.order_by(
  'common_vehicle__body_style__style').values
  ('common_vehicle__body_style__style').annotate(count=Count('id'))
colour_count = vehicle_query.order_by(
  'exterior_colour__exterior_colour').values(
  'exterior_colour__exterior_colour').annotate(count=Count('id'))
有帮助吗?

解决方案 2

我得到了一个解决方案,因此我认为这会是很好的,更新的答案在这里:

在头部分,我有这样的:

<script type="text/javascript" src="{{ MEDIA_URL }}share/jquery/jquery.min.js"></SCRIPT>
<script type="text/javascript">
(function($) {
$(document).ready(function() {
    //hide the additional content under "Display More"
    $("div.additional_content").hide();

    $("a.more").click(function () { 
        //show or hide the additional content
        $(this).siblings("div.additional_content").toggle();
        //change the attributes and text value of the link toggle
        if($(this).text() == "Display Less"){
            $(this).removeClass("less");
            $(this).addClass("more");
            $(this).html("Display More");
        }else{
            $(this).removeClass("more");
            $(this).addClass("less");
            $(this).html("Display Less");
        }
        return false;
    });             
});
})(jQuery);

然后无论我想要减少数目的可用的选择我有这样的:

<div class="module_wrap">
  <div class="module"> {% if year_count %} <strong>{% trans "Year" %}</strong> <br />
    {% for item in year_count|slice:":6" %}
    <ul>
      <li> <a href="/inventory/year/{{ item.common_vehicle__year__year }}/">{{ item.common_vehicle__year__year }} ({{ item.count }})</a> {% if request.session.chosen_year %} <a href="/undo_year/"><img src="{{ MEDIA_URL }}img/undo.gif" border="0" alt="Remove Year Filter" title="Remove Year Filter" /></a> {% endif %} </li>
    </ul>
    {% endfor %}
    <div class="additional_content"> {% for item in year_count|slice:"6:" %}
      <ul>
        <li> <a href="/inventory/year/{{ item.common_vehicle__year__year }}/">{{ item.common_vehicle__year__year }} ({{ item.count }})</a></li>
      </ul>
      {% endfor %} </div>
    {% if year_count|slice:"6:" %}<a href="" class="more">Display More</a><br />
    {% endif %} <br />
  </div>
</div>
{% endif %}

其他提示

大什么你要求可能会更好地处理外Django而是通过客户的侧javascript。到是清楚的,你 可能 已经部分处理的决,但这将是吸尘器没有这样做。是有好处的,这样做:

  1. 你Django模板码保持清洁
  2. 它会降解很好
  3. 你以后可以更新界面(更改javascript),而不必担心破Django模板

处理这个你可能只是做一个脚本时 <ul> 标记(以及或许一些参数)将使该清单的格式要求。

这里有一个简单的例子使用的!对这个例子中,我要去换功能的使用jQuery插件模式。

说你django模板的产出以下...

<ul>
    <li>Chevy</li>
    <li>Mazda</li>
    <li>Honda</li>
    <li>Ford</li>
    <li>BMW</li>
</ul>

jquery.showmorelist.js

(function($) {

    $.fn.ShowMoreList = function(visibleItemCount) {

        // Wrap parent element
        var parent = $(this).wrap('<div class="show-more-list"></div>').parent();
        var ul = $(this);

        // Enumerate children and hide extras
        var counter = 0;
        $(this).children().filter('li').each(function(){
            counter += 1;
            if (counter > visibleItemCount) {
                $(this).hide();
                $(this).addClass('hidden');
            }
        });

        // Add link and bind click
        var link = $('<a href="#">&gt; Show More</a>').click(function(){
           $(ul).children().filter('.hidden').show();
        });
        $(parent).append(link);
    }

})(jQuery);

page.html

<script type="text/javascript" src="jquery.showmorelist.js"></script>
<script type="text/javascript">
    // On page load...
    $(function() {
            $('ul').ShowMoreList(4);    // Shows only the first 4 items
    });
</script>

这是一个相当简单的例子,它不会开关的"更多"到"隐藏更多的"但你应该能够图,从上下文。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top