Live demo

ID headline pub date


To embed a datatable onto a page that shouldn't be responsible for generating all of the ajax queries, you can easily just create the structure object that serves as the context variable, but base it on the options of some other view. The other view will indeed need access to those options once queries begin to autonomously route to it over AJAX, so you won't be able to specify the column options directly inside of the get_context_data(), but you can get pretty close.

In this example, we've created a separate view, called SatelliteDatatableView, which houses all of the options and machinery for getting the structure object for the context.

Just add a get_context_data() method, instantiate the other view, and ask it to generate the options object via get_datatable_options(). You can feed this options object into the datatableview.utils.get_datatable_structure() utility.

get_datatable_structure() takes at least two arguments, and is the mechanism that a regular call to a DatatableView.get_datatable() uses to get the context variable: the ajax_url the table will target, and the options object. Unless there are ordering options set in the main datatable_options object, the table won't know how to use default ordering, so you can specify the third optional argument model.

Implementation in code

    class EmbeddedTableDatatableView(TemplateView):
        def get_context_data(self, **kwargs):
            context = super(EmbeddedTableDatatableView, self).get_context_data(**kwargs)

            satellite_view = SatelliteDatatableView()
            model = satellite_view.model
            options = satellite_view.get_datatable_options()
            datatable = get_datatable_structure(reverse('satellite'), options, model=model)

            context['datatable'] = datatable
            return context

    class SatelliteDatatableView(DatatableView):
        External view powering the embedded table for ``EmbeddedTableDatatableView``.
        model = Entry
        datatable_options = {
            'columns': [