在Xpages中的文档中添加编号 [英] Adding numbering to documents in Xpages
问题描述
我需要一些有关此的提示,因为尽管这可能很简单,但我找不到任何提示,但是我想不出办法. 好的,所以我需要一个隐藏字段,该字段将为每个文档自动计算所谓的id编号,这些编号必须从00001开始并向前,并且永远不能重复.例如,我创建了一个编号为00001的文档,另一个创建编号为00002的文档,如果我删除了第一个或第二个文档,则创建的第三个文档的编号应为00003.无论删除多少文档,下一个文档创建的应该获得最新的numberId并向其添加1.
I needs some tips on this one, since I couldn't find anything on this, though it's probably pretty simple to do, but I can't think of a way. Ok, so I need a hidden field which will automatically compute so called id numbers for every document, the numbers must start from 00001 and forward and must never ever repeat. For instance i create a document with numberId = 00001 and another document which creates numberId = 00002, if I ever delete the first or second document, the third created document should have numberId = 00003. No matter how many documents get deleted, the next document created should get the latest numberId and add 1 to it.
我希望我对这里的需求很清楚.我需要有关如何实现此目标的建议或提示.
I hope I was clear about what I need here. I need an advice or a tip on how would I achieve this.
此外,我不能使用@Unique.
Also, I can't use @Unique.
推荐答案
这是基于对问题的一些评论所需要的代码.
Here is the code that you will need based on some of the comments to the question.
function simplegetSequentialNumber(){
synchronized(applicationScope){
var newSeqNum:Int = 0;
if (applicationScope.containsKey("seqNumber")){
newSeqNum = applicationScope.get("seqNumber") + 1;
applicationScope.put("seqNumber", newSeqNum);
var seqView:NotesView = database.getView("vw_SequentialNumberStore");
var seqNumberDoc:NotesDocument = seqView.getFirstDocument();
seqNumberDoc.replaceItemValue("seqNumber",applicationScope.get("seqNumber"));
seqNumberDoc.save(true,true);
} else {
var seqView:NotesView = database.getView("vw_SequentialNumberStore");
try {
var seqNumberDoc:NotesDocument = seqView.getFirstDocument();
applicationScope.put("seqNumber",seqNumberDoc.getItemValueInteger("seqNumber") + 1);
seqNumberDoc.replaceItemValue("seqNumber",applicationScope.get("seqNumber"));
seqNumberDoc.save(true,true);
newSeqNum = applicationScope.get("seqNumber");
} catch(e) {
var seqNumberDoc:NotesDocument = database.createDocument();
seqNumberDoc.replaceItemValue("Form","cPanel");
seqNumberDoc.replaceItemValue("seqNumber",1);
applicationScope.put("seqNumber", 1);
seqNumberDoc.save(true,true);
newSeqNum = 1;
}
}
}
var seqNNNN:String = ("0000" + newSeqNum.toString()).slice(-4);
return seqNNNN;
}
如您所见,它首先在同步块中获取下一个序列号,将其添加一个,然后将该数字放回applicationScope中.
As you can see it first gets the next sequential number in a synchronized block, adds one to it and then puts the number back into the applicationScope.
然后将其转换为字符串,添加另外的4个零,然后添加右边的4个字符.这将返回一个字符串,需要将其存储在文本字段中.您不能将其存储在数字字段中,因为Notes会自动从值中删除前导零.
Then it converts it to the string, adds the additional 4 zeros and then the right 4 characters from it. This returns a string and needs to be stored in a text field. You cannot store it in a number field because Notes will automatically drop the leading zeros from the value.
您可以通过将此功能添加到服务器端javascript库中,然后将其包含在一个简单的页面中来进行测试,该页面运行一个重复控件以重复一个仅调用该函数的计算字段.这是我的测试页.
You can test this function by adding it to a server side javascript library and then including it in a simple page that runs a repeat control to repeat a computed field that just calls the function. Here is my test page.
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:this.resources>
<xp:script src="/seqNum.jss" clientSide="false" />
</xp:this.resources>
<xp:repeat id="repeat1" rows="30" value="#{javascript:30}">
<xp:text escape="true" id="computedField1"
value="#{javascript:simplegetSequentialNumber();}" />
<xp:br id="br1" />
</xp:repeat>
</xp:view>
这篇关于在Xpages中的文档中添加编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!