'Filterable datatable/datalist
@param r: the S3Request
@param attr: dictionary of parameters for the method handler'

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

    resource = self.resource
    tablename = resource.tablename
    get_config = resource.get_config
    list_fields = get_config('list_fields', None)
    representation = r.representation
    if (representation in ('html', 'iframe', 'aadata', 'dl', 'popup')):
        hide_filter = self.hide_filter
        filter_widgets = get_config('filter_widgets', None)
        show_filter_form = False
        if (filter_widgets and (not hide_filter) and (representation not in ('aadata', 'dl'))):
            show_filter_form = True
            from s3filter import S3FilterForm
            default_filters = S3FilterForm.apply_filter_defaults(r, resource)
        else:
            default_filters = None
        get_vars = r.get_vars
        attr = dict(attr)
        list_type = attr.get('list_type', 'datatable')
        if (list_type == 'datalist'):
            filter_ajax = True
            target = 'datalist'
            output = self._datalist(r, **attr)
        else:
            if (filter_widgets and (not hide_filter)):
                dtargs = attr.get('dtargs', {})
                if ('dt_searching' not in dtargs):
                    dtargs['dt_searching'] = False
                if default_filters:
                    ajax_vars = dict(get_vars)
                    ajax_vars.update(default_filters)
                    ajax_url = r.url(representation='aadata', vars=ajax_vars)
                    dtargs['dt_ajax_url'] = ajax_url
                attr['dtargs'] = dtargs
            filter_ajax = True
            target = 'datatable'
            output = self._datatable(r, **attr)
        if (representation in ('aadata', 'dl')):
            return output
        output['list_type'] = list_type
        crud_string = self.crud_string
        if (representation != 'iframe'):
            if r.component:
                title = crud_string(r.tablename, 'title_display')
            else:
                title = crud_string(self.tablename, 'title_list')
            output['title'] = title
        if show_filter_form:
            filter_submit_url = attr.get('filter_submit_url')
            if (not filter_submit_url):
                get_vars_ = self._remove_filters(get_vars)
                filter_submit_url = r.url(vars=get_vars_)
            filter_ajax_url = attr.get('filter_ajax_url')
            if (filter_ajax_url is None):
                filter_ajax_url = r.url(method='filter', vars={}, representation='options')
            filter_clear = get_config('filter_clear', current.deployment_settings.get_ui_filter_clear())
            filter_formstyle = get_config('filter_formstyle', None)
            filter_submit = get_config('filter_submit', True)
            filter_form = S3FilterForm(filter_widgets, clear=filter_clear, formstyle=filter_formstyle, submit=filter_submit, ajax=filter_ajax, url=filter_submit_url, ajaxurl=filter_ajax_url, _class='filter-form', _id=('%s-filter-form' % target))
            fresource = current.s3db.resource(resource.tablename)
            alias = (resource.alias if r.component else None)
            output['list_filter_form'] = filter_form.html(fresource, get_vars, target=target, alias=alias)
        else:
            output['list_filter_form'] = ''
        insertable = get_config('insertable', True)
        if insertable:
            addbtn = get_config('addbtn', False)
            listadd = get_config('listadd', True)
            if listadd:
                response = current.response
                view = response.view
                s3 = response.s3
                cancel = s3.cancel
                s3.cancel = {'hide': 'list-add', 'show': 'show-add-btn'}
                form = self.create(r, **attr).get('form', None)
                if (form is not None):
                    output['form'] = form
                    addtitle = self.crud_string(tablename, 'label_create')
                    output['addtitle'] = addtitle
                    showadd_btn = self.crud_button(None, tablename=tablename, name='label_create', icon='add', _id='show-add-btn')
                    output['showadd_btn'] = showadd_btn
                response.view = view
                s3.cancel = cancel
            elif addbtn:
                buttons = self.render_buttons(r, ['add'], **attr)
                if buttons:
                    output['buttons'] = buttons
        return output
    elif (representation == 'plain'):
        if (resource.count() == 1):
            resource.load()
            r.record = resource.records().first()
            if r.record:
                r.id = r.record.id
                self.record_id = self._record_id(r)
                if (('update' in r.get_vars) and self._permitted(method='update')):
                    items = self.update(r, **attr).get('form', None)
                else:
                    items = self.sqlform(request=self.request, resource=self.resource, record_id=r.id, readonly=True, format=representation)
            else:
                raise HTTP(404, body='Record    not    Found')
        else:
            rows = resource.select(list_fields, limit=None, as_rows=True)
            if rows:
                items = rows.as_list()
            else:
                items = []
        current.response.view = 'plain.html'
        return {'items': items}
    elif (representation == 'csv'):
        exporter = S3Exporter().csv
        return exporter(resource)
    elif (representation == 'json'):
        get_vars = self.request.get_vars
        (start, limit) = self._limits(get_vars, default_limit=None)
        tooltip = get_vars.get('tooltip', None)
        represent = get_vars.get('represent', False)
        if (represent and (represent != '0')):
            represent = True
        exporter = S3Exporter().json
        return exporter(resource, start=start, limit=limit, represent=represent, tooltip=tooltip)
    elif (representation == 'pdf'):
        report_hide_comments = get_config('report_hide_comments', None)
        report_filename = get_config('report_filename', None)
        report_formname = get_config('report_formname', None)
        exporter = S3Exporter().pdf
        return exporter(resource, request=r, list_fields=list_fields, report_hide_comments=report_hide_comments, report_filename=report_filename, report_formname=report_formname, **attr)
    elif (representation == 'shp'):
        exporter = S3Exporter().shp
        return exporter(resource, list_fields=list_fields, **attr)
    elif (representation == 'svg'):
        exporter = S3Exporter().svg
        return exporter(resource, list_fields=list_fields, **attr)
    elif (representation == 'xls'):
        report_groupby = get_config('report_groupby', None)
        exporter = S3Exporter().xls
        return exporter(resource, list_fields=list_fields, report_groupby=report_groupby, **attr)
    elif (representation == 'msg'):
        if (r.http == 'POST'):
            from s3notify import S3Notifications
            return S3Notifications.send(r, resource)
        else:
            r.error(405, current.ERROR.BAD_METHOD)
    else:
        r.error(415, current.ERROR.BAD_FORMAT)
