vendredi 10 juin 2016

web2py datatables time format


I am using web2py and integrating datatables to get a nice responsive table for my data. One of the fields coming from MySQL is a timestamp (datetime) field. When I pull it into datatables I need to serialize it because it can't be formatted as json.

I am using these two functions to produce the data:

def json_serial(obj):

    if isinstance(obj, datetime.datetime):
        serial = obj.isoformat()
        return serial
    raise TypeError ("Type not serializable")

def index():
    allUsers = json.dumps(db(db.userInfo).select().as_list(), default=json_serial)
    return dict(allUsersList=XML(allUsers))

I am them using the jquery configuration below to produce the datatable:

$(document).ready(function(){
    $("#allUser-table").DataTable({
        "scrollY": calcDataTableHeight(),
        "scrollCollapse": true,
        "lengthMenu": [ [50, 100, 250, 500, -1], [50, 100, 250, 500, "All"] ],
        "order": [ 2, 'asc' ],
        fixedHeader: {
            header: true,
            footer: false
        },
        data:  {{=allUsersList}},
        columns: [
            { data: 'userID' },
            { data: 'userName' },
            { data: 'jabberVersion' },
            { data: 'lastSeen' , "type": "date" , "dateFormat": "yy-mm-dd" }
        ]
    })
});

The column with the datetime is in this format:
2016-06-10T14:41:40

How can I get datatables to format the datetime into something a bit more readable? It's obviously not changing with the configuration I've tried above.


Aucun commentaire:

Enregistrer un commentaire