Xpages 动态移动内容

Xpages 动态移动内容,xpages,xpages-ssjs,Xpages,Xpages Ssjs,我有一个xpage,其中字段是动态创建的。默认情况下,有3个,但您可以单击按钮添加任意数量的内容,命名约定为“ObjectiveDetails1”、“ObjectiveDetails2”等等 我正在尝试处理空白字段。。。例如,字段1和2中有内容,字段3和字段4中没有内容,但字段5中有内容。我想做的是将内容从字段5移到第一个可用的空白处,在本例中为3。同样,如果字段1、2、4、5中有内容,那么我需要4中的内容进入字段3,5中的内容进入字段4,等等。目前,我正在设法将内容向上移动1 因此,如果字段2

我有一个xpage,其中字段是动态创建的。默认情况下,有3个,但您可以单击按钮添加任意数量的内容,命名约定为“ObjectiveDetails1”、“ObjectiveDetails2”等等

我正在尝试处理空白字段。。。例如,字段1和2中有内容,字段3和字段4中没有内容,但字段5中有内容。我想做的是将内容从字段5移到第一个可用的空白处,在本例中为3。同样,如果字段1、2、4、5中有内容,那么我需要4中的内容进入字段3,5中的内容进入字段4,等等。目前,我正在设法将内容向上移动1

因此,如果字段2、3、4为空,但5有内容,它只会将内容移动到4中,我需要它移动到字段2中

我希望我已经解释得足够清楚了。。。。。当前的代码有点乱

