I have a notification system that works. Right now, when there is no notification or not, a notification appears in <span class='glyphicon glyphicon-inbox' aria-hidden="true"></span> just like stackoverflow. but my desire is when there is a notice, I want the box to be turned into
<span class="badge badge-notify" style="red">notification count</span>
again, like a stack overflow.
So, I tried to remove a specific class that changes the shape of the field if count == 0 and adding a class when the counter is not zero. I also tried to set the install interval, but it just won't work.
Can you help me?
below is what i have in navbar, i have notification window and icon.
<li class="dropdown"> <a href="#" class="dropdown-toggle notification-toggle" data-toggle="dropdown" role="button" aria-expanded="false" id="button"> <span class='glyphicon glyphicon-inbox' aria-hidden="true"></span> <span class="caret"></span> <span class="badge badge-notify">notification count</span></a> <ul class="dropdown-menu" role="menu" id='notification_dropdown'> </ul> </li>
below is my ajax function to indicate notification
<script> $(document).ready(function(){ $(".notification-toggle").click(function(e){ e.preventDefault(); $.ajax({ type: "POST", url: "{% url 'get_notifications_ajax' %}", data: { csrfmiddlewaretoken: "{{ csrf_token }}", }, success: function(data){ $("#notification_dropdown").html(' <li role="presentation" class="dropdown-header">alarm</li>'); var count = data.count console.log(count) if (count == 0) { $("#notification_dropdown").removeClass('notification'); var url = '{% url "notifications_all" %}' $("#notification_dropdown").append("<li><a href='" + url+ "'>view all</a></li>") } else { $("#notification_dropdown").addClass('notification'); $(data.notifications).each(function(){ var link = this; $("#notification_dropdown").append("<li>" + link + "</li>") }) } console.log(data.notifications); }, error: function(rs, e) { console.log(rs); console.log(e); } }) }) }) </script>
so I tried this
<style> {% block style %} #notification_dropdown{ } #notification_dropdown.notification{ color: red; } {% endblock %} </style> and <script> setTimeout(function(){ $("#notification_dropdown:visible").addClass('notification'); }, 2000); </script>
Perhaps I set id incorrectly ... they didnโt do anything unfortunately.
Not sure if this is necessary, I will also add a notification function (ajax)
def get_notifications_ajax(request): if request.is_ajax() and request.method == "POST": notifications = Notification.objects.all_for_user(MyProfile.objects.get(user=request.user)).recent() count = notifications.count() notes = [] for note in notifications: notes.append(note.get_link.encode('utf-8')) data = { "notifications": notes, "count": count, } print data json_data = json.dumps(data) print json_data return HttpResponse(json_data, content_type='application/json') else: raise Http404
So my question is)
1) what I did wrong, that the shape / color of the notification window did not change when the notification is not zero, and how do I achieve what I want the window to be turned into
<span class="badge badge-notify" style="red">notification count</span>
2) I can show the number of notifications in the console using console.log (count), how can I display this account in the navigation bar?