我尝试通过勾选django管理界面中的复选框为每个事件选择一种颜色
使用django,我有我的文件:
model.py
class Event(models.Model):
.....
red = models.BooleanField(verbose_name='red', default=False, blank=True)
blue = models.BooleanField(verbose_name='blue', default=False, blank=True)
green = models.BooleanField(verbose_name='green', default=False, blank=True)
yellow = models.BooleanField(verbose_name='yellow', default=False, blank=True)
orange = models.BooleanField(verbose_name='orange', default=False, blank=True)
我的view.py:
events = Event.objects.all()
for event in events:
event_list.append({
.....
'red': event.red,
'blue': event.blue,
'green': event.green,
'yellow': event.yellow,
...
return http.HttpResponse(json.dumps(event_list),
content_type='application/json')
我的admin.py
fieldsets = (
......
(u"couleur de l' évenement", {
'fields': ( ('red', 'blue', 'green', 'yellow',))
}),
我的javascript文件:
eventAfterRender: function(event, element, view) {
var red=event.red;
var blue=event.blue;
var green=event.green;
var yellow=event.yellow;
if(red=true){
element.children('.fc-event-inner').css({'background-color':'#ff0000'});
element.children('.fc-event-inner').css({'color':'black'});
}
if(blue=true) {
element.children('.fc-event-inner').css({'background-color':'#013adf'});
element.children('.fc-event-inner').css({'color':'black'});
}
if(green=true){
element.children('.fc-event-inner').css({'background-color':'#00ff40'});
element.children('.fc-event-inner').css({'color':'black'});
}
if(yellow=true){
element.children('.fc-event-inner').css({'background-color':'#f7fe2e'});
element.children('.fc-event-inner').css({'color':'black'});
}
问题是我用相同的颜色显示所有事件:黄色...... 这是什么错误?
答案 0 :(得分:0)
更新if语句使用 == 而不是 = (如果明确为true,则可以使用 === )
eventAfterRender: function(event, element, view) {
var red = event.red;
var blue = event.blue;
var green = event.green;
var yellow = event.yellow;
if (red == true) {
element.children('.fc-event-inner').css({
'background-color': '#ff0000'
});
element.children('.fc-event-inner').css({
'color': 'black'
});
}
if (blue == true) {
element.children('.fc-event-inner').css({
'background-color': '#013adf'
});
element.children('.fc-event-inner').css({
'color': 'black'
});
}
if (green == true) {
element.children('.fc-event-inner').css({
'background-color': '#00ff40'
});
element.children('.fc-event-inner').css({
'color': 'black'
});
}
if (yellow == true) {
element.children('.fc-event-inner').css({
'background-color': '#f7fe2e'
});
element.children('.fc-event-inner').css({
'color': 'black'
});
}
}
作为替代方案,您可以直接在Event object中设置颜色
events: [{
start: '2015-12-12',
end: '2015-12-15',
title: 'This should have a blue background',
/*
'color' sets background AND border.
You can also use 'textColor', 'backgroundColor', and 'borderColor'
*/
color: 'blue'
}]