2017-07-03 127 views
1

我有一個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++; 
} 

回答

2

這應該是你的核心算法:

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變量「行」。
+0

Knut爲此感謝,xpages-noob也爲您提供解決方案!我稍微修改了Knuts代碼以實現我想要的其他事情,但正如你提到的Knut,核心內容是讓我能夠實現我想要的。有一兩件事我注意到...我做一個按鈕,一個完整的提交運行此代碼,當它重新加載,它仍然顯示了已刪除的字段,但是當我在一個新的標籤或窗口中打開同一個文檔,它正確地顯示沒有刪除的字段。有任何想法嗎?再次感謝所有貢獻傢伙! –

+0

它與顯式的'document1.save()'一起工作嗎? –

+0

這就是我想要的 - 因爲在我的beforePageLoad中,我運行一些代碼來從後端文檔中構建顯示正確字段(存儲爲viewScope變量)的字段數組。行viewScope正確地顯示4例如,但是字段viewScope變量仍然顯示7個元素(8個字段)將更新我的問題與beforePageLoad代碼 –

2

如果我理解正確的話您的問題,那麼這段代碼應該工作(未測試):

// 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); 
} 
+0

我建議用@KnutHerrmann給出的解決方案,它提供了更好的性能。 –