setvalue within a repeat using current() or index()

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

setvalue within a repeat using current() or index()

chrisswick
Hi,

I have a setvalue nested inside a repeat, where a trigger will set a
value relative to the current index of the repeat. This works fine
inside the setvalue's value attribute using the index() function, as
long as you refer to the repeat's instance from the top.

My repeat also has an xpath expression which filters the repeat. When
the repeat is filtered, the index() of my repeat is not updated, where
using the current() function I can refer to the correct filtered
result. However the current() function cannot be called from the
setvalue since the value attribute's context of a setvalue is taken
from the setvalue's ref attribute. Below is my example, where if you
filter the repeat with titles that start with 'M', and you select the
'Mahogany' title, the 'Oak' title actually gets used in the setvalue.

Is there any way to have the context of the setvalue refer to a
repeat's current position? If not, any workaround?

Thanks,
Chris

EXAMPLE:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/
2002/xforms"
  xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xs="http://
www.w3.org/2001/XMLSchema">

        <head>
                <xf:model>

                        <xf:instance id="data" xmlns="">
                                <data>
                                        <collection id="1">
                                                <title>Maple</title>
                                        </collection>
                                        <collection id="2">
                                                <title>Oak</title>
                                        </collection>
                                        <collection id="3">
                                                <title>Ash</title>
                                        </collection>
                                        <collection id="4">
                                                <title>Mahogany</title>
                                        </collection>
                                </data>
                        </xf:instance>

                        <xf:instance id="search" xmlns="">
                                <search>
                                        <filter />
                                </search>
                        </xf:instance>

                        <xf:instance id="selected-data" xmlns="">
                                <data>
                                        <item />
                                </data>
                        </xf:instance>

                </xf:model>

        </head>
        <body>
                <xf:input ref="instance('search')/filter" incremental="true">
                        <xf:label>Filter by Title: </xf:label>
                </xf:input>

                <strong><xf:output ref="instance('selected-data')/item">
                        <xf:label>Selected Item: </xf:label>
                </xf:output></strong>
                <br/>
                <hr/>

                <xf:repeat nodeset="instance('data')/collection[title[starts-with(.,
instance('search')/filter)]]" id="data-list">
                        <xf:trigger>
                                <xf:label>Pick</xf:label>
                                <xf:action ev:event="DOMActivate">
                                        <xf:setvalue ref="instance('selected-data')/item"
value="instance('data')/collection[index('data-list')]/title" />
                                        <xf:message level="modal">Selected Item: <xf:output
value="current()/title" /></xf:message>
                                </xf:action>
                        </xf:trigger>
                        <xf:output ref="attribute::id" />
                        <xf:output ref="title" />
                        <hr/>
                </xf:repeat>
        </body>
</html>

_______________________________________________
dev-tech-xforms mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xforms
Reply | Threaded
Open this post in threaded view
|

Re: setvalue within a repeat using current() or index()

Aaron Reed
Hi Chris,

Might be a good question for the W3C mailing list.  Maybe someone has
already done something like this that could give you some pointers.

--Aaron

[hidden email] wrote:

> Hi,
>
> I have a setvalue nested inside a repeat, where a trigger will set a
> value relative to the current index of the repeat. This works fine
> inside the setvalue's value attribute using the index() function, as
> long as you refer to the repeat's instance from the top.
>
> My repeat also has an xpath expression which filters the repeat. When
> the repeat is filtered, the index() of my repeat is not updated, where
> using the current() function I can refer to the correct filtered
> result. However the current() function cannot be called from the
> setvalue since the value attribute's context of a setvalue is taken
> from the setvalue's ref attribute. Below is my example, where if you
> filter the repeat with titles that start with 'M', and you select the
> 'Mahogany' title, the 'Oak' title actually gets used in the setvalue.
>
> Is there any way to have the context of the setvalue refer to a
> repeat's current position? If not, any workaround?
>
> Thanks,
> Chris
>
> EXAMPLE:
> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/
> 2002/xforms"
>   xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xs="http://
> www.w3.org/2001/XMLSchema">
>
> <head>
> <xf:model>
>
> <xf:instance id="data" xmlns="">
> <data>
> <collection id="1">
> <title>Maple</title>
> </collection>
> <collection id="2">
> <title>Oak</title>
> </collection>
> <collection id="3">
> <title>Ash</title>
> </collection>
> <collection id="4">
> <title>Mahogany</title>
> </collection>
> </data>
> </xf:instance>
>
> <xf:instance id="search" xmlns="">
> <search>
> <filter />
> </search>
> </xf:instance>
>
> <xf:instance id="selected-data" xmlns="">
> <data>
> <item />
> </data>
> </xf:instance>
>
> </xf:model>
>
> </head>
> <body>
> <xf:input ref="instance('search')/filter" incremental="true">
> <xf:label>Filter by Title: </xf:label>
> </xf:input>
>
> <strong><xf:output ref="instance('selected-data')/item">
> <xf:label>Selected Item: </xf:label>
> </xf:output></strong>
> <br/>
> <hr/>
>
> <xf:repeat nodeset="instance('data')/collection[title[starts-with(.,
> instance('search')/filter)]]" id="data-list">
> <xf:trigger>
> <xf:label>Pick</xf:label>
> <xf:action ev:event="DOMActivate">
> <xf:setvalue ref="instance('selected-data')/item"
> value="instance('data')/collection[index('data-list')]/title" />
> <xf:message level="modal">Selected Item: <xf:output
> value="current()/title" /></xf:message>
> </xf:action>
> </xf:trigger>
> <xf:output ref="attribute::id" />
> <xf:output ref="title" />
> <hr/>
> </xf:repeat>
> </body>
> </html>
>
_______________________________________________
dev-tech-xforms mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xforms
Reply | Threaded
Open this post in threaded view
|

