weixin_33691817 2016-07-12 21:33 采纳率: 0%
浏览 92

Joomla Ajax jQuery

Hello!

I have a problem with my "custom" pagination for component in joomla.

I wanted to do list of user's articles which will shows for an example 3 posts per page. My goal was to make pagination without refreshing webpage. Ajax was the best choice. 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

    <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

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

Ps: If I choose "url : "Db_joomla.php" in this script it won't make anything. still doesn't work (and the same exception).

  • 写回答

2条回答 默认 最新

  • local-host 2016-07-13 04:23
    关注

    It's always a better idea to rely on Joomla's Ajax interface rather than implementing yours. 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.

    Then, you will need to add a simple jquery code that will call the module through the com_ajax interface. 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献