for (var i = 1; i < viewScope.rows+1; i++) {
print("Starting Array.....");
if(applicationScope.get("BreakAllCode")==true){
  break;
}
var objFieldName:string = "ObjectiveDetails" +i;
print ("Field Name: " + objFieldName);

var fieldValue = document1.getItemValueString(objFieldName);

print ("Field Value: " + fieldValue);
if (fieldValue =="" || fieldValue==null){
    print("EMPTY"); 

    // We now need to delete the 3 fields related to this objective
    var updFieldName:string = "ObjectiveUpdates" +i;
    var SAFieldName:string = "ObjectiveSelfAssessment" +i;

    // Before we delete the fields, we need to check if the next field is blank
    // and if not, copy its contents into this field.

    for (var n =i+1; n < viewScope.rows+1; n++) {
    if(applicationScope.get("BreakAllCode")==true){
     break;
    }

        if(document1.hasItem("ObjectiveDetails" +n)){       
            print("Next field: " +"ObjectiveDetails" +n);
            var nextFieldValue = document1.getItemValueString("ObjectiveDetails" +n);

            if (!nextFieldValue =="" || !nextFieldValue==null){
                // Now copy the content into the field
                var nextFieldName:string = "ObjectiveDetails" +n;
                var previousNo = n-1;
                var previousFieldName:string = "ObjectiveDetails" +previousNo;
                print ("Previous field: " + previousFieldName);
                document1.replaceItemValue(previousFieldName,nextFieldValue);

                // Now clear the content from next field
                document1.replaceItemValue(nextFieldName,"");
            }else{
                // Do nothing
            }
        }else{
            print("Last field");
        }
    }
    // Remove items from the document
    //document1.removeItem(objFieldName);
    //document1.removeItem(updFieldName);
    //document1.removeItem(SAFieldName);

    // Remove fields from our arrays
    //viewScope.fields.splice(i-1, 1);
    //viewScope.fields2.splice(i-1, 1);
    //viewScope.fields3.splice(i-1, 1);

    // Update the row variable
    //viewScope.rows--;
    //document1.replaceItemValue("rows",viewScope.rows);
    //document1.save();

    // We now need to "re-index" the array so that the fields are numbered corectly

}else{
    print("We have a value");
}
}
for(变量i=1;i
使用beforePageLoad更新:

viewScope.rows = document1.getItemValueInteger("rows");

var rowCount:integer = viewScope.rows;
var newCount:integer = 0;

while(newCount<rowCount){
if (!viewScope.fields) {
    viewScope.fields = [];
    viewScope.fields2 = [];
    viewScope.fields3 = [];
}

viewScope.fields.push("ObjectiveDetails" + (viewScope.fields.length + 1));
viewScope.fields2.push("ObjectiveUpdates" +  (viewScope.fields2.length + 1));
viewScope.fields3.push("ObjectiveSelfAssessment" +  (viewScope.fields3.length + 1));

newCount++;
}
viewScope.rows=document1.getItemValueInteger(“rows”);
var rowCount:integer=viewScope.rows;
var newCount:integer=0;

而(newCount如果我正确理解了您的问题,那么此代码应该可以工作(未经测试):

//定义变量
变量i、j、nRows、值、值、allowContinue、字段名;
//从文档字段收集值
nRows=viewScope.rows;
值=[];

对于(i=0;i如果我正确理解了您的问题,那么此代码应该可以工作(未测试):

//定义变量
变量i、j、nRows、值、值、allowContinue、字段名;
//从文档字段收集值
nRows=viewScope.rows;
值=[];

对于(i=0;i这应该是您的核心算法:

var writeIndex=0;

对于(var readIndex=1;readIndex,这应该是您的核心算法:

var writeIndex=0;

对于(var readIndex=1;readIndex我建议使用@KnutHerrmann给出的解决方案,因为它提供了更好的性能。我建议使用@KnutHerrmann给出的解决方案,因为它提供了更好的性能。Knut为此表示感谢,xpages noob也提供了您的解决方案!我稍微修改了Knuts代码,以实现我想要的其他一些功能,但是正如你提到的克努特,核心内容是为了让我实现我想要的。我注意到一件事……我在运行此代码的按钮上执行完全提交,当它重新加载时,它仍然显示删除的字段,但是当我在新选项卡或窗口中打开同一文档时,它正确地显示,而没有删除的字段。有什么想法吗?谢谢这是因为在我的beforePageLoad中,我运行了一些代码从后端文档构建一个字段数组,以显示正确的字段(存储为viewScope变量)。例如,行viewScope正确显示4,但fields viewScope变量仍然显示7个元素(8个字段)将使用beforePageLoad codeIMO更新我的问题在这种情况下,完全刷新是不够的。您必须重新加载整个页面才能再次执行beforePageLoad事件。您可以通过添加
context.reloadPage()来测试这一点;
到submit按钮中的代码末尾。xpages-noob您是对的!我今天早上发现Paul的以下博文,其中说完全刷新只会重新使用组件树,以便使用context.reloadPage();再次感谢()克努特谢谢你,xpages noob也谢谢你的解决方案!我稍微修改了克努特代码,以实现我想要的其他一些事情,但正如你提到的克努特,核心内容是为了让我实现我想要的。我注意到一件事……我对运行此代码的按钮进行了完全提交,当它重新加载时,它仍然显示del但是,当我在
// define variables
var i,j,nRows,values,value,allowContinue,fieldname;

// collect values from document fields
nRows=viewScope.rows;
values=[];
for (i=0;i<nRows;i++) {
    values.push(document1.getItemValueString("ObjectiveDetails"+(i+1).toFixed(0)));
}

// fill empty values
for (i=0;i<nRows-1;i++) {
    if (values[i]) continue;
    allowContinue=false;
    for (j=i+1;j<nRows;j++) {
        if (value=values[j]) {
            values[i]=value;
            values[j]="";
            allowContinue=true;
            break;
        }
    }
    if (!allowContinue) break;
}

// write back to document and remove empty fields on the end
for (i=0;i<nRows;i++) {
    fieldname="ObjectiveDetails"+(i+1).toFixed(0);
    if (value=values[i]) document1.replaceItemValue(fieldname,value);
    else document1.removeItem(fieldname);
}