如何使用提交按钮(onclick)在javaScript(服务器端)上创建消息框

时间:2013-07-16 08:04:17

标签: javascript xpages xpages-extlib xpages-ssjs

这是我写的样本脚本。

任务: - 创建弹出消息以向用户显示他们是否未在系统上注册为成员。

- 用户必须在备注视图“员工信息名称”

中包含姓名

- 如果用户在视图中有名称,它会向审阅者写一条消息,通知他们要求的内容。

- 如果用户在“员工信息名称”视图中没有姓名,则会弹出一条消息告诉用户该怎么做。

// address book
var db:NotesDatabase = session.getDatabase("websvr/pcs", "names", false);
var vw2:NotesView = db.getView("($VIMPeople)");
var dc2:NotesDocumentCollection = vw2.getAllDocumentsByKey(document1.getItemValue("Name"),true);
var doc2:NotesDocument=dc2.getFirstDocument();

while (doc2!=null){
sname=doc2.getItemValueString('LastName')
doc2=dc2.getNextDocument();
}
// end of address book


//current database
var vw:NotesView = database.getView("Staff Information By Name");
var doc:NotesDocument = vw.getDocumentByKey(sname, true);


    //check whether have setting on reviewer for current user else will be hard code to specific person
    if (doc!=null)
    {
        revname=doc.getItemValueString("Reviewer")
        rev=doc.getItemValueString("ReviewerEmail");

        //set Email
        var maildoc:NotesDocument=database.createDocument();
        var body=maildoc.createMIMEEntity();
        var stream=session.createStream();
        var content="Dear "+revname+",<br></br>"+

        "Please click <a href='http://"+applicationScope.hostname+"/"+(applicationScope.dbfilepath).replace(/(\\)/g, "/")+"/"+
        "RequisitionForm.xsp?databaseName="+applicationScope.serverCN+"!!"+
        applicationScope.dbfilepath+"&documentId="+
        document1.getDocument().getUniversalID()+
        "&action=editDocument'>here</a> to view it if you are in a web browser (eg: Internet Explorer, Mozilla Firefox, Google Chrome etc.)"+
        "<br /><br /><b>OR</b><br /><br />"+
        "<a href='"+document1.getDocument().getNotesURL()+"'>here</a> if you are in the Notes Client. Thank you.";

        stream.writeText(content);
        body.setContentFromText(stream, "text/html; charset=iso-8859-1", 0);
        maildoc.replaceItemValue('Subject', 'Kindly review this '+document1.getItemValueString('Item')+' request by '+document1.getItemValueString('Name')+" on "+I18n.toString(@Today(), 'dd/MM/yyyy'));
        maildoc.send(rev);

        document1.replaceItemValue("TotalCost",document1.getValue("Cost")*document1.getValue("Qty"));
        document1.replaceItemValue("Status","Pending");
        document1.save();
        context.redirectToPage("/MyRequisition.xsp");
    }
else
{
a = 'alert("You do not have access right, please contact admin to register!")' ;
view.postScript(a);
break;
}

在其他页面上运行的示例脚本:

a = 'alert("The applicant details must be unique!")' ;
b = 'alert("Applicant Details inserted!")' ;

var vw:NotesView = database.getView("Staff information by Name");

var doc:NotesDocument = vw.getDocumentByKey(getComponent("Name").getValue(), true);

if (doc!=null){
view.postScript(a);
break;
}
else
{

var newDoc = database.createDocument();
newDoc.appendItemValue("Form", "Staff Form");
newDoc.appendItemValue("Name", getComponent("Name").getValue());
newDoc.appendItemValue("Designation", getComponent("Designation").getValue());
newDoc.appendItemValue("Department", getComponent("Department").getValue());
newDoc.appendItemValue("Reviewer", getComponent("Reviewer").getValue());
newDoc.appendItemValue("Email", getComponent("Email").getValue());
newDoc.appendItemValue("ReviewerEmail", getComponent("ReviewerEmail").getValue());
newDoc.save();
getComponent("Name").setValue("");
getComponent("Designation").setValue("");
getComponent("Department").setValue("");
getComponent("Reviewer").setValue("");
getComponent("Email").setValue("");
getComponent("ReviewerEmail").setValue("");
view.postScript(b);
}

2 个答案:

答案 0 :(得分:2)

postScript()仅适用于部分刷新。如果您的代码在完全刷新模式下在事件处理程序中运行,则您的postScript语句将无效。

替代解决方案是使用输出脚本(xp:scriptBlock)组件,如下所示:

var message = "#{requestScope.message}";
if (message) {
  alert(message);
}

在你的SSJS中设置你的信息:

requestScope.message = condition ? "True!" : "False!";

http://publib.boulder.ibm.com/infocenter/domhelp/v8r0/index.jsp?topic=%2Fcom.ibm.designer.domino.ui.doc%2Fwpd_controls_cref_scriptblock.html

答案 1 :(得分:0)

您可以使用facesContext和postscript在服务器端调用JavaScript函数。

facesContext.getViewRoot().postScript("alert('"+documentName.getItemValueString("FieldName"));