{% load example_app_tags %}

make_xeditable

Columns from demo: Headline

Shortcut for converting the value of the column into an interactive x-editable form.

This helper will generally require that you inherit your view from datatableview.views.XEditableDatatableView instead of just the normal DatatableView, so that the view can serve as the ajax endpoint for the updates that get made to the data. See x-editable columns for a full reference of how to get that view set up so that this helper can do its job.

This helper allows you to specify the HTML data-* API attributes expected by the x-editable javascript. By default you can get away with not sending any keyword arguments to the helper, and it will choose reasonable defaults for field types, etc.

The full list of default keyword arguments are as follows:

Extra data-* API attributes can be given by specifying what they are in the keyword argument extra_attrs:

helpers.make_xeditable(extra_attrs=['data_format'], data_format="yyyy-mm-dd")

Arguments given in this way will be converted to use "-" instead of "_", which simplifies how you give them as keyword arguments.

Calls to make_xeditable that don't include an "instance" as its first positional argument will create a functools.partial around the call and return it so that new arguments can be given. This lets you build up a set of custom attributes if you want to create certain common x-editable styles. See the examples below.

Following are some examples of how the helper can be used:

from datatableview.views import XEditableDatatableView
from datatableview import helpers
from .models import Entry

# See third and fourth example columns just below
xeditable_date = helpers.make_xeditable(type="date")

class EditableDatatableView(XEditableDatatableView):
    model = Entry
    datatable_options = {
        'columns': [
            # Standard use; supply callback directly for all default options
            ("Headline", 'headline', helpers.make_xeditable),

            # Customize options described above via kwargs
            ("Headline", 'headline', helpers.make_xeditable(type="textarea")),

            # Supply a previously created "partial" helper; calls chain until there is an instance
            ("Publication Date", 'pub_date', xeditable_date),
            ("Publication Date", 'pub_date', xeditable_date(placeholder="Enter a valid date")),
        ],
    }

If you're using a custom callback method, you can still ask make_xeditable to create a <a> tag to use in your own return value. Using this direct strategy, you can supply the instance at the same time as the other keyword arguments to avoid having to call the helper more than once for the final effect.

The only catch to using the helper manually is that you should take care to forward the **kwargs from your callback to it. Chances are good that some default value that the helper wants to fill out may need information that XEditableDatatableView is trying to supply to it.

def get_column_Headline_data(self, instance, *args, **kwargs):
    # Simplest use; direct return of the x-editable <a> HTML
    return helpers.make_xeditabe(instance, **kwargs)

    # You can also supply additional arguments at the same time
    return helpers.make_xeditabe(instance, type="textarea", **kwargs)

    # You can still chain calls together and reuse previously generated "partials"
    xeditable_date = helpers.make_xeditabe(type="date")
    return xeditable_date(instance, **kwargs)
    return xeditable_date(instance, placeholder="Enter a valid date", **kwargs)