Since you are using datatable you do not have mach choice and you have to
handle this serverside. You want the json to contain the nationality
description instead of the nationality id. This can be done in the
controller.
Before you do that two observation: User is a reserved keywork so I am
going to use Person. Table Nationality should be defined before table
Person to avoid trouble. So, given:
db.define_table('Person',
Field('first_name', 'string'),
Field('last_name', 'string'),
Field('email','string'),
Field('username','string'),
Field('nationality','reference Nationality', requires =
IS_IN_DB(db,db.Nationality.id,'%(description)s')
)
db.define_table('Nationality',
Field('description','string'),
format = '%(description)s'
)
You can do:
def user():
import json
rows = jdb(db.Person.id>0).select()
ids = [row.nationality for row in rows] # find the nationalities
mapping = db(db.Nationality.id.belongs(ids)).select().as_dict() # build
a mapping in one extra query
for row in rows: row.nationality=mapping[row.nationality].description #
applly the mapping
return dict(formListar=XML(rows.as_json()))
On Friday, 12 April 2019 20:58:36 UTC-7, Cristina Sig wrote:
> Hello everybody,
>
> I have two tables and I am working with Datatables and what I would like
> to do is use the child row to show more information about a particular row.
> For example, on the table I will display first name, last name, username,
> and email and on the child or expandable row, I will show the nationality
> of that user.
> The problem I am having is that when I expand the row, it shows only the
> id from the 'User' table and not the description associated to that id.
> this is my first time dealing with Datatables so I don't have my
> experience with it.
>
> Any suggestions to solve this issue?
>
> Thanks :)
>
> DB
> db.define_table('User',
> Field('first_name', 'string'),
> Field('last_name', 'string'),
> Field('email','string'),
> Field('username','string'),
> Field('nationality','reference Nationality', requires =
> IS_IN_DB(db,db.Nationality.id,'%(description)s')
> )
>
>
> db.define_table('Nationality',
> Field('description','string'),
> format = '%(descripcion)s'
> )
>
>
>
> My controler
> def user():
> import json
> usuario = json.dumps(db(db.auth_user.id>0).select().as_list())
> return dict(formListar=XML(usuario))
>
>
>
>
> My view
> <script>
> var tabla;
> $(document).ready(function(){
> tabla= $('#tablaGenerica').DataTable({
> "data": {{=formListar}},
> "scrollX": false,
> "dom": 'lrtip',
> "searching": true,
> "sRowSelect": "single",
> "columns": [
> {
> "class":"details-control",
> "orderable":false,
> "data":null,
> "defaultContent": ""
> },
> { data: 'first_name' },
> { data: 'last_name' },
> { data: 'email' },
> { data: 'username' },
> ]
> });
>
> $('#tablaGenerica tbody').on('click', 'td.details-control', function () {
> var tr = $(this).closest('tr');
> var row = tabla.row( tr );
> if ( row.child.isShown() ) {
> // This row is already open - close it
> row.child.hide();
> tr.removeClass('shown');
> }
> else {
> // Open this row
> row.child( format(row.data()) ).show();
> tr.addClass('shown');
> }
> } );
>
> function format ( d ) {
> // `d` is the original data object for the row
> return '<table cellpadding="5" cellspacing="0" border="0"
> style="padding-left:50px;">'+
> '<tr>'+
> '<td>Nationality:</td>'+
> '<td>'+d.nationality+'</td>'+
> '</tr>'+
> '</table>';
> }
> </script>
>
>
>
> <table id="tablaGenerica" class="tablaC table-striped hover cell-border"
> cellspacing="0" width="100%" >
> <thead>
> <tr>
> <th></th>
> <th>First name</th>
> <th>Last name</th>
> <th>Email</th>
> <th>Username</th>
> </tr>
> </thead>
> </table>
>
>
>
>
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.