动态移动内容
Moving round content dynamically
我有一个 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");
}
}
使用 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++;
}
如果我正确理解你的问题,那么这段代码应该可以工作(未测试):
// 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);
}
这应该是你的核心算法:
var writeIndex = 0;
for (var readIndex = 1; readIndex <= viewScope.rows; readIndex++) {
var value = document1.getItemValueString("ObjectiveDetails" + readIndex);
if (value) {
writeIndex++;
if (readIndex !== writeIndex) {
document1.removeItem("ObjectiveDetails" + readIndex);
document1.replaceItemValue("ObjectiveDetails" + writeIndex, value);
}
} else {
document1.removeItem("ObjectiveDetails" + readIndex);
}
}
viewScope.rows = writeIndex;
代码
- 删除所有空白字段,
- 将字段重命名为 ObjectiveDetails1、ObjectiveDetails2、ObjectiveDetails3... 和
- 将结果行数写回 viewScope 变量 "rows"。
我有一个 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");
}
}
使用 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++;
}
如果我正确理解你的问题,那么这段代码应该可以工作(未测试):
// 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);
}
这应该是你的核心算法:
var writeIndex = 0;
for (var readIndex = 1; readIndex <= viewScope.rows; readIndex++) {
var value = document1.getItemValueString("ObjectiveDetails" + readIndex);
if (value) {
writeIndex++;
if (readIndex !== writeIndex) {
document1.removeItem("ObjectiveDetails" + readIndex);
document1.replaceItemValue("ObjectiveDetails" + writeIndex, value);
}
} else {
document1.removeItem("ObjectiveDetails" + readIndex);
}
}
viewScope.rows = writeIndex;
代码
- 删除所有空白字段,
- 将字段重命名为 ObjectiveDetails1、ObjectiveDetails2、ObjectiveDetails3... 和
- 将结果行数写回 viewScope 变量 "rows"。