Xpages文件下载控件排序列

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

我想有一个文件下载控件显示顶部最新(最新)创建日期的附件。默认是显示最新的最后一个。

<xp:fileDownload rows="30" id="FD"
    displayLastModified="false" value="#{document1.files}"
    style="width:98%" hideWhen="false"
    displayType="true" allowDelete="false" displayCreated="true">
</xp:fileDownload>
file download controls xpages
1个回答
4
投票

由于目前没有更好的答案,我会在这里发布一个。

实际上,<xp:fileDownload>组件列出了文件附件,它们出现在文档的Rich Text字段中,而不是最新的文件:enter image description here

您无法使用任何属性更改此行为,因此可能的方法是获取附件列表,按需要对其进行排序,然后将排序后的列表提供给<xp:repeat>组件,您可以在其中绘制附件表与<xp:fileDownload>显示的内容略有差异甚至没有差异。这并不难,只需在浏览器调试工具中查看创建的HTML标记,然后在<xp:repeat>中重新创建。

假设您在页面上声明了dominoData

<xp:this.data>
    <xp:dominoDocument var="document1"
        documentId="9CAA72D47AEA7C8D462582FB005AB525"
        action="openDocument" />
</xp:this.data>

然后创建你的<xp:panel>所在的<xp:repeat>。为您的面板创建dataContext

<xp:panel>
    <xp:this.dataContexts>
        <xp:dataContext var="attachments">
            <xp:this.value><![CDATA[
                #{javascript:
                    var sourceList:java.util.List = document1.getAttachmentList('files');
                    if (sourceList.size() == 0) {
                        return sourceList;
                    }
                    java.util.Collections.sort(sourceList, createdComparator);
                    return sourceList;
                }
            ]]></xp:this.value>
        </xp:dataContext>
    </xp:this.dataContexts>
</xp:panel>

在那里你得到一个com.ibm.xsp.model.domino.wrapped.DominoDocument.AttachmentValueHolder对象列表,然后使用created文件属性对声明的Comparator进行排序(参见下面的更新),并将排序列表作为attachments变量返回。

然后你创建<xp:repeat>并在<xp:panel>之后将它嵌入你的<xp:dataContexts>。给它dataContext的变量名称为value

<xp:repeat value="#{attachments}" var="attachment">
    <xp:text value="#{attachment.type}" />
    <xp:label value=" - " />
    <xp:text>
        <xp:this.value><![CDATA[
            #{javascript:
                var rawSize = attachment.getLength();
                return (rawSize < 1024 ? 1 : (rawSize / 1024).toFixed(0)) + " KB";
            }
        ]]></xp:this.value>
    </xp:text>
    <xp:label value = " - " />
    <xp:link text="#{attachment.name}" value="#{attachment.href}" />
    <xp:label value = " - " />
    <xp:text>
        <xp:this.value>
            #{javascript:
                return new java.util.Date(attachment.getCreated());
            }
        </xp:this.value>
        <xp:this.converter>
            <xp:convertDateTime type="both" timeStyle="short" />
        </xp:this.converter>
    </xp:text>
    <xp:br />
</xp:repeat>

这是<xp:repeat>输出与<xp:fileDownload>相比的结果:enter image description here

只需创建看起来像fileDownload表的标记,就完成了。

更新

值得努力创建一个请求范围的Managed Bean,它将作为比较器,而不是在SSJS代码块内部实现一些好的排序算法。

在一些现有或新的包下的Code / Java文件夹中创建一个Java类。如果包裹名称是例如com.benway.util和班级名称是CreatedComparator

package com.benway.util;

import java.util.Comparator;
import com.ibm.xsp.model.FileRowData;

public class CreatedComparator implements Comparator<FileRowData> {

    public int compare(FileRowData file1, FileRowData file2) {
        if (file1 == null || file2 == null) return 0;
        return (int)(file2.getCreated() - file1.getCreated());
    }
}

faces-config.xml中将您的新类注册为托管bean:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
    <managed-bean>
        <managed-bean-name>createdComparator</managed-bean-name>
        <managed-bean-class>
            com.benway.util.CreatedComparator
        </managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
    ...etc...
</faces-config>

现在你真的完成了:)

© www.soinside.com 2019 - 2024. All rights reserved.