> I am fresh to tg-widgets  , i want my table in my desired format, how
> can i apply the css and do the formats.

Hi Antony, you have three choices ( that I know of ). You can pass css
attributes into the field widgets for id and class and style them, or
you can use a custom template, or you can make your own widget class and
include the custom template in it.

To pass in a custom attribute on the individual field widgets do:
w = widgets.TextArea( attrs={'id':'foo'}, css_classes=['red','middle'] )

which will produce:
<textarea rows="7" cols="50" class="textarea red middle" name="widget"
id="foo"></textarea>

or for the table and form itself:
t = widgets.TableForm(table_attrs={'id':'foo', 'class':'mytableclass'},
form_attrs={'id':'spam', 'class':'myformclass'})

producing:
<form class="myformclass" id="spam" name="form" method="post">
<table cellpadding="2" cellspacing="0" border="0" id="foo"
class="mytableclass">
<tr><td>\xc2\xa0</td>
<td><input type="submit" class="submitbutton"></td>
</tr></table></form>

The best way to muck around with that is in the tg-admin python shell.
If you make a widget and then type it's name you'll see a long form of
the contructor pop up:

In [56]: t = widgets.TextArea()

In [57]: t
Out[57]: TextArea(name='widget', convert=True, rows=7, cols=50,
attrs={}, css_classes=[], field_class='textarea')


You can also use the intepreter to grab the template from the table from
and make your own. That way you can really change the layout. And
lastly, if that's enough you can subclass TableForm and put in your own
templates and extra widget variables. 

I've attached two widgets of my own, one simple one complicated.
Attached as my email client screws up the formatting something fierce!

Iain



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

# Edit widget:
class CrudEditWidget(w.TableForm):
    params = ['object', 'Model', 'parent_url' ]
    template ="""
        <form xmlns:py="http://purl.org/kid/ns#"; name="${name}" action="${action}" 
          method="${method}" class="tableform crud_${Model._name}" py:attrs="form_attrs">
            <div py:for="field in hidden_fields" py:replace="field.display(value_for(field), **params_for(field))" />
            <table py:attrs="table_attrs">
              <h3 colspan="2" py:if="value">Editing ${Model._name.capitalize()}</h3>
              <h3 colspan="2" py:if="value==None">Create New</h3>
              <tr py:for="i, field in enumerate(fields)" class="${i%2 and 'odd' or 'even'}">
                <th><label class="fieldlabel" for="${field.field_id}" py:content="field.label" /></th>
                <td><span py:replace="field.display(value_for(field), **params_for(field))" />
                    <span py:if="error_for(field)" class="fielderror" py:content="error_for(field)" />
                    <span py:if="field.help_text" class="fieldhelp" py:content="field.help_text" />
                </td></tr>
                <tr><td>&#160;</td>
                  <td>
                    ${submit.display(submit_text)}
                    <a href="${parent_url}/${Model._name}">
                    <span class="crud_button">Back To ${Model._name.capitalize()} List</span>
                    </a>
                  </td>
                </tr>
             </table>
        </form>"""      


# Booking staff widget, manual field generation for now:
class BookingStaffWidget(w.TableForm):
    params = [ 'booking', 'Model', 'parent_url' ]
    template ="""
        <form xmlns:py="http://purl.org/kid/ns#"; name="${name}" action="${action}" 
          method="${method}" class="tableform crud_${Model._name}" py:attrs="form_attrs">
            <table id="booking_summary">
                <th>Booking: 
                    <strong class="warning" py:if="booking.staff_needed &gt; booking.staff_booked">
                        This booking is understaffed</strong>
                    <strong class="warning" py:if="booking.staff_needed &lt; booking.staff_booked">
                        This booking is overstaffed</strong>
                </th>
                <tr><td>Date:</td><td> ${booking.date}</td></tr>
                <tr><td>Client:</td><td> ${booking.client}</td></tr>
                <tr><td>Category:</td><td> ${booking.category}</td></tr>
                <tr><td>Size:</td><td> ${booking.size}</td></tr>
                <tr><td>Staff Needed:</td><td> ${booking.staff_needed}</td></tr>
            </table>
            <table py:attrs="table_attrs">
              <tr py:for="i, staff_member in enumerate( get_staff(booking) )">
                <td>Staff #${i+1} : 
                  <select name="staff_${i}"> 
                    <div py:for="staff_option in staff_options()" py:strip="True">
                      <option py:if="staff_option['id']!=staff_member['id']" value="${staff_option['id']}">
                        ${staff_option['name']}</option>
                      <option py:if="staff_option['id']==staff_member['id']" value="${staff_option['id']}" selected="selected">
                        ${staff_option['name']}</option>
                    </div>
                  </select>
                </td>    
              </tr>
              <tr><td><input type="submit" value="Update Staffing"/></td></tr>
             </table>
        </form>"""      


    def staff_options(self):
        option_list = [ {'id':0, 'name':'None' } ]
        option_list += [ {'id':s.id, 'name':"%s, %s"%(s.name_last,s.name_first) } for s in Staff.select() ]
        return option_list
        
    # method to a return a list tuples of current staff, or empty slots
    def get_staff(self, booking):
        # the number of slots should be the greater of staff needed or staff on it now ( in case of size reduction )
        if len( booking.staff ) > booking.staff_needed: num_slots = len(booking.staff)
        else: num_slots = booking.staff_needed
        staff_list = []
        for i in range(0, num_slots ):
            try:
                staff_list.append( {'id':booking.staff[i].id, 'name':booking.staff[i] } )
            except:
                staff_list.append( {'id':0, 'name':'None'} )
        return staff_list
    
    def update_params(self, params):
        super(BookingStaffWidget, self).update_params(params)
        params['staff_options'] = self.staff_options
        params['get_staff'] = self.get_staff 

Reply via email to