Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added table html javascript placeholders and Column url_name for href link #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions datatables/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Column(object):
'search_field': None,
'value_renderer': None,
'label_renderer': None,
'url_name': None,
}

def __init__(self, **kwargs):
Expand All @@ -55,10 +56,28 @@ def __init__(self, **kwargs):
self.value_renderer = getattr(self, 'render_value', None)
if self.label_renderer is None:
self.label_renderer = getattr(self, 'render_label', None)
if self.url_name:
parts = self.url_name.split(':')
if len(parts) > 1:
self.url_name = parts[0]
self.url_field = parts[1]
else:
self.url_field = 'id'

# Increase the creation counter, and save our local copy.
self.creation_counter = Column.creation_counter
Column.creation_counter += 1

def get_absolute_url(self, row):
return reverse(self.url_name, args=(lookupattr(row, self.url_field),))

def render_value(self, row, bc):
value = lookupattr(row, bc.display_field)
if self.url_name:
value = '<a href="%s">%s</a>' % (self.get_absolute_url(row), value)
value = mark_safe(value)
return value


class CheckboxColumn(Column):

Expand Down
9 changes: 8 additions & 1 deletion datatables/templates/datatables/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@
<script type="text/javascript">
{% if table.var %}var {{ table.var }};{% endif %}
$(document).ready(function() {
{% if table.before_js %}
{{ table.before_js }}
{% endif %}
{% if table.var %}{{ table.var }} = {% endif %}$('#{{ table.id }}').dataTable({{ table.js_options }});
{% if table.after_js %}
{{ table.after_js }}
{% endif %}

});

{% if table.extra_js %}
{{ table.extra_js }}
{% endif %}
</script>
</script>