Richard,
Here is my minimal code for returning json data in dataTables via
web2py's ajax function.
---View---
<!-- here's a well-formed table -->
<table id="abc_table">
<thead>
<tr>
<th>ABC Category Name</th>
<th>Code</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
// this I keep in js folder
function dtbl_show(hash_div, sAjaxSource, aoColumns) {
$(document).ready(function() {
$(hash_div).dataTable( {
"bJQueryUI": true,
"bDeferRender": true,
"bPaginate": true,
'sPaginationType': 'full_numbers',
"bProcessing": true,
"bServerSide": true,
"aaSorting": [[ 1, "desc" ]],
"bAutoWidth": false,
"aoColumns" : aoColumns,
"sAjaxSource": sAjaxSource
} );
} );
jQuery.fn.dataTableExt.oPagination.iFullNumbersShowPages = 10;
}
</script>
<script>
// call here this script & pass the table's div for rendering it
dtbl_show("#abc_table", "{{=URL('abc','get_abcdata')}}", [{ sWidth:
'350px' }, { sWidth: '200px' }] )
</script>
--- End View ---
--- Controller for populating data via ajax ---
(excuse the bugged-up indentation here)
def get_abcdata():
if request_vars_iDisplayStart != None:
iDisplayStart = request_vars_iDisplayStart
else:
iDisplayStart = 0
if request_vars_iDisplayLength != None:
iDisplayLength = request_vars_iDisplayLength
else:
iDisplayLength = 10
if request_vars_sEcho != None:
sEcho = int(request_vars_sEcho)
else:
sEcho = 1
qry = 'your sql query string'
### Below this, I am using a 3rd party library viz. DABO for
interacting with DB.
### You may substitute that syntax with the DB layer of your choice
(DAL or whatever else) and get dataset.
try:
conn_name = connInstance.makeConn()
cur = conn_name.cursor()
qry = qry1 + ' limit ' + str(iDisplayStart) + ', ' +
str(iDisplayLength) + ';'
# fetch total data
cur.execute(qry1)
data_full = cur.getDataSet()
iTotalRecords = len(data_full)
# fetch data as requested from client
cur.execute(qry)
data_disp = cur.getDataSet()
iTotalDisplayRecords = len(data_full)
finally:
conn_name.close()
aaData = []
# Now populate the aaData with data in form of lists.
# e.g. aaDada will look like -- [[1, 'Vineet'],[2,'Richard']]
# This formatting is important
D=dict(sEcho=sEcho, iTotalRecords=iTotalRecords,
iTotalDisplayRecords=iTotalDisplayRecords,
iDisplayLength=iDisplayLength, aaData=aaData)
return response.json(D)
-- End of Controller code --
That's wraps it up.
If you need any further drilling down in my code, pl. ask.
I will be happy to share.
HTH,
--- Vineet
On Feb 8, 10:18 pm, Richard Vézina <
ml.richard.vez...@gmail.com>
wrote:
> Yes!
>
> I think the PowerTable of Burno don't implement server side processing
> yet... We maybe can start form there to make a server side implementation
> of PowerTable... What do you think?
>
> I think, we have to split table header from data then wrote a python
> implementation fo the php script as shown in the example of DTs...
>
> For sure see a bit of your code could help me figure out the path I should
> follow and help my reflexion.
>
> About "fnInitComplete" it is just needed in case you want to initialise DTs
> with fixed columns...
>
> Richard
>