Learner
Learner

Reputation: 827

Get selected documents from a view and save as separate document

I am trying to get selected documents from a view and save as separate documents

Here is the design of the xpage

A combo box on the top, a view with check box (this view is used for selection), a button for save the selection values from the view and the combo box. Also there will another view which will display the saved values.

From this post, I can get the unid of selected documents and save to view scope variable. The add button can save the selected value from the combo box and the view and show the result in another view.

However, if I choose more than one value in the view and click save, it saves all values in one document. So I try to use for loop to loop through the selected values in view and save, it still saves one value only in one document.

   <xp:table id="table1a">
    <xp:tr>
        <xp:td id="table1">
            <xp:comboBox id="comboBox1"
                dojoType="dijit.form.ComboBox" style="width:250.0px"
                value="#{document1.Category}">
                <xp:selectItems id="selectItems3">
                    <xp:this.value><![CDATA[#{javascript:var SetFirstValueBlank = @Text("");
                    return SetFirstValueBlank;
                }]]></xp:this.value>
                </xp:selectItems>
                <xp:selectItems id="selectItems4">
                    <xp:this.value><![CDATA[#{javascript:@Unique(@DbColumn(@DbName(), "CategoryListView", 1));}]]></xp:this.value>
                </xp:selectItems>
                <xp:eventHandler event="onchange" submit="true"
                    refreshMode="partial" refreshId="table1a">
                </xp:eventHandler>
            </xp:comboBox>
            <xp:text escape="true" id="computedField1">
            </xp:text>
        </xp:td>
    </xp:tr>
    <xp:tr>
        <xp:td>
            <xp:viewPanel rows="10" id="viewPanel1" var="rowData">
                <xp:this.data>
                    <xp:dominoView var="view1"
                        viewName="hListView">
                    </xp:dominoView>
                </xp:this.data>
                <xp:viewColumn columnName="ItemName"
                    id="viewColumn1" style="background-color:rgb(255,255,255)"
                    showCheckbox="true">
                    <xp:viewColumnHeader value="Item Name"
                        id="viewColumnHeader1" rendered="false">
                    </xp:viewColumnHeader>
                </xp:viewColumn>
                <xp:this.facets>
                    <xp:pager partialRefresh="true"
                        layout="Previous Group Next" xp:key="footerPager" id="pager2">
                    </xp:pager>
                </xp:this.facets>
            </xp:viewPanel>
            <xp:br></xp:br>
            <xp:button value="Add" id="button1"
                style="height:35.0px">
                <xp:eventHandler event="onclick" submit="true"
                    refreshMode="complete">
                    <xp:this.action><![CDATA[#{javascript:var Category = getComponent("comboBox1").getValue();
                    var viewPanel=getComponent("viewPanel1");
                    var docIDArray=viewPanel.getSelectedIds();
                    var unidArray = new Array();
                    for(i=0; i < docIDArray.length; i++) {
                   var unid=database.getDocumentByID(docIDArray[i]).getUniversalID();
                      unidArray.push(unid);
                    }
                    viewScope.put("unidArray", @Implode(unidArray, ","));
                for(var i=0; i< unidArray.length;i++ )
                {
                    document1.replaceItemValue("ItemName", unidArray[i]);
                    document1.save();
                }
        }]]></xp:this.action>
                </xp:eventHandler>
            </xp:button>
            <xp:br></xp:br>
            <xp:br></xp:br>
            <xp:viewPanel rows="6" id="viewPanel2">                     
                    <xp:this.data>
                        <xp:dominoView var="view2" viewName="CategoryItemView">
                            <xp:this.categoryFilter><![CDATA[#{javascript:getComponent("comboBox1").getValue();}]]></xp:this.categoryFilter>
                        </xp:dominoView>
                    </xp:this.data>
                    <xp:this.rendered><![CDATA[#{javascript:var value = getComponent("comboBox1").getValue();
                    if(value =="" || value == null)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }}]]></xp:this.rendered><xp:viewColumn id="viewColumn4" columnName="Category" rendered="false">
                        <xp:this.facets>
                            <xp:viewColumnHeader xp:key="header" id="viewColumnHeader4" value="Category">
                            </xp:viewColumnHeader>
                        </xp:this.facets>
                    </xp:viewColumn>
                    <xp:viewColumn id="viewColumn3" columnName="$10">
                        <xp:this.facets>
                            <xp:viewColumnHeader xp:key="header" id="viewColumnHeader3" value="Category">
                            </xp:viewColumnHeader>
                        </xp:this.facets>
                    </xp:viewColumn>
                    <xp:viewColumn columnName="ItemName" id="viewColumn2">
                        <xp:viewColumnHeader value="Item" id="viewColumnHeader2">
                        </xp:viewColumnHeader>
                    </xp:viewColumn>
                    <xp:this.facets>
                        <xp:pager partialRefresh="true" layout="Previous Group Next" xp:key="footerPager" id="pager3">
                        </xp:pager>
                    </xp:this.facets></xp:viewPanel><xp:br></xp:br></xp:td>
                </xp:tr>
            </xp:table>

Would someone let me know how to save multiple values in separate documents please? Thank you.

(edit: some of previous content are removed)

I think I will focus on how to save multiple value as separate documents first because this part is quite important in the program, once I can save as separate documents, there will another function that needs the result to process.

I review the code, I think I will still use for loop but unfortunately, it still saves one document only.

Upvotes: 0

Views: 526

Answers (1)

Simon Delicata
Simon Delicata

Reputation: 411

I'm guessing the document1 object is bound to a on the XPage, which is only created when the XPage is opened. To cause your button to create multiple documents, the for loop needs to modified :

var oneOfMany : NotesXspDocument;
for(var i=0; i< unidArray.length;i++ )
{
  oneOfMany = database.createDocument();
  oneOfMany.replaceItemValue( "Category",  document1.getItemValue("Category" ) );
  oneOfMany.replaceItemValue("ItemName", unidArray[i]);
  oneOfMany.replaceItemValue("form", "MyFormName" );
  oneOfMany.save();
}

Because it no longer uses document1, this loop will need to populate the other items you need such as the form, for example MyFormName as in the example.

Upvotes: 1

Related Questions