infinite rolování článků nette/ajax
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- microcz
- Člen | 62
Použil jsem, díky, ale zlobí mě to (asi dělám něco špatně) , protože „$this->isAjax“ je pořád false…
Presenter
class TopicPresenter extends BasePresenter {
/** @persistent */
public $topic;
/** @var topic object */
private $topicObj;
/** @var step size */
private $step = 5;
/**
* Preloads topic object
*/
public function actionDefault() {
if ($this->topicObj == null) {
$this->topicObj = (new Topic())->setDb($this->db)->loadByStrKey($this->topic);
}
}
/**
* Renders articles
*/
public function renderDefault() {
if (!isset($this->template->articles)) {
$this->template->articles = $this->loadArticles();
$this->template->topicObj = $this->topicObj;
$this->template->offset = 0;
}
}
/**
* Updates infinite list of articles
* @param int $offset
*/
public function handleUpdate($offset) {
if ($this->isAjax()) {
$offset += $this->step;
$this->template->articles = $this->loadArticles($offset);
$this->template->offset = $offset;
} else {
$this->template->articles = array();
$this->template->offset = $offset;
}
$this->template->topicObj = $this->topicObj;
$this->redrawControl('articlesContainer');
}
/**
* Loads articles
* @param int $offset
* @return array list of articles
*/
private function loadArticles($offset = 0) {
return $this->db->fetchAll($this->createQuery($offset));
}
/**
* Creates query for articles
* @param string $offset
* @return string query string
*/
private function createQuery($offset = 0) {
$view = 'view_verified_article';
$q = 'SELECT a.* FROM ' . $view . ' AS a ';
$q .= 'WHERE a.topic_id = ' . $this->topicObj->getId() . ' ';
$q .= 'ORDER BY a.article_inserted DESC ';
$q .= 'LIMIT ' . $offset . ', ' . $this->step;
return $q;
}
}
A latte:
{block content}
<h1>{$topicObj->getName()}</h1>
<p>{$topicObj->getDescr()}<p/>
<div n:snippet="articlesContainer" data-ajax-append="true">
{foreach $articles as $article}
<h2>{$article['article_title']}</h2>
{$article['article_content']}
{/foreach}
</div>
<a class="ajax" n:href="update! $offset">Starší články</a>
{/block}
{block scripts}
<script src="{$basePath}/js/jquery.js"></script>
<script src="{$basePath}/js/main.js"></script>
<script src="{$basePath}/js/nette.ajax.js"></script>
{/block}