我有一个有效的通知系统。现在,无论是否有通知,通知都会出现在 <span class='glyphicon glyphicon-inbox' aria-hidden="true"></span> 中。就像计算器一样。但我的愿望是当有通知时,我希望盒子变成一个
<span class="badge badge-notify" style="red">notification count</span>
再次就像堆栈溢出一样。
所以我尝试删除一个特定的类,该类在 if count == 0 时更改框的形式,并在 count 不为零时添加该类。我也尝试设置设置间隔,但它不起作用。
你能帮帮我吗?
下面是我在导航栏中的内容,我设置了通知框和徽章。
<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>
下面是我显示通知的ajax函数
<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>
所以我有点尝试的是这个,
<style>
{% block style %}
#notification_dropdown{
}
#notification_dropdown.notification{
color: red;
}
{% endblock %}
</style>
and
<script>
setTimeout(function(){
$("#notification_dropdown:visible").addClass('notification');
}, 2000);
</script>
也许我把 id 设置错了...不幸的是那些什么也没做。
不确定是否需要这个,我还会添加通知功能(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
所以我的问题)
1) 当通知不为零时通知框的形式/颜色没有改变我做错了什么,我如何实现我想要的是将框变成一个
<span class="badge badge-notify" style="red">notification count</span>
2) 我可以使用 console.log(count) 在控制台中显示通知计数,如何在导航栏中显示此计数?
最佳答案
看起来您在 AJAX 成功中所做的所有更改都在 #notification_dropdown 上但在你的导航栏 HTML 中 <span>您想要切换的元素永远不会被触及。它在你的 CSS 中是一样的:
<style> {% block style %} #notification_dropdown{ } #notification_dropdown.notification{ color: red; } {% endblock %} </style>
使用的 CSS 选择器 ( #notification_dropdown ) 不会将 CSS 属性应用于 <span>重要的元素。
解决这个问题的方法之一 -
将您的导航栏 HTML 更改为:
<li class="dropdown">
<a href="#" class="dropdown-toggle notification-toggle" data-toggle="dropdown" role="button" aria-expanded="false" id="button">
<span id="no_notify" class='glyphicon glyphicon-inbox' aria-hidden="true"></span>
<span class="caret"></span>
<span id="notify_count" class="badge badge-notify">notification count</span></a>
<ul class="dropdown-menu" role="menu" id='notification_dropdown'>
</ul>
</li>
更改:添加了 id属性no notify和 notify count badge span元素。
和
将您的 Ajax 脚本更改为:
<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) {
$("#notify_count").html(count).hide();
$("#no_notify").show();
$("#notification_dropdown").removeClass('notification');
var url = '{% url "notifications_all" %}'
$("#notification_dropdown").append("<li><a href='" + url+ "'>view all</a></li>")
} else {
$("#no_notify").hide();
$("#notify_count").html(count).show();
$("#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>
更改:添加了 $("#no_notify")和 $("#notify_count")与 show() 相关的行/ hide() 这些跨度基于 count
并将您的 setTimeout 更改为:
<script>
setTimeout(function(){
$(".notification-toggle").click();
}, 2000);
</script>
触发对 标记的点击似乎是个好主意。如果将来您希望计数更新仅在用户操作(单击 anchor 标记)时发生并且不希望调用定期发生,您所要做的就是摆脱
最佳做法是添加 $(".notification-toggle").click(); triggers a click在导航栏中的 上,它执行 Ajax 调用。如果来自 Ajax 响应的计数为零,我们将隐藏 notify_count跨越并显示 no_notify否则做相反的事情。setTimeout逻辑。error 也为您的 AJAX 调用回调,以防万一 POST 调用 get_notifications_ajax失败。
关于javascript - 如果通知计数为零,我想要我现在拥有的(框),但如果它不为零,我希望将框更改为带有通知计数的徽章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36673262/
如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
如果我使用ruby版本2.5.1和Rails版本2.3.18会怎样?我有基于rails2.3.18和ruby1.9.2p320构建的rails应用程序,我只想升级ruby的版本,而不是rails,这可能吗?我必须面对哪些挑战? 最佳答案 GitHub维护apublicfork它有针对旧Rails版本的分支,有各种变化,它们一直在运行。有一段时间,他们在较新的Ruby版本上运行较旧的Rails版本,而不是最初支持的版本,因此您可能会发现一些关于需要向后移植的有用提示。不过,他们现在已经有几年没有使用2.3了,所以充其量只能让更
假设我有一个FireNinja我的数据库中的对象,使用单表继承存储。后来才知道他真的是WaterNinja.将他更改为不同的子类的最干净的方法是什么?更好的是,我很想创建一个新的WaterNinja对象并替换旧的FireNinja在数据库中,保留ID。编辑我知道如何创建新的WaterNinja来self现有FireNinja的对象,我也知道我可以删除旧的并保存新的。我想做的是改变现有项目的类别。我是通过创建一个新对象并执行一些ActiveRecord魔法来替换行,还是通过对对象本身做一些疯狂的事情,或者甚至通过删除它并使用相同的ID重新插入来做到这一点,这是问题的一部分。
尝试在我的RoR应用程序中实现计数器缓存列时出现错误Unknownkey(s):counter_cache。我在这个问题中实现了模型关联:Modelassociationquestion这是我的迁移:classAddVideoVotesCountToVideos0Video.reset_column_informationVideo.find(:all).eachdo|p|p.update_attributes:videos_votes_count,p.video_votes.lengthendenddefself.downremove_column:videos,:video_vot