4

长期用户,第一次发帖。

基本上,我在不同位置有一系列售货亭,并使用“液体”我有一个凭证系统,它会自动将与某个位置关联的所有凭证放入列表中。我还使用此代码在每 10 个条目后开始一个新列表。然后使用 jQuery,我添加了滚动条以在列表之间滑动。代码非常简单:

{% for voucher in vouchers %}
{% capture modulus %}{{ forloop.index0 | mod:10 }}{% endcapture %} 
{% if forloop.index0 != 0 %} 
{% if modulus == "0" %} 
</ul></li><li><ul class="voucherlist"> 
{% endif %}
{% endif %}
<div id="{{ voucher.meta }}" class="voucher_list">
    <li><a href="{{ 'voucher' | url_for_page : voucher.id }}">{{ voucher.meta }}</a></li>
</div>
{% endfor %}

但是,列表中的某些凭证出现了不止一次。这是因为代金券分为 3 类,有些可以交叉。由于有多个位置,我无法在凭证中添加任何东西,例如钥匙或标签来阻止它显示,因为它可能希望显示在另一个位置。另外,每一个都必须手动调整,这个系统的重点是尽可能地自动化。因此,我使用了一些我非常满意的 jquery。

<script type="text/javascript">
    $(document).ready(function () {
        $('[id]').each(function () {
            var ids = $('[id=' + this.id + ']');
            if (ids.length > 1 && ids[0] == this) {
                $(ids[1]).remove()
            }
        });
    });
</script>

从这里你可以看出我使用凭证名称作为 div id,然后 jquery 删除任何具有相同 id 的 div。但是,它在原来的位置留下了一个空间,因此有些页面有 8 个而不是 10 个。因此我们遇到了我的问题。如何在不留任何空格的情况下删除液体列表中的重复项?

我试图将“.remove()”更改为“.hide()”,但没有用。我把它改成

.addClass( "duplicate" )

希望然后在液体中添加一条线来表示类似

{% if div.class != "duplicate" %}

因此,不要使用 div 为“重复”的那些。这会很好。但我找不到执行此操作的代码,甚至不知道它是否可能。我试图在这里涵盖所有角度并尽我所能解释一切。我是如此接近,但也许不同的观点会起作用,或者有更简单的方法吗?我什至走在正确的轨道上吗?任何想法将不胜感激。

编辑:这是一张尝试进一步解释的图片。http://img683.imageshack.us/img683/6295/voucherpagehelp.jpg 另外,我添加了一些从其他地方提取的代码,这将有助于解释滚动系统。抱歉,这之前不清楚。

提前致谢。

4

2 回答 2

2

这是我对删除 Liquid 中的重复项的看法:

{% assign array = 'c|c|b|b|a|a' | split: '|' %}
{% assign result = array[1] %}

{% for item in array %}
    {% unless result contains item %}
        {% capture result %}{{ result }}|{{ item }}{% endcapture %}
    {% endunless %}
{% endfor %}

{{ result | split: '|' | sort | join: ', ' }}
于 2014-03-03T21:19:06.333 回答
0

如果你打算使用 jQuery 来构建这个列表,我会使用液体 JSON 过滤器将整个凭证对象捕获到一个 JavaScript 对象中。然而,我更喜欢使用液体,以允许没有 JavaScript 的浏览器正确查看内容:

{% assign UsedIDs = '' %}
{% assign Counter = 0 %}
{% for voucher in vouchers %}
{% capture IDToCheck %},{{voucherID}},{% endcapture %}
{% unless UsedIDs contains IDToCheck %}
{% capture modulus %}{{ Counter | mod:10 }}{% endcapture %} 
{% if Counter != 0 %} 
{% if modulus == "0" %} 
</ul></li><li><ul class="voucherlist"> 
{% endif %}
{% endif %}
<div id="{{ voucher.meta }}" class="voucher_list">
    <li><a href="{{ 'voucher' | url_for_page : voucher.id }}">{{ voucher.meta }}</a></li>
</div>
{% capture UsedIDs %}{{ UsedIDs }}{{ IDToCheck }}{% endcapture %}
{% capture Counter %}{{ Counter | plus: 1 }}{% endcapture %}
{% endunless %}
{% endfor %}
于 2011-11-17T05:23:44.137 回答