Datatables Object ID

Datatables Object ID

KryzKryz Posts: 1Questions: 1Answers: 0
edited April 2019 in Free community support

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>

Answers

  • allanallan Posts: 64,015Questions: 1Answers: 10,555 Site admin

    Do you mean that d.nationality is an number - i.e. the id for the nationality record? Is the actual text for the nationality available in the data set (we'd need a test case to know for sure, as requested in the forum rules)? If so, then reference that text. If it isn't then you'd need to include that in the data feed from the server - probably using a left join, but it will depend on what you are using on the server.

    Allan

This discussion has been closed.