'Get a data table
@param r: the S3Request
@param attr: parameters for the method handler'

def _datatable(self, r, **attr):

    authorised = self._permitted()
    if (not authorised):
        r.unauthorised()
    resource = self.resource
    get_config = resource.get_config
    linkto = get_config('linkto', None)
    list_id = attr.get('list_id', 'datatable')
    list_fields = resource.list_fields()
    orderby = get_config('orderby', None)
    response = current.response
    s3 = response.s3
    representation = r.representation
    get_vars = self.request.get_vars
    if (representation == 'aadata'):
        (start, limit) = self._limits(get_vars)
    else:
        start = None
        limit = (None if s3.no_sspag else 0)
    output = {}
    if (not linkto):
        linkto = self._linkto(r)
    left = []
    distinct = False
    dtargs = attr.get('dtargs', {})
    if r.interactive:
        if s3.dataTable_pageLength:
            display_length = s3.dataTable_pageLength
        else:
            display_length = 25
        if (not s3.no_sspag):
            dt_pagination = 'true'
            if (not limit):
                limit = (2 * display_length)
            current.session.s3.filter = get_vars
            if (orderby is None):
                dt_sorting = {'iSortingCols': '1', 'sSortDir_0': 'asc'}
                if (len(list_fields) > 1):
                    dt_sorting['bSortable_0'] = 'false'
                    dt_sorting['iSortCol_0'] = '1'
                else:
                    dt_sorting['bSortable_0'] = 'true'
                    dt_sorting['iSortCol_0'] = '0'
                (q, orderby, left) = resource.datatable_filter(list_fields, dt_sorting)
        else:
            dt_pagination = 'false'
        (dt, totalrows, ids) = resource.datatable(fields=list_fields, start=start, limit=limit, left=left, orderby=orderby, distinct=distinct)
        displayrows = totalrows
        if (not dt.data):
            if (r.component and ('showadd_btn' in output)):
                del output['showadd_btn']
                datatable = ''
        dtargs['dt_pagination'] = dt_pagination
        dtargs['dt_pageLength'] = display_length
        dtargs['dt_base_url'] = r.url(method='', vars={})
        dtargs['dt_permalink'] = r.url()
        datatable = dt.html(totalrows, displayrows, id=list_id, **dtargs)
        response.view = self._view(r, 'list_filter.html')
        output['items'] = datatable
    elif (representation == 'aadata'):
        (searchq, orderby, left) = resource.datatable_filter(list_fields, get_vars)
        if (searchq is not None):
            totalrows = resource.count()
            resource.add_filter(searchq)
        else:
            totalrows = None
        if (orderby is None):
            orderby = get_config('orderby', None)
        if (totalrows != 0):
            (dt, displayrows, ids) = resource.datatable(fields=list_fields, start=start, limit=limit, left=left, orderby=orderby, distinct=distinct, getids=False)
        else:
            (dt, displayrows) = (None, 0)
        if (totalrows is None):
            totalrows = displayrows
        draw = int(get_vars.get('draw', 0))
        if (dt is not None):
            output = dt.json(totalrows, displayrows, list_id, draw, **dtargs)
        else:
            output = ('{"recordsTotal":%s,"recordsFiltered":0,"dataTable_id":"%s","draw":%s,"data":[]}' % (totalrows, list_id, draw))
    else:
        r.error(415, current.ERROR.BAD_FORMAT)
    return output
