#!/usr/bin/env python # coding: utf8 from gluon.html import * from gluon.http import * from gluon.validators import * from gluon.sqlhtml import * # request, response, session, cache, T, db(s) # must be passed and cannot be imported! class JSONDATATABLE(DIV): def __init__(self, dataUrl, editCallbackMethod, menuCallbackMethod, columns = [], **attributes): DIV.__init__(self, **attributes) hidden = [] colDef = [] col = [] for field in columns: if field.get('hidden', False) != True: line = ['key:"' + field['key'] +'"', 'label: "' + str(field.get('label', field['key'] )) + '"', 'sortable:' + field.get('sortable', 'false'), 'formatter: ' + field.get('formatter', 'YAHOO.widget.DataTable.formatDefault')] #if field.get('width', None): # line.append('width:"' + field['width'] +'"') colDef.append( '{' + ', '.join(line) + '}') else: hidden.append( '"' + field['key'] +'"' ) col.append( '"' + field['key'] +'"' ) scriptText = 'YAHOO.util.Event.onContentReady("' + self.attributes['_id'] + '", function() { \n' scriptText += '\tthis.myColumnDefs = [' + ',\n'.join(colDef) + '];\n' scriptText += '\tthis.responseSchema = { \n resultsList: "result", \n fields: [' + ','.join(col) + '], \n metaFields: { totalRecords: "totalRecords", recordsReturned : "recordsReturned", startIndex : "startIndex", sort : "sort", dir : "dir" } \n }; \n' scriptText += '\tthis.hideColumns = [' + ','.join(hidden) + ' ]; \n ' scriptText += '\tthis._table = new JsonDataTable( "' + self.attributes['_id']+ '", "' + dataUrl + '", this.myColumnDefs, this.responseSchema, this.hideColumns ); \n ' scriptText += '\tthis._table.editOpperation = ' + editCallbackMethod + ';\n' scriptText += '\tthis._table.setupContextMenu(' + menuCallbackMethod + ');\n' scriptText += '\tsetInterval( function(){ DataTable.UpdateCallback(document.getElementById("' + self.attributes['_id']+ '")._table)}, 10000 ); \n ' scriptText += '}); \n' self.insert(0, SCRIPT( scriptText, _language='javascript' ) ) def _postprocessing(self): if '_class' in self.attributes: self.attributes['_class'] += ' json-datatable' else: self.attributes['_class'] = 'json-datatable' if '_id' not in self.attributes: self.attributes['_id'] = 'json-datatable-' + web2py_uuid()