Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

How to apply colour in row according to condition in python Django

table.py

class resultTable(BaseTable):
class Meta(BaseTable.Meta):
    model =Modelname
    attrs = {'class': 'table table-striped table-bordered table-hover row-color=green' , 'width': '70%'}
    fields = (
        "field1",
        "field2 ",
        "status",
        "field4",
        "field5"
    )

admin.py

@admin.register(Modelname)
class resultAdmin(admin.ModelAdmin):
list_display=('field1', 'field2 ', 'status','field4 ','field5')

how to apply the condition in table. if the status is warring and ok the row color should be yellow.

question from:https://stackoverflow.com/questions/65884187/how-to-apply-colour-in-row-according-to-condition-in-python-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you could add a js to the model admin class of the model. and then using ja to change the color of the field text.

class FooAdmin(admin.ModelAdmin):
# regular stuff
class Media:
    js = (
        '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', # jquery
        'js/myscript.js',       # project static folder
        'app/js/myscript.js',   # app static folder
    )

but remind add jQuery is not a good work. use pure js instead. inside the js file you can select the element and change its css.

var aTags = document.getElementsByTagName("a");
var searchText = "SearchingText";
var found;

for (var i = 0; i < aTags.length; i++) {
    if (aTags[i].textContent == searchText) {
        found = aTags[i];
        break;
    }
 }

// Use found.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...