Ticket #11195: change-list-classes.patch

File change-list-classes.patch, 3.2 KB (added by Diederik van der Boor, 14 years ago)

Patch that does what I've suggested. Add default classes to the columns, allow setting list_column_classes for the rest.

  • django/contrib/admin/templatetags/admin_list.py

    diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py
    index fdf082b..316f3fe 100644
    a b def result_headers(cl):  
    100100            # It is a non-field, but perhaps one that is sortable
    101101            admin_order_field = getattr(attr, "admin_order_field", None)
    102102            if not admin_order_field:
    103                 yield {"text": header}
     103                yield {
     104                    "text": header,
     105                    "class_attrib": mark_safe(' class="col-%s"' % field_name)
     106                }
    104107                continue
    105108
    106109            # So this _is_ a sortable non-field.  Go to the yield
    def result_headers(cl):  
    108111        else:
    109112            admin_order_field = None
    110113
    111         th_classes = []
     114        th_classes = ['col-' + field_name]
    112115        new_order_type = 'asc'
    113116        if field_name == cl.order_field or admin_order_field == cl.order_field:
    114117            th_classes.append('sorted %sending' % cl.order_type.lower())
    def result_headers(cl):  
    118121            "text": header,
    119122            "sortable": True,
    120123            "url": cl.get_query_string({ORDER_VAR: i, ORDER_TYPE_VAR: new_order_type}),
    121             "class_attrib": mark_safe(th_classes and ' class="%s"' % ' '.join(th_classes) or '')
     124            "class_attrib": mark_safe(' class="%s"' % ' '.join(th_classes))
    122125        }
    123126
    124127def _boolean_icon(field_val):
    def items_for_result(cl, result, form):  
    131134    """
    132135    first = True
    133136    pk = cl.lookup_opts.pk.attname
     137    list_column_classes = getattr(cl.model_admin, 'list_column_classes', {})
    134138    for field_name in cl.list_display:
    135139        row_class = ''
     140        row_classes = []
    136141        try:
    137142            f, attr, value = lookup_field(field_name, result, cl.model_admin)
    138143        except (AttributeError, ObjectDoesNotExist):
    def items_for_result(cl, result, form):  
    140145        else:
    141146            if f is None:
    142147                if field_name == u'action_checkbox':
    143                     row_class = ' class="action-checkbox"'
     148                    row_classes = ['action-checkbox']
    144149                allow_tags = getattr(attr, 'allow_tags', False)
    145150                boolean = getattr(attr, 'boolean', False)
    146151                if boolean:
    def items_for_result(cl, result, form):  
    166171                if isinstance(f, models.DateField)\
    167172                or isinstance(f, models.TimeField)\
    168173                or isinstance(f, models.ForeignKey):
    169                     row_class = ' class="nowrap"'
     174                    row_classes.append('nowrap')
    170175        if force_unicode(result_repr) == '':
    171176            result_repr = mark_safe(' ')
     177        column_class = list_column_classes.get(field_name)
     178        if column_class:
     179            row_classes.append(column_class)
     180        if row_classes:
     181            row_class = ' class="%s"' % ' '.join(row_classes)
    172182        # If list_display_links not defined, add the link tag to the first field
    173183        if (first and not cl.list_display_links) or field_name in cl.list_display_links:
    174184            table_tag = {True:'th', False:'td'}[first]
Back to Top