'Update output with a dataTable and a create_popup
Update dt_init for the dataTable'

def _datatable(self, output, tablename, search=True, updateable=True, export=False, event_id=None, incident_id=None, ajax_vars=None, dt_init=None):

    T = current.T
    s3db = current.s3db
    (c, f) = tablename.split('_', 1)
    if (ajax_vars is None):
        ajax_vars = {}
    resource = s3db.resource(tablename)
    if event_id:
        ltablename = ('event_%s' % f)
        if (tablename == ltablename):
            resource.add_filter((FS('event_id') == event_id))
            ajax_vars['~.event_id'] = event_id
        else:
            resource.add_filter((FS(('event_%s.event_id' % f)) == event_id))
            ajax_vars[('event_%s.event_id' % f)] = event_id
    elif incident_id:
        ltablename = ('event_%s' % f)
        if (tablename == ltablename):
            resource.add_filter((FS('incident_id') == incident_id))
            ajax_vars['~.incident_id'] = incident_id
        else:
            resource.add_filter((FS(('event_%s.incident_id' % f)) == incident_id))
            ajax_vars[('event_%s.incident_id' % f)] = incident_id
    dataTable_id = ('custom-list-%s' % tablename)
    if dt_init:
        if search:
            dt_init.append(("$('#dt-%(tablename)s    .dataTables_filter').prependTo($('#dt-search-%(tablename)s'));$('#dt-search-%(tablename)s    .dataTables_filter    input').attr('placeholder','%(placeholder)s').attr('name','%(tablename)s-search').prependTo($('#dt-search-%(tablename)s    .dataTables_filter'));$('#dt-search-%(tablename)s    .dataTables_filter').removeClass('dataTables_filter');" % dict(tablename=tablename, placeholder=T('Search'))))
        current.deployment_settings.ui.datatables_initComplete = ''.join(dt_init)
    displayLength = 10
    if export:
        current.response.s3.no_formats = False
    else:
        current.response.s3.no_formats = True
    get_config = s3db.get_config
    list_fields = get_config(tablename, 'list_fields')
    orderby = get_config(tablename, 'orderby')
    (dt, totalrows, ids) = resource.datatable(fields=list_fields, start=0, limit=(2 * displayLength), orderby=orderby)
    displayrows = totalrows
    if dt.empty:
        empty_str = self.crud_string(tablename, 'msg_list_empty')
    else:
        empty_str = self.crud_string(tablename, 'msg_no_match')
    empty = DIV(empty_str, _class='empty')
    dtargs = {'dt_pagination': 'true', 'dt_pageLength': displayLength}
    if (not search):
        dtargs['dt_searching'] = False
    dtargs['dt_row_actions'] = [{'label': '', 'url': ''}]
    dtargs['dt_ajax_url'] = URL(c=c, f=f, vars=ajax_vars, extension='aadata')
    datatable = dt.html(totalrows, displayrows, id=dataTable_id, **dtargs)
    if dt.data:
        empty.update(_style='display:none')
    else:
        datatable.update(_style='display:none')
    contents = DIV(datatable, empty, _class='dt-contents')
    if (updateable and current.auth.s3_has_permission('create', tablename)):
        if (tablename == 'event_human_resource'):
            label = T('Assign    Staff')
            if event_id:
                url = URL(c='event', f='event', args=[event_id, 'assign.popup'], vars={'refresh': dataTable_id})
            else:
                url = URL(c='event', f='incident', args=[incident_id, 'assign.popup'], vars={'refresh': dataTable_id})
        else:
            if event_id:
                if (f == 'team'):
                    f = 'group'
                url = URL(c='event', f='event', args=[event_id, f, 'create.popup'], vars={'refresh': dataTable_id})
            elif incident_id:
                if (f == 'team'):
                    f = 'group'
                url = URL(c='event', f='incident', args=[incident_id, f, 'create.popup'], vars={'refresh': dataTable_id})
            else:
                url = URL(c=c, f=f, args=['create.popup'], vars={'refresh': dataTable_id})
            if (tablename == 'event_organisation'):
                label = T('Add    Organization')
            elif (tablename == 'project_task'):
                label = T('Create    Task')
            else:
                label = T('Add')
        output[('create_%s_popup' % tablename)] = DIV(A(TAG[''](ICON('plus'), label), _href=url, _class='button    wide    radius    s3_modal', _title=label), _class='panel')
    else:
        output[('create_%s_popup' % tablename)] = ''
    output[('%s_datatable' % tablename)] = contents
