Posted on 

Joomla 3.x 前台 content category 單一分類列表套用後台的排序方法

打開 components/com_content/views/category/tmpl/default_articles.php

在檔案最頭的 $listDirn 下一行貼上以下程式碼:

1
2
3
4
5
6
7
$saveOrder = $listOrder == 'a.ordering';

if ($saveOrder)
{
$saveOrderingUrl = 'index.php?option=com_content&task=article.saveOrderAjax&tmpl=component';
JHtml::_('sortablelist.sortable', 'articleList', 'adminForm', strtolower($listDirn), $saveOrderingUrl);
}

然後把這一行

1
<table class="category table table-striped table-bordered table-hover">

換成這一行

1
<table class="category table table-striped table-bordered table-hover" id="articleList">

<th> 中加入這行

1
2
3
<th width="1%" class="nowrap center hidden-phone">  
<?php echo JHtml::_('grid.sort', '排序', 'a.ordering', $listDirn, $listOrder, null, 'asc', '排序', 'icon-menu-2'); ?>
</th>

<tbody> 換成下列這行

1
<tbody class="ui-sortable">

<tbody> 中適當加入以下程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<td class="order nowrap center hidden-phone">  
<?php
$iconClass = '';
if (!$saveOrder) {
$iconClass = ' inactive tip-top hasTooltip" title="' . JHtml::tooltipText('JORDERINGDISABLED');
}
?>
<span class="sortable-handler<?php echo $iconClass ?>">
<i class="icon-menu"><?php //echo $article->id.' | '.$article->ordering ?></i>
</span>
<?php if ($isEditable && $saveOrder) : ?>
<div style="display: none;">
<input type="text" name="order[]" size="5" value="<?php echo $article->ordering; ?>" class="width-20 text-area-order " />
<?php echo JHtml::_('grid.id', $i, $article->id); ?>
</div>
<?php endif; ?>
</td>

打開 components/com_content/models/articles.php

在 function getListQuery 中,在 $query->select 裡加入 a.ordering

1
'a.id, a.title, a.alias, a.introtext, a.fulltext,  a.ordering, ' .

打開 components/com_content/controllers/category.php 加入以下程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public function getModel($name = 'Article', $prefix = 'ContentModel', $config = array('ignore_request' => true)) {
JLoader::import('joomla.application.component.model');
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_content/models', 'ContentModel');
$model = JModelLegacy::getInstance($name, $prefix);

return $model;
}

public function saveOrderAjax() {
// Get the input
$pks = $this->input->post->get('cid', array(), 'array');
$order = $this->input->post->get('order', array(), 'array');

// Sanitize the input
JArrayHelper::toInteger($pks);
JArrayHelper::toInteger($order);

// Get the model
$model = $this->getModel();

// Save the ordering
$return = $model->saveorder($pks, $order);

if ($return) {
echo "1";
}

// Close the application
JFactory::getApplication()->close();
}

這樣應該就完成了,就可以在前台將文章拉來拉去做排序囉~~!