Re: setvalue within a repeat using current() or index()

arabans
Hi Chris,

Try using:

<xf:setvalue ref="instance('selected-data')/item"
                  value="instance('data')/collection[title[starts-
with(.,instance('search')/filter)]][index('data-list')]/title" />

       - the 'value' attribute on the xf:setvalue here uses the same
'nodeset' xpath used in the xf:repeat.

-- Sivaram


On Aug 6, 11:40 am, Aaron Reed <[hidden email]> wrote:

> Hi Chris,
>
> Might be a good question for the W3C mailing list.  Maybe someone has
> already done something like this that could give you some pointers.
>
> --Aaron
>
> [hidden email] wrote:
> > Hi,
>
> > I have a setvalue nested inside a repeat, where a trigger will set a
> > value relative to the current index of the repeat. This works fine
> > inside the setvalue's value attribute using the index() function, as
> > long as you refer to the repeat's instance from the top.
>
> > My repeat also has an xpath expression which filters the repeat. When
> > the repeat is filtered, the index() of my repeat is not updated, where
> > using the current() function I can refer to the correct filtered
> > result. However the current() function cannot be called from the
> > setvalue since the value attribute's context of a setvalue is taken
> > from the setvalue's ref attribute. Below is my example, where if you
> > filter the repeat with titles that start with 'M', and you select the
> > 'Mahogany' title, the 'Oak' title actually gets used in the setvalue.
>
> > Is there any way to have the context of the setvalue refer to a
> > repeat's current position? If not, any workaround?
>
> > Thanks,
> > Chris
>
> > EXAMPLE:
> > <html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/
> > 2002/xforms"
> >   xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xs="http://
> >www.w3.org/2001/XMLSchema">
>
> >    <head>
> >            <xf:model>
>
> >                    <xf:instance id="data" xmlns="">
> >                            <data>
> >                                    <collection id="1">
> >                                            <title>Maple</title>
> >                                    </collection>
> >                                    <collection id="2">
> >                                            <title>Oak</title>
> >                                    </collection>
> >                                    <collection id="3">
> >                                            <title>Ash</title>
> >                                    </collection>
> >                                    <collection id="4">
> >                                            <title>Mahogany</title>
> >                                    </collection>
> >                            </data>
> >                    </xf:instance>
>
> >                    <xf:instance id="search" xmlns="">
> >                            <search>
> >                                    <filter />
> >                            </search>
> >                    </xf:instance>
>
> >                    <xf:instance id="selected-data" xmlns="">
> >                            <data>
> >                                    <item />
> >                            </data>
> >                    </xf:instance>
>
> >            </xf:model>
>
> >    </head>
> >    <body>
> >            <xf:input ref="instance('search')/filter" incremental="true">
> >                    <xf:label>Filter by Title: </xf:label>
> >            </xf:input>
>
> >            <strong><xf:output ref="instance('selected-data')/item">
> >                    <xf:label>Selected Item: </xf:label>
> >            </xf:output></strong>
> >            <br/>
> >            <hr/>
>
> >            <xf:repeat nodeset="instance('data')/collection[title[starts-with(.,
> > instance('search')/filter)]]" id="data-list">
> >                    <xf:trigger>
> >                            <xf:label>Pick</xf:label>
> >                            <xf:action ev:event="DOMActivate">
> >                                    <xf:setvalue ref="instance('selected-data')/item"
> > value="instance('data')/collection[index('data-list')]/title" />
> >                                    <xf:message level="modal">Selected Item: <xf:output
> > value="current()/title" /></xf:message>
> >                            </xf:action>
> >                    </xf:trigger>
> >                    <xf:output ref="attribute::id" />
> >                    <xf:output ref="title" />
> >                    <hr/>
> >            </xf:repeat>
> >    </body>
> > </html>


_______________________________________________
dev-tech-xforms mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xforms
Reply | Threaded
Open this post in threaded view
|

Re: setvalue within a repeat using current() or index()

Dan McCreary
Sivaram - nice solution.  I confirmed it works as expected!

I thought this was a great example so I took the liberty of adding it
to our cookbook:

http://en.wikibooks.org/wiki/XForms/Repeat_filter

I just cleaned up the code a bit.

I gave you what credit I could (I can't see your full e-mail) so feel
free to put full credit for yourself.

Thanks for sharing! - Dan

_______________________________________________
dev-tech-xforms mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xforms