I found out how to pre-select the values of the multipleSelect.
It's quite easy,
My form is
----------------------------
def getDbGroups():
res = dbGroup.select().orderBy("group_name")
options = [ (group.id, group.group_name) for group in res ]
return options
def getDbEstabs():
res = dbEstab.select().orderBy("nombre")
options = [ (estab.id, estab.nombre) for estab in res ]
return options
def getDbCargos():
res = dbCargo.select().orderBy("nombre")
options = [ (cargo.id, cargo.nombre) for cargo in res ]
return options
class ContactoModifyForm(widgets.WidgetsList):
hidden_user_id = widgets.HiddenField(
name = 'user_id')
hidden_contacto_id = widgets.HiddenField(
name = 'contacto_id')
user_name = widgets.TextField(
name = 'user_name',
label = 'Nombre',
attrs={'size': 32, 'maxlength': 255})
user_surname = widgets.TextField(
name = 'user_surname',
label = 'Apellido',
attrs={'size': 32, 'maxlength': 255 })
display_name = widgets.TextField(
name = 'display_name',
label = 'Nick',
attrs={'size': 32, 'maxlength': 255})
establecimiento = widgets.SingleSelectField(
name = "establecimiento",
label = 'Establecimiento',
options = getDbEstabs,
validator = v.All())
nuevo_estab = widgets.Button(
name = "nuevo_estab",
label = '',
attrs = {'value': "Insertar nuevo
establecimiento",
'onclick' : "javascript:popUp('/
establecimiento/insert?popUp=1')"})
cargos = widgets.MultipleSelectField(
label = 'Cargo',
name = 'cargo',
options = getDbCargos,
validator = v.All())
nuevo_cargo = widgets.Button(
label = '',
attrs =
{'id':'nuevo_cargo_button',
'value': "Insertar
nuevo cargo",
'onclick' : "javascript:popUp('/
cargo/insert?popUp=1')"},
name = 'nuevo_cargo'
)
email_address_contacto = AppendableFormFieldList(
label='Email contacto',
fields=[widgets.TextField(name='email_address_contacto',label='',attrs
= {'size':32,'maxlength':64})]
)
telefono_contacto = AppendableFormFieldList(
label = 'Telefono contacto',
fields = [widgets.TextField(name =
'telefono_contacto', label = '')]
)
fax_contacto = AppendableFormFieldList(
label = 'Fax contacto',
fields = [widgets.TextField(name =
'fax_contacto', label = '')]
)
modify_contacto_form= widgets.TableForm(fields=ContactoModifyForm())
---------------------
the important part of the template is
-----------------------
<div py:content="form.display(value=valores, method=method,
action=action, submit_text=submit_text,attrs=attrs )">Form</div>
--------------------------
and the controller is
##################################
@expose(template='intranet.templates.agenda.insert_estab')
def insert(self, id = 0, popUp = 0, *args, **kw):
JSLink = widgets.JSLink('contacto_js',
'radio_onclick.js');
try:
contacto = dbContacto.get(id)
page_title = 'Modificar contacto'
valores = {'user_name':
contacto.user.user_name,
'user_surname':contacto.user.user_surname,
'display_name':contacto.user.display_name,
'establecimiento':
contacto.establecimientoID,
'email_address_contacto':
[ {'email_address_contacto':email.email}
for email in contacto.user.email_address],
'telefono_contacto':
[ {'telefono_contacto':telefono.numero} for
telefono in contacto.user.telefono],
'cargo': [ cargo.id for cargo
in contacto.cargo],
'fax_contacto':
[{'fax_contacto':fax.numero} for fax in contacto.user.fax]}
attrs = dict(display_name=
{'readonly':'readonly'},
user_name =
{'readonly':'readonly'},
user_surname =
{'readonly':'readonly'},
user_id =
{'value':contacto.userID},
contacto_id =
{'value':contacto.id})
submit_text = 'Aplicar cambios'
volver_url = 'javascript:history.back()'
page_title = 'Modificar contacto'
method = 'post'
return dict(form = modify_contacto_form,
valores = valores,
method = 'post',
action = 'save',
submit_text = submit_text,
attrs = attrs,
JSLink = JSLink,
popUp = popUp,
page_title = page_title)
except:
valores = {}
submit_text = 'Insertar Nuevo contacto'
attrs = {}
page_title = 'Ingresar Contacto'
volver_url = './'
return dict(form = insert_contacto_form,
valores = valores,
method = 'post',
action = 'save',
submit_text = submit_text,
attrs = attrs,
JSLink = JSLink,
popUp = popUp,
page_title = page_title)
######################################3
in short words, in your controller, where you've got the id of the
record (and eventually all the information you need), you have to
pass
a dict of dicts to the .kid, where the field of
the dict ( in this case 'cargo') should be the same as the name of the
widget. As you can see in the form.....
cargos = widgets.MultipleSelectField(
label = 'Cargo',
name = 'cargo', <-----
options = getDbCargos,
validator = v.All())
and in the controller
valores = {....., 'cargo': [ cargo.id for cargo in
contacto.cargo],.....}
and in the .kid
<div py:content="form.display( value=valores, <-----------
method=method,
action=action,
submit_text=submit_text,
attrs=attrs )">Form</
div>
hope it helps.
--
miya
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---