How can I set focus to Edit Box inside repeat control?

☆樱花仙子☆ 提交于 2019-12-12 20:42:53

问题


I would like to set focus + place cursor to an Edit Box (the last one) within a repreat control. The repeat is inside a panel (panelRep). I then have a button outside the panel.

This is the client side code for the button which almost works.. Focus is set (blue border around field), but cursor is not placed in field. User must still click the field to be able to write input.

Example without focus:

Example with focus:

Client side code for button which sets focus to last Edit Box in which id contains the string inputKode:

try {
var el = dojo.query('div[id*="inputKode"]');
var node = el[el.length-1];
setTimeout(function() { node.focus(); }, 500);
//node.focus();
} catch (e) { } 

Code for panelRep:

<xp:panel id="panelRep">
            <xp:repeat id="repeat1" rows="12" var="row" indexVar="idx"
                value="#{view1}" repeatControls="false">
                <xp:panel id="panelLinje">
                    <xp:this.data>
                        <xp:dominoDocument formName="frmPBudKodeVerdi"
                            var="dsdoc" action="editDocument" computeWithForm="both"
                            documentId="#{javascript:row.getUniversalID();}">                           
                        </xp:dominoDocument>
                    </xp:this.data>
                    <xp:table style="width:800.0px">
                        <xp:tr>
                            <xp:td style="width:100px">
                                <xp:inputText id="inputKode"
                                    value="#{dsdoc.KodeNr}" style="width:62px">
                                    <xp:this.attrs>
                                        <xp:attr name="tabindex"
                                            value="#{javascript:return idx + '1';}">
                                        </xp:attr>
                                    </xp:this.attrs>
                                    <xp:typeAhead mode="partial"
                                        minChars="1" var="lukey" valueMarkup="true" id="typeAhead1">
                                        <xp:this.valueList><![CDATA[#{javascript://var type = compositeData.type;
                                        return TypeAheadKode2(sessionScope.type,lukey);
                                        }]]></xp:this.valueList>    
                                    </xp:typeAhead>
                                    <xp:eventHandler event="onchange"
                                        submit="true" refreshMode="partial" refreshId="panelLinje">
                                        <xp:this.action><![CDATA[#{javascript:onChangeKode();}]]></xp:this.action>
                                    </xp:eventHandler>
                                </xp:inputText>                             
                            </xp:td>
                            <xp:td style="width:450px">
                                <xp:inputText id="inputNavn"
                                    value="#{dsdoc.KodeNavn}" style="width:440px"
                                    readonly="true">
                                </xp:inputText>
                            </xp:td>
                            <xp:td style="width:60px">
                                <xp:inputText id="inputNorm"
                                    style="width:45px" value="#{dsdoc.NormPrProd}"
                                    rendered="#{javascript:viewScope.visNorm}" readonly="true">
                                    <xp:this.attrs>
                                        <xp:attr name="tabindex"
                                            value="#{javascript:return idx + '2';}">
                                        </xp:attr>
                                    </xp:this.attrs>
                                    <xp:this.converter>
                                        <xp:convertNumber
                                            type="number">
                                        </xp:convertNumber>
                                    </xp:this.converter>
                                </xp:inputText>
                            </xp:td>
                            <xp:td style="width:50px">
                                <xp:inputText id="inputAntall"
                                    style="width:45px" value="#{dsdoc.NormAntall}"
                                    rendered="#{javascript:viewScope.visNorm}">
                                    <xp:this.converter>
                                        <xp:convertNumber
                                            type="number">
                                        </xp:convertNumber>
                                    </xp:this.converter>
                                    <xp:eventHandler
                                        event="onchange"
                                        submit="true"
                                        refreshMode="partial"
                                        refreshId="inputTimer">
                                        <xp:this.action><![CDATA[#{javascript:onChangeAntall()}]]></xp:this.action>
                                    </xp:eventHandler>
                                    <xp:this.attrs>
                                        <xp:attr name="tabindex"
                                            value="#{javascript:return idx + '3';}">
                                        </xp:attr>
                                    </xp:this.attrs>
                                </xp:inputText>
                            </xp:td>
                            <xp:td
                                style="width:50px;text-align:right">
                                <xp:inputText id="inputTimer"
                                    value="#{dsdoc.Timer}" style="width:45px;text-align:right">
                                    <xp:this.converter>
                                        <xp:convertNumber
                                            type="number">
                                        </xp:convertNumber>
                                    </xp:this.converter>
                                    <xp:this.attrs>
                                        <xp:attr name="tabindex"
                                            value="#{javascript:return idx + '4';}">
                                        </xp:attr>
                                    </xp:this.attrs>
                                </xp:inputText>
                            </xp:td>
                            <xp:td>                             
                            </xp:td>
                        </xp:tr>
                    </xp:table>
                    <xp:eventHandler event="onClientLoad" submit="true"
                        refreshMode="norefresh">
                    </xp:eventHandler>
                </xp:panel>
            </xp:repeat>
</xp:panel>

回答1:


Update 22.09.2012:

@MarkyRoden - Thanks for pointing me in the right direction.

After refining the dojo.query selector, I ended up with 1 line of code.

var el = dojo.query('div[id*="inputKode"] .dijitInputField > input').at(-1)[0].focus(); 



Original post:

I found out that the element being set focus on was not an INPUT element. Element id started with widget_

E.g widget_view:_id1:_id2:_id3:repeat1:8:inputKode if there are 8 rows in the repeat

I then discovered that the nodeType of the element was DIV By viewing the element.innerHTML, I discovered that it had multiple children.

I tried to use element.querySelector or element.querySelectorAll, but I couldn't get them to work for the element, so I ended up looping through element.childNodes. Not very pretty, but it does the work for now..

I'm sure it can be done much nicer by adding elements to the dojo.query selector, or by using jquery. Have to look into that later..

Well, here's the code I put in the onClientLoad event for my CC:

var el = dojo.query('div[id*="inputKode"]');
var node = el[el.length-1];
node.focus();

var activeElementId = document.activeElement.id;
var activeElement = dojo.byId( activeElementId );
var kids = activeElement.childNodes;

for(var i=0; i < kids.length; i++)
{
    if(kids[i].className == 'dijitReset dijitInputField dijitInputContainer')
    {
    var elementDiv = kids[i];
    var elementDivKids = elementDiv.childNodes;

    for(var j=0; j < elementDivKids.length; j++)
    {
        var elementInput = elementDivKids[j];
        elementInput.focus();
    }
    } 
}

regards, Petter



来源:https://stackoverflow.com/questions/12459512/how-can-i-set-focus-to-edit-box-inside-repeat-control

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!