按FTL liferay中的嵌套字段值排序

时间:2017-07-25 13:05:28

标签: freemarker liferay-7 webcontent

我在liferay中创建的结构如下所示:

{
    "availableLanguageIds": [
        "en_US"
    ],
    "defaultLanguageId": "en_US",
    "fields": [
        {
            "label": {
                "en_US": "Image"
            },
            "predefinedValue": {
                "en_US": ""
            },
            "style": {
                "en_US": ""
            },
            "tip": {
                "en_US": "Upload the image to be displayed in the Small Banner"
            },
            "dataType": "image",
            "fieldNamespace": "ddm",
            "indexType": "keyword",
            "localizable": true,
            "name": "smallImage",
            "readOnly": false,
            "repeatable": true,
            "required": true,
            "showLabel": true,
            "type": "ddm-image",
            "nestedFields": [
                {
                    "label": {
                        "en_US": "Priority of Image"
                    },
                    "predefinedValue": {
                        "en_US": "0"
                    },
                    "style": {
                        "en_US": ""
                    },
                    "tip": {
                        "en_US": "Priority of the uploaded Image. Lower the number, higher the priority."
                    },
                    "dataType": "integer",
                    "fieldNamespace": "ddm",
                    "indexType": "keyword",
                    "localizable": true,
                    "name": "imagePriority",
                    "readOnly": false,
                    "repeatable": false,
                    "required": true,
                    "showLabel": true,
                    "type": "ddm-integer"
                }
            ]
        }
    ]
}

现在在模板中我想使用图像的优先级字段对其进行排序。我的模板如下所示:

<div id="example2" class="slider-pro">
    <div class="sp-slides">
        <#if smallImage.getSiblings()?has_content>
            <#list smallImage.getSiblings()?sort_by(["properties","imagePriority"]) as cur_smallImage>
                <#if cur_smallImage.getData()?? && cur_smallImage.getData() != "">
                    <div class="sp-slide">
                        <img class="sp-image" alt="${cur_smallImage.getAttribute("alt")}" src="${cur_smallImage.getData()}" data-src="${cur_smallImage.getData()}" data-retina="${cur_smallImage.getData()}" />
                    </div>
                </#if>
            </#list>
        </#if>
    </div>
</div>

我尝试排序为?sort_by(&#34; cur_smallImage.imagePriority&#34;)也是?sort_by(&#34; imagePriority&#34;)但它没有用。我想按照指定的优先级顺序显示图像。
请帮忙。

1 个答案:

答案 0 :(得分:0)

如果要用于排序的子变量位于更深的级别(即,如果它是子变量的子变量,依此类推),则可以使用序列作为参数,该参数指定导致所需子变量的子变量。例如:

Full Page

按名称排序。姓:

<#assign members = [
    {"name": {"first": "Joe", "last": "Smith"}, "age": 40},
    {"name": {"first": "Fred", "last": "Crooger"}, "age": 35},
    {"name": {"first": "Amanda", "last": "Fox"}, "age": 25}]>

取自:     https://freemarker.apache.org/docs/ref_builtins_sequence.html#ref_builtin_sort_by