-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/changelist-data-attributes' into develop
- Loading branch information
Showing
13 changed files
with
189 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
{% extends 'admin/change_list.html' %} | ||
{% load baton_tags %} | ||
{% block content %}{{ block.super }} | ||
|
||
{% for template, position in cl.model_admin.baton_cl_includes %} | ||
<template data-position="{{ position }}"> | ||
<template data-type="include" data-position="{{ position }}"> | ||
<div class="baton-cl-include baton-cl-include-{{ position }}"> | ||
{% include template %} | ||
</div> | ||
</template> | ||
{% endfor %} | ||
|
||
{% call_model_admin_method model_admin=cl.model_admin method='baton_cl_rows_attributes' as data %} | ||
{% if data %} | ||
<template data-type="attributes"> | ||
{{ data }} | ||
</template> | ||
{% endif %} | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
Changelist Row Attributes | ||
========================= | ||
|
||
.. image:: images/baton-cl-row-attributes.png | ||
.. important:: In order for this feature to work, the user browser must support html template tags. | ||
|
||
With Baton you can add every kind of html attribute (including css classes) to any element in the changelist table (cell, rows, ...) | ||
|
||
|
||
It's a bit tricky, let's see how: | ||
|
||
1. Add a ``baton_cl_rows_attributes`` function to your ``ModelAdmin`` class, which takes ``request`` as a parameter. | ||
2. Return a json dictionary where the keys are used to match an element and the values specifies the attributes and other rules to select the element. | ||
|
||
Better to see an example: :: | ||
|
||
class NewsModelAdmin(admin.ModelAdmin): | ||
# ... | ||
|
||
def get_category(self, instance): | ||
return mark_safe('<span class="span-category-id-%d">%s</span>' % (instance.id, str(instance.category))) | ||
get_category.short_description = 'category' | ||
|
||
def baton_cl_rows_attributes(self, request): | ||
data = {} | ||
for news in News.objects.filter(category__id=2): | ||
data[news.id] = { | ||
'class': 'table-info', | ||
} | ||
data[news.id] = { | ||
'class': 'table-success', | ||
'data-lol': 'lol', | ||
'title': 'A fantasctic tooltip!', | ||
'selector': '.span-category-id-%d' % 1, | ||
'getParent': 'td', | ||
} | ||
return json.dumps(data) | ||
|
||
In such case we're returning a dictionary with possibly many keys (each key is an id of a news instance). | ||
|
||
The first kind of dictionary elements will add a ``table-info`` class to the ``tr`` (rows) containing the news respecting the rule ``category__id=2`` | ||
|
||
The second kind of element instead uses some more options to customize the element selection: you can specify a css selector, and you can specify if Baton should then take one of its parents, and in such case you can give a parent selector also. | ||
In the example provided Baton will add the class ``table-success``, ``data-attribute`` and the ``title`` attribute to the cell which contains the element ``.span-category-id-1``. | ||
|
||
So these are the rules: | ||
|
||
- the default ``selector`` is ``#result_list tr input[name=_selected_action][value=' + key + ']``, meaning that it can work only if the model is editable (you have the checkox inputs for selecting a row), and selects the row of the instance identified by ``key``. If you use a custom selector the dictionary ``key`` is unuseful. | ||
- the default ``getParent`` is ``tr``. You can change it at you will, or set it to `False`, in such case the element to which apply the given attributes will be the one specified by ``selector``. | ||
- Every other key different from ``selector`` and ``getParent`` will be considered an attribute and added to the element. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters