Take background images in OptionsWidget

21 views
Skip to first unread message

raj...@gmail.com

unread,
Apr 28, 2014, 3:02:55 PM4/28/14
to web...@googlegroups.com, ami...@mail.ru
I have a tables

db.define_table('provider',
Field('name'),
Field('image', type='upload'),
format='%(name)s',)

db.define_table('payment',
Field('provider', 'reference provider'),
...

how i can use "SQLFORM.widgets.options.widget"
def index():
db.payment.provider.widget = lambda field, value: \
SQLFORM.widgets.options.widget(field, value, _style='background-image:url(%s);' % field.image)

to get something like this

<select>
  <option style="background-image:url(provider1.png);">Provider1</option>
  <option style="background-image:url(provider2.png);">Provider2</option>
<option style="background-image:url(provider3.png);">Provider3</option>
</select>
Thanks.

Massimo Di Pierro

unread,
Apr 28, 2014, 5:32:42 PM4/28/14
to web...@googlegroups.com, ami...@mail.ru
The best way is just to give it a class

SQLFORM.widgets.options.widget(field, value, _class="myclass" % field.image)
and then use JS (something like this):

jQuery(function(){
    jQuery('.myclass option').each(function() { 
        var image=jQuery(this).html()+'.png'
        jQuery(this).css('background-image','url('+image+')');
   });
});

raj...@gmail.com

unread,
Apr 29, 2014, 5:26:41 AM4/29/14
to web...@googlegroups.com
In current time i rewrote mede a simple widget 
but this widget work only for firefox

class MyOptionsWidget(FormWidget):

    @staticmethod
    def has_options(field):
        """
        checks if the field has selectable options

        :param field: the field needing checking
        :returns: True if the field has options
        """

        return hasattr(field.requires, 'options')

    @classmethod
    def widget(cls, field, value, **attributes):
        """
        generates a SELECT tag, including OPTIONs (only 1 option allowed)

        see also: :meth:`FormWidget.widget`
        """
        default = dict(value=value)
        attr = cls._attributes(field, default,
                               **attributes)

        options = db().select(db.provider.ALL)
        opts = []
        for option in options:
            opts.append(OPTION(option.name, _value=option.id,
                               _style='background-image:url("%s"); '
                                      'background-repeat : no-repeat; padding-left: 20px;' %
                                      URL('download', args=option.image)))

        return SELECT(*opts, **attr)

вторник, 29 апреля 2014 г., 2:32:42 UTC+5 пользователь Massimo Di Pierro написал:
Reply all
Reply to author
Forward
0 new messages