1
votes
  • I am using a viewpanel and allowing users to choose documents via a checkbox in a "choose" panel.

  • The document source is defined in the "view/edit" panel.

    • When they reach the last document the "view/edit" panel disappears and the "choose" panel appears.

All of the above works.

  • I have navigation button at the top which opens the "view/edit" panel. If the user puts a document in edit mode and decides (for whatever reason) that they want to use the navigation button to return to the "choose" panel and then chooses a new batch of documents to view or edit and the document that was in edit mode is the first document to be viewed - it will still be in edit mode.

    • I have tried using the javascript command: context.setDocumentMode("readonly") in many different places (before calculating the document, in afterRestoreView, in the navigation button) to no effect.

I hope there is an obvious solution.

Here is a code simplified code snippet to demonstrate the problem. All you need is to plugin your own datasource:>)

    <?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    >
    <xp:panel id="buttonpanel">
        <xp:button id="button1" value="Choose Docs">
            <xp:eventHandler event="onclick" submit="true"
                refreshMode="partial" refreshId="wrapperpanel">
                <xp:this.action><![CDATA[#{javascript:viewScope.choosepanelvis= true;
viewScope.vieweditpanelvis = false;}]]></xp:this.action>
            </xp:eventHandler>
        </xp:button>
    </xp:panel>
    <xp:panel id="wrapperpanel">
        <xp:panel id="choosepanel" rendered="#{javascript:viewScope.choosepanelvis}">
            <xp:label id="label1"></xp:label>
            <xp:button styleClass="BlueButton" id="button6"
                value="View/Edit Selected Docs">
                <xp:eventHandler event="onclick" submit="true"
                    refreshMode="partial" refreshId="wrapperpanel">
                    <xp:this.action>
                        <xp:actionGroup>
                            <xp:executeScript>
                                <xp:this.script><![CDATA[#{javascript:viewScope.DocIdList = new Array();
var viewPanel = getComponent("viewPanel1");
var docIDList = viewPanel.getSelectedIds();
viewScope.DocIdList = docIDList;
viewScope.DocIdLength = viewScope.DocIdList.length;}]]></xp:this.script>
                            </xp:executeScript>
                            <xp:actionGroup condition="#{javascript:viewScope.DocIdList.length === 0;}">
                                <xp:executeScript>
                                    <xp:this.script><![CDATA[#{javascript:var varAlert = "Please select one or more documents.";
var al = "alert('"+ varAlert +"')";
view.postScript(al);
return false;
}]]></xp:this.script>
                                </xp:executeScript>
                            </xp:actionGroup>
                            <xp:actionGroup>
                                <xp:this.condition><![CDATA[#{javascript:viewScope.DocIdList.length > 0}]]></xp:this.condition>
                                <xp:executeScript>
                                    <xp:this.script><![CDATA[#{javascript:viewScope.DocIdIndex = 0
var DocId=viewScope.DocIdList[viewScope.DocIdIndex];
viewScope.DocId = DocId;

viewScope.choosepanelvis= false;
viewScope.vieweditpanelvis = true;}]]></xp:this.script>
                                </xp:executeScript>
                            </xp:actionGroup>
                        </xp:actionGroup>
                    </xp:this.action>
                </xp:eventHandler>
            </xp:button>
            <xp:viewPanel rows="30" id="viewPanel1">
                <xp:this.facets>
                    <xp:pager partialRefresh="true" layout="Previous Group Next"
                        xp:key="headerPager" id="pager1">
                    </xp:pager>
                </xp:this.facets>
                <xp:this.data>
                    <xp:dominoView var="view1" viewName="NISDTeacher">
                    </xp:dominoView>
                </xp:this.data>
                <xp:viewColumn columnName="CampusName" id="viewColumn1"
                    showCheckbox="true">
                    <xp:viewColumnHeader value="CampusName" id="viewColumnHeader1"
                        showCheckbox="true">
                    </xp:viewColumnHeader>
                </xp:viewColumn>
                <xp:viewColumn columnName="TeacherFirst" id="viewColumn2">
                    <xp:viewColumnHeader value="TeacherFirst" id="viewColumnHeader2">
                    </xp:viewColumnHeader>
                </xp:viewColumn>
                <xp:viewColumn columnName="TeacherLast" id="viewColumn3">
                    <xp:viewColumnHeader value="TeacherLast" id="viewColumnHeader3">
                    </xp:viewColumnHeader>
                </xp:viewColumn>
            </xp:viewPanel>
        </xp:panel>
        <xp:panel id="vieweditpanel" rendered="#{javascript:viewScope.vieweditpanelvis}">
            <xp:this.data>
                <xp:dominoDocument var="document1" action="openDocument"
                    formName="CoopInfo" documentId="#{javascript:viewScope.DocId}"
                    ignoreRequestParams="true">
                </xp:dominoDocument>
            </xp:this.data>
            <xp:text escape="true" id="computedField1" value="#{document1.TeacherFirst}"></xp:text>
            <xp:text escape="true" id="computedField2" value="#{document1.TeacherLast}"></xp:text>
            <xp:text escape="true" id="computedField3" value="#{document1.CampusName}"></xp:text>
            <xp:br></xp:br>
            <xp:inputTextarea id="inputTextarea1" value="#{document1.Comments}"></xp:inputTextarea>
            <xp:button id="button2" styleClass="BlueButton" value="View Next Doc">
                <xp:eventHandler event="onclick" submit="true"
                    refreshMode="partial" id="eventHandler5" refreshId="wrapperpanel">
                    <xp:this.action>
                        <xp:actionGroup>
                            <xp:executeScript script="#{javascript:viewScope.DocIdIndex++;}">
                            </xp:executeScript>
                            <xp:actionGroup>
                                <xp:this.condition><![CDATA[#{javascript:viewScope.DocIdIndex >= viewScope.DocIdLength}]]></xp:this.condition>
                                <xp:executeScript>
                                    <xp:this.script><![CDATA[#{javascript:viewScope.DocIdList = new Array();
viewScope.choosepanelvis= true;
viewScope.vieweditpanelvis = false;}]]></xp:this.script>
                                </xp:executeScript>
                            </xp:actionGroup>
                            <xp:actionGroup>
                                <xp:this.condition><![CDATA[#{javascript:viewScope.DocIdIndex < viewScope.DocIdLength}]]></xp:this.condition>
                                <xp:executeScript>
                                    <xp:this.script><![CDATA[#{javascript:var DocId=viewScope.DocIdList[viewScope.DocIdIndex];
viewScope.DocId = DocId;}]]></xp:this.script>
                                </xp:executeScript>
                            </xp:actionGroup>
                        </xp:actionGroup>
                    </xp:this.action>
                </xp:eventHandler>
            </xp:button>
            <xp:button id="button3" styleClass="BlueButton" value="Edit This Record">
                <xp:this.rendered><![CDATA[#{javascript:if (document1.isEditable()) {
    return false;
} else {
    return true;
}}]]></xp:this.rendered>
                <xp:eventHandler event="onclick" submit="true"
                    refreshMode="partial" id="eventHandler8" refreshId="wrapperpanel">
                    <xp:this.action>
                        <xp:actionGroup>
                            <xp:executeScript>
                                <xp:this.script><![CDATA[#{javascript:context.setDocumentMode("document1","edit");}]]></xp:this.script>
                            </xp:executeScript>
                        </xp:actionGroup>
                    </xp:this.action>
                </xp:eventHandler>
            </xp:button>
            <xp:button id="button11" styleClass="BlueButton"
                value="Save Doc and View Next Record">
                <xp:this.rendered><![CDATA[#{javascript:if (document1.isEditable()) {
    return true;
} else {
    return false;
}}]]></xp:this.rendered>
                <xp:eventHandler event="onclick" submit="true"
                    refreshMode="partial" id="eventHandler7" refreshId="wrapperpanel">
                    <xp:this.script><![CDATA[var choice = null;
var comment = null;

for (var i=0; i<document.forms[0].elements.length; i++) { 
    if (document.forms[0].elements[i].name === "#{id:RadioG2}") { 
        if (document.forms[0].elements[i].checked === true) { 
            choice = document.forms[0].elements[i].value;
        }
    }
}

if (choice === "0") {
    for (var i=0; i<document.forms[0].elements.length; i++) { 
        if (document.forms[0].elements[i].name === "#{id:Comments2}") { 
            comment = document.forms[0].elements[i].value;
        }
    }
}

if (comment === "" && choice === "0") {
    alert("You must enter a comment if you are not going to recommend a teacher.");
    return false;
}


]]></xp:this.script>
                    <xp:this.action>
                        <xp:actionGroup>
                            <xp:executeScript>
                                <xp:this.script><![CDATA[#{javascript:var radioGroup1:com.ibm.xsp.component.xp.XspSelectOneRadio = getComponent("radioGroup1").getValue();
document1.replaceItemValue("RatingNum",radioGroup1);
var inputTextarea1:com.ibm.xsp.component.xp.XspInputTextarea = getComponent("inputTextarea1").getValue();
document1.replaceItemValue("Comments",inputTextarea1);
database.updateFTIndex(false); 
viewScope.DocIdIndex++;
}]]></xp:this.script>
                            </xp:executeScript>
                            <xp:actionGroup>
                                <xp:this.condition><![CDATA[#{javascript:viewScope.DocIdIndex >= viewScope.DocIdLength}]]></xp:this.condition>
                                <xp:executeScript>
                                    <xp:this.script><![CDATA[#{javascript:viewScope.DocIdList = new Array();
viewScope.choosepanelvis= true;
viewScope.vieweditpanelvis = false;}]]></xp:this.script>
                                </xp:executeScript>
                            </xp:actionGroup>
                            <xp:actionGroup>
                                <xp:this.condition><![CDATA[#{javascript:viewScope.DocIdIndex < viewScope.DocIdLength}]]></xp:this.condition>
                                <xp:executeScript>
                                    <xp:this.script><![CDATA[#{javascript:var DocId=viewScope.DocIdList[viewScope.DocIdIndex];
viewScope.DocId = DocId;
}]]></xp:this.script>
                                </xp:executeScript>
                            </xp:actionGroup>
                        </xp:actionGroup>
                    </xp:this.action>
                </xp:eventHandler>
            </xp:button>
            <xp:radioGroup id="radioGroup1" value="#{document1.RatingNum}">
                <xp:selectItem itemLabel="Highly Recommended"
                    itemValue="0">
                </xp:selectItem>
                <xp:selectItem itemLabel="Recommended" itemValue="1"></xp:selectItem>
                <xp:selectItem itemLabel="Not Recommended" itemValue="2"></xp:selectItem>
            </xp:radioGroup>
        </xp:panel>
    </xp:panel>
</xp:view>
2

2 Answers

0
votes

context.setDocumentMode() will set the edit/read mode of the most local XPages dominoDocument. So that will only work within the specific row that has the document whose edit mode you want to control. As soon as you're on a component outside that row, it no longer has access to that specific dominoDocument datasource. Similarly from the button where you're setting edit mode, I think you could also use document1.setEditable(true() to switch to edit mode. But you would not be able to affect that particular document1 datasource's edit mode from outside.

You may be able to get a handle on the datasources by using .getData().get() as detailed in this wiki article, where the index passed in the get() method is the relevant row index. However, with a ViewPanel that may not work, because it's a repeating control, and the relevant datasource is only created at the time that row is being processed and dumped from memory afterwards.

A possible alternate approach may be to set a viewScope variable with the relevant row's document UNID currently edited. As soon as an attempt is made to edit another row's document, then replace the viewScope variable, so it only ever has a single row. Then cross-reference that viewScope variable in your save code, and abort if the UNIDs don't match. That's typically an approach I've used when editing within any kind of repeating control (repeat control, view panel, data view etc).

0
votes

I continued to work with the simplified code I submitted for this program. The code defines 4 panels:

  1. a button panel (buttonpanel)
  2. a wrapper panel (wrapperpanel) The wrapperpanel contains
  3. a panel (listpanel) with a viewpanel that allows the user to choose records using a checkbox. 4 a panel (vieweditpanel) that views the selected records and contains a button that allows the user to put the document in edit mode. This panel has the datasource definition. When the program loads, the listpanel and vieweditpanel are hidden. The button in the button panel makes the listpanel visible. The user chooses the documents and clicks a button that hides the listpanel and makes the vieweditpanel visible with the document in read mode. All my code for saving and loading the chosen documents worked fine - except in the instance where a user would
  4. place a document in edit mode
  5. immediately clicks on button in the button panel (hiding the vieweditpanel and making the listpanel visible).
  6. choose the same document they had put in edit mode from the viewpanel and click the button that hides the listpanel and makes the vieweditpanel visible.

In this case the document would open in edit mode.

I solved the problem by adding the following code to the button in the button panel: viewScope.DocIdList = new Array(); viewScope.DocId = null;

Descriptions of my debugging work follow - but as I say at the end, I am still not sure why the fix worked. As I described in my original post, I tried putting the document in read mode in many different places, but as Paul explained the XSP document was changing underneath me - so to speak.

Finally, I allowed all the panels to be visible and added a computed field that displayed the value of the currentDocument in the viewedit panel. This actually confused me more because 1. I discovered there was an XSP (currentDocument) document defined in veiweditpanel from the very beginning of the program loading. And it was in edit mode. No information was displayed since it was not associated with a backend document - I guess. Then I chose a document using the viewpanel in the listpanel and woudl display it in the viewedit panel. It would be in read mode.
2 (second confusing thing). The currentDocument would change when I would put it in edit mode.

Utimately, I fixed the problem by in the manner described at the top of the post. Still not exactly sure why it worked.