简体   繁体   中英

How to define dynamic number of columns in django-datatables-view

I'm using the django-datatables-view (that is used in conjunction with the jQuery plugin DataTables). For normal use, I create a class-based view based on BaseDatatableView and define there the columns variable:

class MyCBV(LoginRequiredMixin, BaseDatatableView):
    model = myModel
    columns = ['my_model_column1','my_model_column2']
    ...

Now I would like to create a variable number of columns that is based on a parameter. I can access the parameter in the CBV functions but it seems I cannot overwrite the columns variable. For example, I tried to overwrite the columns variable in get_context_data function but columns retains its original value later in render_column function.

Somebody has a solution?

BaseDatatableView implements DatatableMixin which has a method get_columns with a default implementation of

def get_columns(self):
    """ Returns the list of columns that are returned in the result set
    """
    return self.columns

You can override it and pass your custom columns here (using your parameter)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM