XPages - save date only in Date field

不羁岁月 提交于 2019-12-02 00:47:18

问题


I'm using an Edit Box control to display a date field. When the XPage is saved, I would like to save the date only (now both date and time are being saved). Is there any way of doing this?

Here is my code:

<xp:inputText id="dateReparatur" value="#{document1.dateReparatur}">
<xp:this.converter>
<xp:convertDateTime type="date" dateStyle="long">
</xp:convertDateTime>
</xp:this.converter>
<xp:dateTimeHelper></xp:dateTimeHelper>
</xp:inputText></xp:td>

UPDATE: I have now implemented the following code:

var dt = currentDocument.getItemValueDateTime("dateReparatur");
var dateonly = dt.getDateOnly();
currentDocument.replaceItemValue("dateReparatur",dateonly);

This gives me the date only, however in Notes the field type is now text rather than Date/Time, which is what I was hoping for.


回答1:


This code worked for me:

    <xp:this.postSaveDocument><![CDATA[#{javascript:
        var dt:DateTime = document1.getItemValueDateTime("dateReparatur");
        dt.setAnyTime();
        currentDocument.getDocument(true).replaceItemValue("dateReparatur", dt); 
        currentDocument.getDocument(true).save()
    }]]></xp:this.postSaveDocument>

It does work at postSaveDocument event only. If you put the same code into querySaveDocument event (without document save() line of course) the date field gets polluted with time after event during saving.

An alternative is to execute computeWithForm at querySaveDocument event:

<xp:this.querySaveDocument><![CDATA[#{javascript:
    document1.getDocument(true).computeWithForm(true, true)
}]]></xp:this.querySaveDocument>

You'd have to add an Input Translation formula to date field(s) in your form:

@Date(@ThisValue)

computeWithForm has a poor performance and causes sometimes side effects on field values though but might be a good solution especially if you have a lot of such date-only-fields.




回答2:


getDateOnly() returns a string. Try this:

dt.setAnyTime();
currentDocument.replaceItemValue("dateReparatur", dt);

Or you may have to get the Document:

currentDocument.getDocument(true).replaceItemValue("dateReparatur", dt);


来源:https://stackoverflow.com/questions/19636641/xpages-save-date-only-in-date-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!