简体   繁体   English

Joomla Ajax jQuery

[英]Joomla Ajax jQuery

Hello! 你好!

I have a problem with my "custom" pagination for component in joomla. 我对joomla中的组件的“自定义”分页有问题。

I wanted to do list of user's articles which will shows for an example 3 posts per page. 我想做一些用户文章的列表,这些文章将显示每页3个帖子的示例。 My goal was to make pagination without refreshing webpage. 我的目标是在刷新网页的情况下进行分页。 Ajax was the best choice. Ajax是最好的选择。 I'm fighting with it right now and have the most difficult problem so far (I tried to search answer of my problem several hours). 我现在正在与它斗争并且到目前为止遇到了最困难的问题(我试图在几个小时内搜索我的问题的答案)。

user.php user.php的

    <div class="userRightContainer">
            <div class="blogArticlesBlock">
            <div class="userItemTagsBlock"><b>Debaty na forum użytkownika(<?php echo $joomla_rows; ?>)</b></div>

            <?//artykuly joomla
            ?>

        <div id="Joomla_block" class="Joomla_block">
            <? require_once("Db_joomla.php") ?>
        </div>

...
...
...

<script type="text/javascript">

function jm_previous(arg) {
    if(arg < 0)
        changePagination(0);
    else
    changePagination(arg);
}
function jm_next(argument) {
    changePagination(argument);
}

function changePagination(pageId){

                  // $("#Joomla_block").html('');
     jQuery.ajax({
           type: "GET",
           // url: "Db_joomla.php",
           url: window.location.href,
           data: { jm_start: pageId},
           success: function(result){
                  alert(result);
                   $("#Joomla_block").html(result);

           }
      });
}
</script>

Db_joomla.php Db_joomla.php

defined('_JEXEC') or die;
//
$offset_jm=$_GET["jm_start"];
if(empty($offset_jm)) $offset_jm=0;
//
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('id', 'title', 'introtext', 'created_by', 'state')));
$query->from($db->quoteName('#__content'));
$query->where($db->quoteName('created_by') . ' LIKE ' . $db->quote(JRequest::getInt('id')));
$query->where('state', '1');
$query->setLimit($joomla_page,$offset_jm);
$db->setQuery($query);
$joomla=$db->loadObjectList();
$joomla_rows = $db->loadResult();


if($joomla_rows > 0){
 foreach($joomla as $row) { 
 if ($row->created_by != JRequest::getInt('id')) continue;
?>
            <div class="articlesBlock" style="margin: 5px;">
                <!-- Avatar -->
        <img src="<?php echo $this->user->avatar; ?>" alt="<?php echo htmlspecialchars($this->user->name, ENT_QUOTES, 'UTF-8'); ?>" style="width:50px; height:auto;border: 1px solid #ccc; float:left;margin: 5px;" />
                    <div class="blogArticlestTitle" style="padding-left:60px;font-weight:bold;word-wrap:break-word;"><a href="index.php?option=com_content&view=article&id=<?php echo $row->id;?>"><?php echo $row->title ?></a></div>
                    <div class="blogArticlesDescription" style="padding: 5px;padding-top:10px;"><?php if (str_word_count($row->introtext) > 100) echo /* Wstawia komentarz i ogranicza tekst do stu znaków, po czym dodaje kropki.*/ substr(strip_tags($row->introtext), 0, 100) . "..."; else echo strip_tags($row->introtext);?>
                    </div>
                    <div class="CommentViewMore">
                    <a href="index.php?option=com_content&view=article&id=<?php echo $row->id;?>">Przejdź do artykułu ›</a>
                    </div>
                </div>
<?php
/*
    $item_counting++;
    if($item_counting == 5) break;*/
                        }


//echo $pageNav->getListFooter(  ); //Displays a nice footer
        ?>
<ul class="pager">
    <li><a href="javascript:void(0)" id="jm_previous" onclick="jm_previous(<? echo $offset_jm-$joomla_page; ?>)" style="background-color: #000;float:left;"><<</a></li>
    <li><a href="javascript:void(0)" id="jm_next" onclick="jm_next(<? echo $offset_jm+$joomla_page; ?>)" style="background-color: #000;float:right;">>></a></li>
</ul>
    <a class ="view_more_link"href="http://konfederaci.pl/index.php/component/komento/profile/id/<?php echo JRequest::getInt('id');?>" title="Artykuły użytkownika">Zobacz wszystkie artykuły</a> 
    <?php
    } 
        else
{

    echo "Ten użytkownik nie posiada żadnych postów na forum.";
}

Now some pictures: 现在一些图片:

From alert(result) 来自警报(结果)

From google chrome's console 来自谷歌Chrome的控制台

Ps: If I choose "url : "Db_joomla.php" in this script it won't make anything. still doesn't work (and the same exception). Ps:如果我选择“url:”Db_joomla.php“在这个脚本中它将不会做任何事情。仍然不起作用(和相同的例外)。

It's always a better idea to rely on Joomla's Ajax interface rather than implementing yours. 依靠Joomla的Ajax接口而不是实现你的接口总是一个更好的主意。 It is super simple and very efficient and it works. 它非常简单,非常高效,而且很有效。 We have written on how to use it here . 我们已经写了如何在这里使用它。

Essentially you will create/modify a simple module that will contain a function (that has the word Ajax at the end of its name) that will be used to process the call. 基本上,您将创建/修改一个简单的模块,该模块将包含一个用于处理调用的函数(在其名称末尾包含单词Ajax )。

Then, you will need to add a simple jquery code that will call the module through the com_ajax interface. 然后,您将需要添加一个简单的jquery代码,该代码将通过com_ajax接口调用该模块。 Note that you will need to make sure to have the module assigned to a menu item because you will need the ID of that menu item for the ajax call. 请注意,您需要确保将模块分配给菜单项,因为您需要该菜单项的ID用于ajax调用。

Simply practics: Your ajax backend part should be contains only PHP code. 简单的实践:你的ajax后端部分应该只包含PHP代码。 (Get DB query). (获取数据库查询)。 Next this PHP should return JSON array with results. 接下来,这个PHP应该返回带有结果的JSON数组。 You get this JSON for ajax request and push this data for your view. 您为ajax请求获取此JSON并为您的视图推送此数据。 For simply I use my plugin "plg_ajax" (after install see method onAjaxAjax()). 简单地说,我使用我的插件“plg_ajax” (安装后见方法onAjaxAjax())。 Example script post for this plugin with com_ajax: 使用com_ajax的此插件的示例脚本发布:

 type: "POST", async: true, url: "index.php?option=com_ajax&plugin=ajax&format=json", dataType: "json", 

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

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