简体   繁体   English

使用Javascript从Joomla加载数据

[英]Loading data from Joomla using Javascript

I am currently coding a timetable application with PHP. 我目前正在使用PHP编写时间表应用程序。

What I want to do is to have a button for each day of the week and that when a user clicks each button, the tasks listed down for that day are loaded into the browser window using JavaScript. 我想要做的是为一周中的每一天都设置一个按钮,当用户点击每个按钮时,使用JavaScript将当天列出的任务加载到浏览器窗口中。

My current code is: 我目前的代码是:

<?php
class loadTimetable
{
    public static function getTimetable( $params )
    {
        //Obtain a database connection
        $db = JFactory::getDbo();
        //Retrieve the shout
        $query = $db->getQuery(true)
                    ->select($db->quoteName('title'))
                    ->from($db->quoteName('#__timetable'))
                    ->where('day = '. $db->Quote($params));
        //Prepare the query
        $db->setQuery($query);
        // Load the row.
        $result = $db->loadResult();
        //Return the Hello
        return $result;
    }
}
$day = date("N");
$timetable = getTimetable($day);
?>
<h1><?php echo $timetable; ?></h1>

The function is inside Joomla and has been tested to work fine. 该功能在Joomla内部,经过测试可以正常工作。 The help I need is how to call that function using Javascript/AJAX. 我需要的帮助是如何使用Javascript / AJAX调用该函数。

As far as I can see, all Javascript would have to do is call the function with the selected day (eg, getTimetable(1) where 1 is the value for the Monday button in the HTML). 据我所知,所有Javascript必须做的是调用所选日期的函数(例如, getTimetable(1) ,其中1是HTML中星期一按钮的值)。

Based on your question and further comments, you should move your code (or its invocation at least) to the component. 根据您的问题和进一步的注释,您应该将代码(或至少其调用)移动到组件。 Only components can be "invoked". 只能“调用”组件。 There are many options, but to keep it simple you need at least (in the component): 有很多选项,但要保持简单,至少需要(在组件中):

  1. A task in a controller, ie a function getTimetable() that will receive the ajax call; 控制器中的任务,即将接收ajax调用的函数getTimetable() ; This can also be done in a subcontroller. 这也可以在子控制器中完成。 In case of a controller, you will invoke it with index.php?option=com_yourcomponent&task=getTimetable&day=2011-09-17 , in case of a subcontroller task=subcontroller.getTimetable 在控制器的情况下,您将使用index.php?option=com_yourcomponent&task=getTimetable&day=2011-09-17调用它,以防子控制器task=subcontroller.getTimetable

  2. The function you pasted in a module, let's call it timemodule for now, and getTimetable($day) will need to be public. 您粘贴在模块中的功能,我们现在将其称为timemodule,并且getTimetable($day)将需要公开。

  3. The controller:getTimetable() function will controller:getTimetable()函数会

    3.a. 3.A. parse and sanitize the day parameter input; 解析并清理日参数输入;

    3.b. 3.B. instantiate the module timemodule 实例化模块timemodule

    3.c. 3.C. instantiate the view, inject the module, let it load the results from db and 实例化视图,注入模块,让它从db加载结果

    3.d. 3.D. invoke the view's render() method 调用视图的render()方法

    3.e. 3.e. the exit statement (so Joomla will not render the template and other modules (all you want is the json/xml output). exit语句(因此Joomla不会渲染模板和其他模块(你想要的只是json / xml输出)。

There are, of course, many alternatives; 当然,有很多选择; in the controller you may set a day variable using setUserState , then redirect to the view, and use the variable from userstate in the module; 在控制器中,您可以使用setUserState设置day变量,然后重定向到视图,并使用模块中userstate的变量; it's really a matter of preference, what you want to keep in mind is that the exit statement should be invoked in the loop that renders output, so if you redirect you'll invoke it in the view's display() method. 这真的是一个偏好问题,你要记住的是应该在呈现输出的循环中调用exit语句,所以如果你重定向,你将在视图的display()方法中调用它。 You may also append &format=json to your url to achieve this. 您也可以将&format=json附加到您的网址以实现此目的。

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

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