不要設置現場手動排序。 使用DataHandler類進行排序。
當你想要做更改條目的排序位置使用COMAND像:
$cmd[ tablename ][ uid ][ command ] = value
你可以找到這裏更多信息:
https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Typo3CoreEngine/Database/Index.html
隨着COMAND 移動您可以交換表格中條目的位置。
當您創建的,您可以在此代碼
$tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
$tce->stripslashes_values = 0;
$tce->start(array(), $cmd);
$tce->process_cmdmap();
這是相同的COMAND TYPO3使用在後端進行排序列表執行COMAND。
對於在DataHandler的通話看看這裏的更多信息:
https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Typo3CoreEngine/UsingDataHandler/Index.html
實例庫:
class SortedRepository extends \TYPO3\CMS\Extbase\Persistence\Repository
{
protected $defaultOrderings = array(
'sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
);
public function moveUp($entity)
{
$tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
$tce->stripslashes_values = 0;
$entityTwoBefore = $this->getTwoBefore($entity);
if ($entityTwoBefore != null) {
//category is minimum 3
//can set over UID
$cmd[$this->getTableName($entity)][$entity->getUid()]['move']= 0-$entityTwoBefore->getUid();
} else {
//can only set over pid
$cmd[$this->getTableName($entity)][$entity->getUid()]['move']= Util::getStorageID();
}
$tce->start(array(), $cmd);
$tce->process_cmdmap();
}
public function moveDown($entity)
{
$tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
$tce->stripslashes_values = 0;
$nextEntity = $this->getNext($entity);
if ($nextEntity != null) {
$cmd[$this->getTableName($entity)][$entity->getUid()]['move'] = 0 - $nextEntity->getUid();
}
$tce->start(array(), $cmd);
$tce->process_cmdmap();
}
private function getNext($entity)
{
$entities = $this->findAll();
$match = false;
foreach ($entities as $entityFor) {
if ($entityFor->getUid() == $entity->getUid()) {
$match = true;
continue;
}
if ($match == true) {
return $entityFor;
}
}
}
private function getBefore($entity)
{
$entities = array_reverse($this->findAll()->toArray());
$match = false;
foreach ($entities as $entityFor) {
if ($entityFor->getUid() == $entity->getUid()) {
$match = true;
continue;
}
if ($match == true) {
return $entityFor;
}
}
}
private function getTwoBefore($entity)
{
$entityTwoBefore = null;
$entityBefore = $this->getBefore($entity);
if ($entityBefore != null) {
$entityTwoBefore = $this->getBefore($entityBefore);
}
return $entityTwoBefore;
}
/**
* Return the current tablename
*
* @return string
*/
private function getTableName($entity) {
/** @var DataMapper $dataMapper */
$dataMapper = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Mapper\\DataMapper');
return $dataMapper->getDataMap(get_class($entity))->getTableName();
}
}
如果你的版本庫擴展SortedRepository可以使用方法爲moveUp()和moveDown()。
注意: DB-Table需要字段「排序」。你需要在文件ext_tables.sql和模型類的TCA:
ext_tables.sql:
#
# Table structure for table 'tx_extension_domain_model_subcategory'
#
CREATE TABLE tx_extension_domain_model_subcategory (
uid int(11) NOT NULL auto_increment,
pid int(11) DEFAULT '0' NOT NULL,
name varchar(255) DEFAULT '' NOT NULL,
tstamp int(11) unsigned DEFAULT '0' NOT NULL,
crdate int(11) unsigned DEFAULT '0' NOT NULL,
cruser_id int(11) unsigned DEFAULT '0' NOT NULL,
deleted tinyint(4) unsigned DEFAULT '0' NOT NULL,
hidden tinyint(4) unsigned DEFAULT '0' NOT NULL,
starttime int(11) unsigned DEFAULT '0' NOT NULL,
endtime int(11) unsigned DEFAULT '0' NOT NULL,
t3ver_oid int(11) DEFAULT '0' NOT NULL,
t3ver_id int(11) DEFAULT '0' NOT NULL,
t3ver_wsid int(11) DEFAULT '0' NOT NULL,
t3ver_label varchar(255) DEFAULT '' NOT NULL,
t3ver_state tinyint(4) DEFAULT '0' NOT NULL,
t3ver_stage int(11) DEFAULT '0' NOT NULL,
t3ver_count int(11) DEFAULT '0' NOT NULL,
t3ver_tstamp int(11) DEFAULT '0' NOT NULL,
t3ver_move_id int(11) DEFAULT '0' NOT NULL,
sorting int(11) DEFAULT '0' NOT NULL,
sys_language_uid int(11) DEFAULT '0' NOT NULL,
l10n_parent int(11) DEFAULT '0' NOT NULL,
l10n_diffsource mediumblob,
PRIMARY KEY (uid),
KEY parent (pid),
KEY t3ver_oid (t3ver_oid,t3ver_wsid),
KEY language (l10n_parent,sys_language_uid)
);
和模型中的TCA:
<?php
return array(
'ctrl' => array(
'title' => 'LLL:EXT:pdvdownloadportal/Resources/Private/Language/locallang_db.xlf:tx_pdvdownloadportal_domain_model_subcategory',
'label' => 'name',
'tstamp' => 'tstamp',
'crdate' => 'crdate',
'cruser_id' => 'cruser_id',
'dividers2tabs' => TRUE,
'versioningWS' => 2,
'versioning_followPages' => TRUE,
'sortby' => 'sorting',
...
很有意思只你設置你的排序固定爲8 ...我想知道如何使用列表視圖中的一些本地排序(拖放或箭頭)來排序我的項目和填寫表字段排序,也許你有線索。 .. – webMan