【问题标题】:How to add jqplot to custom Joomla (3.3) component?如何将 jqplot 添加到自定义 Joomla (3.3) 组件?
【发布时间】:2014-06-22 12:09:30
【问题描述】:

在开始使用 Joomla 之前,我开发了一个使用 jqplot 的 html 页面,并且该页面运行良好。

现在我想在我正在开发的自定义 joomla (3.3) 组件中包含 jqplot,但是在调用组件时(通过主菜单项)没有显示图表。

更新 DEFAULT.PHP (JOOMLA) 代码以进一步评论:

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
$document = JFactory::getDocument();

//add jqplot libraries
JHtml::_('jquery.framework');
$document->addScript(JPATH_ROOT.'/media/system/js/jquery.jqplot.min.js');
$document->addStyleSheet(JPATH_ROOT.'/media/system/js/jquery.jqplot.min.css');
$document->addScript(JPATH_ROOT.'/media/system/js/jqplot.barRenderer.min.js');
$document->addScript(JPATH_ROOT.'/media/system/js/jqplot.categoryAxisRenderer.min.js');
$document->addScript(JPATH_ROOT.'/media/system/js/jqplot.pointLabels.min.js');
$document->addScript(JPATH_ROOT.'/media/system/js/jqplot.enhancedLegendRenderer.js');
$document->addScript(JPATH_ROOT.'/media/system/js/weqlib.js');

?>

<head>
<script type="text/javascript">

    jQuery(document).ready(function(){
        var plot1 = jQuery.jqplot ('chart1', [[3,7,9,1,4,6,8,2,5]]); //copied from example at http://www.jqplot.com/tests/line-charts.php

    }); //$(document).ready
</script>
</head>

<!--<h1><?php echo $this->msg; ?></h1>-->
<h1>Prosumer Dashboard </h1>

<div id="chart1" style="width:600px; height:250px;"> </div>

我认为我调用 libabries 的方式是错误的(我确定 jqplot 函数调用没问题,因为我也从旧的 html 文件中复制了它)。

知道我做错了什么以及如何解决这个问题吗?

【问题讨论】:

    标签: php jquery joomla jqplot


    【解决方案1】:

    您应该通过以下方式将 javascript 添加到页面头部...

    <?php
    $document = JFactory::getDocument();
    $document->addScript('/media/system/js/sample.js');
    ?>
    

    您的脚本当前将尝试加载与页面 URL 相关的 javascript,而不是与您的组件 URL 相关的,因此它不会找到它们。

    此外,Joomla 3 附带 JQuery - 像这样加载它(可能在您的模板中而不是在您的组件中)...

    JHtml::_('jquery.framework');
    

    请注意,这将在无冲突模式下运行,因此您必须将 jquery 脚本中的任何“$”替换为“jQuery”。你也可以让它在冲突模式下运行...

    JHtml::_('jquery.framework', false);
    

    但是,如果您的网站有任何其他库正在运行,这可能会导致问题。

    【讨论】:

    • 嗨,谢谢你的评论,理查德。根据您的建议,我更改了 default.php 文件的代码,但首页没有显示任何图表。此外,控制台显示许多 404 错误,告诉它找不到 jqplot js 文件,我根本不明白,因为控制台中的路径与文件的路径匹配???请注意,我包括了 JPATH_ROOT;如果没有那个常数,控制台也会显示 404 错误以及错误的路径。我在笔记本电脑(本地主机)上运行 Joomla
    猜你喜欢
    • 2014-12-04
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    相关资源
    最近更新 更多