是否可以通过if条件来更新Visualforce页面的内容类型?

发布于 2025-01-25 22:58:09 字数 390 浏览 5 评论 0原文

一直在寻找一种更新Apex的方法:页面内容类型,但看不到有关它的任何来源。 我有此ATM:

<apex:page standardController="sObject" extensions="<apexClass>"
</apex:page> 

扩展程序中的Apex类具有很多功能,并且页面中间有一个HREF,该功能链接到模板时,当单击时可以查看并保存为CSV文件。 但是,我没有查看模板,而是想将功能升级到可下载的功能。因此,我想找到一种更新标题的方法,即单击链接时,它会添加此contentType =“ application/vnd.ms-excel the Apex:page

。想知道VF中是否有这样的事情?

Been looking for a way to update an apex:page content type but don't see any source about it AHAHA.
I have this ATM:

<apex:page standardController="sObject" extensions="<apexClass>"
</apex:page> 

the apex class in the extension has a lot of functionalities and i have an href in the middle of the page that is linked to a template which when clicked can be viewed and saved as a CSV file.
but instead of viewing a template, I wanted to upgrade the functionality into a downloadable one. So i want to find a way to update the header that when the link gets clicked it adds this contenttype="application/vnd.ms-excel to the header of the apex:page.

you guys have an idea if there's such a thing in VF?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唐婉 2025-02-01 22:58:09

是的,您几乎可以将表达式放在任何Visualforce参数中,包括APEX:page's contentType。这样的事情:

<apex:page lightningstylesheets="true" standardController="Contact" recordSetVar="contacts" extensions="SomeClass"
    showHeader="{!showForm}" sidebar="{!showForm}" applyHtmlTag="{!showForm}" standardStylesheets="{!showForm}"
    id="p" contentType="{!IF(showForm, 'text/html', 'application/vnd.ms-excel#Contactss.xls')}" readonly="true">

<apex:outputPanel layout="none" rendered="{!showForm}">
<apex:form >
    <apex:pageBlock>
        <apex:pageBlockButtons location="top">
            <apex:actionStatus id="status">
                <apex:facet name="stop">
                    <apex:outputPanel >
                        <apex:commandButton value="{!$Label.preview}" action="{!preview}" rerender="controls,preview" status="status"/>
                        <apex:commandButton value="{!$Label.export}" action="{!export}" status="status"/>
                        <apex:commandButton value="{!$Label.cancel}" action="{!cancel}" immediate="true" />
                    </apex:outputPanel>
                </apex:facet>
                <apex:facet name="start">
                    <apex:outputPanel >
                        <apex:commandButton value="{!$Label.preview}" disabled="true"/>
                        <apex:commandButton value="{!$Label.export}" disabled="true"/>
                        <apex:commandButton value="{!$Label.cancel}" action="{!cancel}" immediate="true" />
                        <img id="status" src="/img/loading.gif" alt="Loading..." title="Loading..."/>
                    </apex:outputPanel>
                </apex:facet>
            </apex:actionStatus>
        </apex:pageBlockButtons>
        
        <apex:pageBlockSection columns="1" id="controls">
            put some form here, input fields...
        </apex:pageBlockSection>
    </apex:pageBlock>
    <apex:pageBlock title="Preview" id="preview">
        optionally display first 10 rows or something
    </apex:pageBlock>
</apex:form>
</apex:outputPanel>

<apex:outputPanel id="o" layout="none" rendered="{!!showForm}">
{!csv} Payload here
</apex:outputPanel>
</apex:page>

但是,请务必检查一下它在闪电体验中的行为(如果使用)。上次我玩它时,Lex有一些额外的输出,最终我作弊了。我将CSV输出到HIDDENT HTML元素,使用JavaScript抓住它。

<apex:commandButton value="{!$Label.export}" action="{!downloadCsv}" rerender="out" oncomplete="javascript:downloadComplete();return false;" status="status"/>

...

<apex:outputPanel id="out" style="display:none">
    <span class="payload">{!csvBody}</span>
    <script>
    function downloadComplete(){
        let payload = document.getElementsByClassName('payload')[0].innerText.replaceAll('<br/>', '\n');
        let filename =  'Equipment Utilization Report.csv';

        let link = document.createElement('a');
        link.href = 'data:text/csv;charset=utf-8,' + encodeURIComponent(payload);
        link.target = '_self';
        link.download = filename;
        link.click();
        return false;
    }
    </script>
</apex:outputPanel>

Yes, you can put expressions in pretty much any Visualforce parameter including apex:page's contentType. Something like this:

<apex:page lightningstylesheets="true" standardController="Contact" recordSetVar="contacts" extensions="SomeClass"
    showHeader="{!showForm}" sidebar="{!showForm}" applyHtmlTag="{!showForm}" standardStylesheets="{!showForm}"
    id="p" contentType="{!IF(showForm, 'text/html', 'application/vnd.ms-excel#Contactss.xls')}" readonly="true">

<apex:outputPanel layout="none" rendered="{!showForm}">
<apex:form >
    <apex:pageBlock>
        <apex:pageBlockButtons location="top">
            <apex:actionStatus id="status">
                <apex:facet name="stop">
                    <apex:outputPanel >
                        <apex:commandButton value="{!$Label.preview}" action="{!preview}" rerender="controls,preview" status="status"/>
                        <apex:commandButton value="{!$Label.export}" action="{!export}" status="status"/>
                        <apex:commandButton value="{!$Label.cancel}" action="{!cancel}" immediate="true" />
                    </apex:outputPanel>
                </apex:facet>
                <apex:facet name="start">
                    <apex:outputPanel >
                        <apex:commandButton value="{!$Label.preview}" disabled="true"/>
                        <apex:commandButton value="{!$Label.export}" disabled="true"/>
                        <apex:commandButton value="{!$Label.cancel}" action="{!cancel}" immediate="true" />
                        <img id="status" src="/img/loading.gif" alt="Loading..." title="Loading..."/>
                    </apex:outputPanel>
                </apex:facet>
            </apex:actionStatus>
        </apex:pageBlockButtons>
        
        <apex:pageBlockSection columns="1" id="controls">
            put some form here, input fields...
        </apex:pageBlockSection>
    </apex:pageBlock>
    <apex:pageBlock title="Preview" id="preview">
        optionally display first 10 rows or something
    </apex:pageBlock>
</apex:form>
</apex:outputPanel>

<apex:outputPanel id="o" layout="none" rendered="{!!showForm}">
{!csv} Payload here
</apex:outputPanel>
</apex:page>

But be sure to check how it behaves in Lightning Experience (if you use it). Last time I played with it there was some extra output in LEX and eventually I cheated. I output CSV to hiddent html element, grab it with JavaScript.

<apex:commandButton value="{!$Label.export}" action="{!downloadCsv}" rerender="out" oncomplete="javascript:downloadComplete();return false;" status="status"/>

...

<apex:outputPanel id="out" style="display:none">
    <span class="payload">{!csvBody}</span>
    <script>
    function downloadComplete(){
        let payload = document.getElementsByClassName('payload')[0].innerText.replaceAll('<br/>', '\n');
        let filename =  'Equipment Utilization Report.csv';

        let link = document.createElement('a');
        link.href = 'data:text/csv;charset=utf-8,' + encodeURIComponent(payload);
        link.target = '_self';
        link.download = filename;
        link.click();
        return false;
    }
    </script>
</apex:outputPanel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文