Question

In django-tables2, By default all table columns support ordering. This means that all the column headers are rendered as links which allow the user to adjust the ordering of the table data. But I don't want the column headers are rendered to links, How to do that?

Here is the document!

By default all table columns support ordering. This means that all the column headers are rendered as links which allow the user to adjust the ordering of the table data.

Ordering can be disabled on a table or column basis.

Table.Meta.orderable = False – default to disable ordering on columns
Column(orderable=False) – disable ordering for specific column

e.g. disable columns on all but one:

class SimpleTable(tables.Table):
    name = tables.Column()
    rating = tables.Column(orderable=True)

    class Meta:
        orderable = False

I do that, but It doesn't work.This is my talbes.py file:

class MusicBaseTable(tables.Table):
    songs = tables.CheckBoxColumn()
    title = tables.Column()
    artist = tables.Column()
    album = tables.Column()
    genre = tables.Column()
    date = tables.Column()

    class Meta:
        orderable = False
        attrs = {"class": "list"}
Was it helpful?

Solution

It's in the documentation:

Disabling ordering for specific columns

By default all table columns support ordering. This means that all the column headers are rendered as links which allow the user to adjust the ordering of the table data.

Ordering can be disabled on a table or column basis.

  • Table.Meta.orderable = False -- default to disable ordering on columns
  • Column(orderable=False) -- disable ordering for specific column e.g. disable columns on all but one:

See how the template decides whether a column should have the order link or not: {% if column.orderable %}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top