简体   繁体   English

将jquery插件放置在joomla模块中

[英]Placing jquery plugin in joomla module

I'm trying to create a module in joomla, which uses one jquery plugin. 我正在尝试在joomla中创建一个模块,该模块使用一个jquery插件。 I've to do an ajax operation while clicking on an element in the module. 我必须在单击模块中的元素时执行ajax操作。 Currently I'm specifying the whole path to the php file. 目前,我正在指定php文件的完整路径。 But I know Its a wrong method. 但是我知道它是错误的方法。

The code in jquery plugin is like this.(please note the second line where the path is specified in the jquery plugin file) jquery插件中的代码是这样的(请注意第二行,其中jquery插件文件中指定了路径)

       $.get(
            "/subdirectory/MyBlog/modules/mod_calendar/getCalendar.php", {
                month: $("#selectedMonth").val(),
                year: $("#selectedYear").val(),
                flag: '-1'
            }, function(data){
                $("#monthlyCalendar").html(data);
                $("#monthlyCalendar").show();
            }
        ); 

What is the correct method to specify the path in a jquery plugin file. 在jQuery插件文件中指定路径的正确方法是什么? Also I need to know where to put my jquery plugin file in the module. 我还需要知道将我的jquery插件文件放在模块中的什么位置。

The best way I found to do it is to use the JURI::root method to create a javascript variable that I can then use. 我发现最好的方法是使用JURI :: root方法创建一个我可以使用的javascript变量。 In your php code, you would do something like this: 在您的php代码中,您将执行以下操作:

?>

<script type="text/javascript">
        var joomlaRoot = '<?php echo JURI::root(); ?>';
</script>

<?php

You can then use that variable when you are doing your AJAX call. 然后,您可以在执行AJAX调用时使用该变量。

As for where to put the jquery plugin file in your module, you can put it whereever you want under your module's directory and then use JURI::root again to create a path to it and call the JDocument::addScript method . 至于将jquery插件文件放置在模块中的位置,可以将其放置在模块目录下的任意位置,然后再次使用JURI :: root创建指向它的路径并调用JDocument :: addScript方法

On a side note, you may consider using MooTools. 另外,您可以考虑使用MooTools。 It comes bundled in Joomla! 它捆绑在Joomla中! already. 已经。 It has the ability to do AJAX calls. 它具有进行AJAX调用的能力。 Also, by using it, you avoid the possibility of having jQuery conflicts. 另外,通过使用它,可以避免发生jQuery冲突的可能性。

Atlast I was able to find a good solution to use the Ajax using Jquery in Joomla. Atlast我能够找到一个很好的解决方案,在Joomla中使用Jquery使用Ajax。

For this first you need to create a view and model to get required html through AJAX call. 首先,您需要创建一个视图和模型以通过AJAX调用获取所需的html。

Then use jQuery code similar to the following to get only the output of the required view. 然后,使用类似于以下内容的jQuery代码仅获取所需视图的输出。

//Code to get the base URL of joomla installation
szURL = document.URL;
componentList = szURL.split('/');
szDocument = componentList[componentList.length-1];
szURL = szURL.replace(szDocument, "");

//URL to the required component
url = szURL + "?option=COMPONENT_NAME&view=VIEW_NAME&tmpl=component&uid=" + getRandomValue();
jQuery.get(url, function(data) {
    jQuery("#mydiv").html(data);
});


//Function to get a random number
//It is used for Ajax calls from IE; Else IE will use the cache values
function getRandomValue(){
    return Math.floor(1000000 * (Math.random() % 1))
}

Note the URL used for the ajax call. 注意用于ajax调用的URL。 It uses " tmpl=component " to get only the html for the selected component without joomla HTMLs. 它使用“ tmpl=component ”仅获取所选组件的html,而没有joomla HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM