我正在从 ajax 调用返回通知
$app = JFactory::getApplication();
$app->enqueueMessage('Joomla notice', 'info');
在前端,这会导致以下内容(请注意空标题):
<div id="system-message-container">
<div id="system-message" class="alert alert-info">
<h4 class="alert-heading"></h4>
<div>
<p>Joomla notice </p>
</div>
</div>
</div>
但是,我想像在后端一样显示带有标题和关闭按钮的通知,即
<div id="system-message-container">
<button type="button" class="close" data-dismiss="alert">×</button>
<div class="alert alert-info">
<h4 class="alert-heading">Info</h4>
<p>Joomla notice</p>
</div>
</div>
有没有一种 Joomla 方法可以做到这一点,或者我必须想出一个解决方法?
消息由Joomla.renderMessages
函数以media/system/js/core.js
呈现。您可以在模板中覆盖它
jQuery(function() {
Joomla.renderMessages = function(messages) {
// copy / adapt the original function here.
}
});
此外,非 ajax 消息可以通过html/message.php
模板覆盖进行自定义。
在
你排队你的消息后,我建议像发送这样的消息
echo new JResponseJson($data);
JFactory::getApplication()->close();
然后,您可以在客户端像@Riccardo的解决方案一样处理消息数组。例如,我的 ajax 成功函数看起来像
success: function(responseText){
var json = jQuery.parseJSON(responseText);
Joomla.renderMessages(json.messages);
....
您可以在此处找到代码 https://github.com/Digital-Peak/DPAttachments/blob/master/com_dpattachments/admin/libraries/dpattachments/core.php#L162