From d38b79de765e91d67bf334ccc85b6a255bdcbff1 Mon Sep 17 00:00:00 2001 From: Denis Flaven Date: Thu, 20 Sep 2012 07:55:25 +0000 Subject: [PATCH] Various fixes for the selection of columns / sort order in the lists. SVN:trunk[2192] --- application/datatable.class.inc.php | 91 ++++++++++++++++++++--------- core/dbobjectset.class.php | 13 +++++ js/datatable.js | 28 ++++++++- pages/ajax.render.php | 1 + 4 files changed, 101 insertions(+), 32 deletions(-) diff --git a/application/datatable.class.inc.php b/application/datatable.class.inc.php index a0ef1af12..9cb7440d4 100644 --- a/application/datatable.class.inc.php +++ b/application/datatable.class.inc.php @@ -51,15 +51,9 @@ class DataTable public function Display(WebPage $oPage, DataTableSettings $oSettings, $bActionsMenu, $sSelectMode, $bViewLink, $aExtraParams) { $this->oDefaultSettings = $oSettings; - if ($this->sTableId != null) - { - // Identified tables can have their own specific settings - $oCustomSettings = DataTableSettings::GetTableSettings($this->aClassAliases, $this->sTableId); - } - else - { - $oCustomSettings = null; - } + + // Identified tables can have their own specific settings + $oCustomSettings = DataTableSettings::GetTableSettings($this->aClassAliases, $this->sTableId); if ($oCustomSettings != null) { @@ -71,11 +65,11 @@ class DataTable $oCustomSettings = $oSettings; } - if ($oCustomSettings->iDefaultPageSize != -1) + if ($oCustomSettings->iDefaultPageSize > 0) { $this->oSet->SetLimit($oCustomSettings->iDefaultPageSize); } - + $this->oSet->SetOrderBy($oCustomSettings->GetSortOrder()); return $this->GetAsHTML($oPage, $oCustomSettings->iDefaultPageSize, $oCustomSettings->iDefaultPageSize, 0, $oCustomSettings->aColumns, $bActionsMenu, true, $sSelectMode, $bViewLink, $aExtraParams); } @@ -98,9 +92,9 @@ class DataTable $sConfigDlg = $this->GetTableConfigDlg($oPage, $aColumns, $bViewLink, $iDefaultPageSize); $sHtml = "iListId}\" class=\"datatable\">\n"; - $sHtml .= "\n"; - $sHtml .= "\n"; - $sHtml .= "\n"; + $sHtml .= "\n"; + $sHtml .= "\n"; + $sHtml .= "\n"; $sHtml .= "
$sObjectsCount$sActionsMenu
$sPager$sToolkitMenu
$sDataTable
$sObjectsCount$sActionsMenu
$sPager$sToolkitMenu
$sDataTable
\n"; $oPage->add_at_the_end($sConfigDlg); @@ -163,7 +157,7 @@ class DataTable protected function GetPager(WebPage $oPage, $iPageSize, $iDefaultPageSize, $iPageIndex) { $sHtml = ''; - if ($iPageSize == -1) // Display all + if ($iPageSize < 1) // Display all { $sPagerStyle = 'style="display:none"'; // no limit: display the full table, so hide the "pager" UI } @@ -175,18 +169,23 @@ class DataTable $sCombo = ''; $sPages = Dict::S('UI:Pagination:PagesLabel'); $sPageSizeCombo = Dict::Format('UI:Pagination:PageSize', $sCombo); - $iNbPages = ($iPageSize == -1) ? 1 : ceil($this->iNbObjects / $iPageSize); + $iNbPages = ($iPageSize < 1) ? 1 : ceil($this->iNbObjects / $iPageSize); + if ($iNbPages == 1) + { + // No need to display the pager + $sPagerStyle = 'style="display:none"'; + } $aPagesToDisplay = array(); for($idx = 0; $idx <= min(4, $iNbPages-1); $idx++) { @@ -249,21 +248,22 @@ EOF; $sHtml = "
iListId}\" style=\"display: none;\">"; $sHtml .= "
"; $sChecked = ($this->bUseCustomSettings) ? '' : 'checked'; - $sHtml .= "

 ".Dict::S('UI:UseDefaultSettings').'

'; + $sHtml .= "

iListId}\" type=\"radio\" name=\"settings\" $sChecked value=\"defaults\">

'; $sHtml .= "
"; $sChecked = ($this->bUseCustomSettings) ? 'checked': ''; - $sHtml .= " ".Dict::S('UI:UseSpecificSettings').""; + $sHtml .= "iListId}\" type=\"radio\" class=\"specific_settings\" name=\"settings\" $sChecked value=\"specific\">"; $sHtml .= Dict::S('UI:ColumnsAndSortOrder').'
    '; $sHtml .= '

    '.Dict::Format('UI:Display_X_ItemsPerPage', '').'

    '; $sHtml .= "
    "; $sHtml .= "
    "; $sSaveChecked = ($this->sTableId != null) ? 'checked' : ''; + $sCustomDisabled = ($this->sTableId == null) ? 'disabled="disabled" stay-disabled="true" ' : ''; $sCustomChecked = ($this->sTableId != null) ? 'checked' : ''; $sGenericChecked = ($this->sTableId == null) ? 'checked' : ''; - $sHtml .= " ".Dict::S('UI:UseSavetheSettings').""; - $sHtml .= '

     '.Dict::S('UI:OnlyForThisList').'    '; - $sHtml .= ' '.Dict::S('UI:ForAllLists').'

    '; + $sHtml .= "iListId}\" type=\"checkbox\" $sSaveChecked name=\"save_settings\">"; + $sHtml .= "

    iListId}\" type=\"radio\" name=\"scope\" $sCustomChecked $sCustomDisabled value=\"this_list\">    '; + $sHtml .= "iListId}\" type=\"radio\" name=\"scope\" $sGenericChecked value=\"defaults\">

    '; $sHtml .= "
    "; $sHtml .= '
    '; $sHtml .= ''; @@ -377,7 +377,11 @@ EOF; public function GetHTMLTable(WebPage $oPage, $aColumns, $sSelectMode, $iPageSize, $bViewLink, $aExtraParams) { - $iNbPages = ($iPageSize == -1) ? 1 : ceil($this->iNbObjects / $iPageSize); + $iNbPages = ($iPageSize < 1) ? 1 : ceil($this->iNbObjects / $iPageSize); + if ($iPageSize < 1) + { + $iPageSize = -1; // convention: no pagination + } $aAttribs = $this->GetHTMLTableConfig($aColumns, $sSelectMode, $bViewLink); $aValues = $this->GetHTMLTableValues($aColumns, $sSelectMode, $iPageSize, $bViewLink, $aExtraParams); @@ -432,7 +436,7 @@ EOF; } if ($bViewLink) { - $iColOffset += 1; +// $iColOffset += 1; } foreach($aRealSortOrder as $sColCode => $bAscending) { @@ -443,7 +447,7 @@ EOF; } else if($sColCode == 'friendlyname' && $bViewLink) { - $aDefaultSort[] = "[".($iColOffset-1).",".($bAscending ? '0' : '1')."]"; + $aDefaultSort[] = "[".($iColOffset).",".($bAscending ? '0' : '1')."]"; } } $sSortList = ''; @@ -491,10 +495,18 @@ EOF public function UpdatePager(WebPage $oPage, $iDefaultPageSize, $iStart) { - $iPageSize = $iDefaultPageSize; + $iPageSize = ($iDefaultPageSize < 1) ? 1 : $iDefaultPageSize; $iPageIndex = 1 + floor($iStart / $iPageSize); $sHtml = $this->GetPager($oPage, $iPageSize, $iDefaultPageSize, $iPageIndex); $oPage->add_ready_script("$('#pager{$this->iListId}').html('".str_replace("\n", ' ', addslashes($sHtml))."');"); + if ($iDefaultPageSize < 1) + { + $oPage->add_ready_script("$('#pager{$this->iListId}').hide()"); + } + else + { + $oPage->add_ready_script("$('#pager{$this->iListId}').show()"); + } } } @@ -504,20 +516,21 @@ class DataTableSettings implements Serializable public $sTableId; public $iDefaultPageSize; public $aColumns; + public function __construct($aClassAliases, $sTableId = null) { $this->aClassAliases = $aClassAliases; $this->sTableId = $sTableId; $this->iDefaultPageSize = 10; - $this->aColumns = array(); + $this->aColumns = array(); } protected function Init($iDefaultPageSize, $aSortOrder, $aColumns) { $this->iDefaultPageSize = $iDefaultPageSize; $this->aColumns = $aColumns; - $this->FixVisibleColumns(); + $this->FixVisibleColumns(); } public function serialize() @@ -654,6 +667,7 @@ class DataTableSettings implements Serializable { $pref = null; $oSettings = new DataTableSettings($aClassAliases, $sTableId); + if ($sTableId != null) { // An identified table, let's fetch its own settings (if any) @@ -671,9 +685,28 @@ class DataTableSettings implements Serializable } } $oSettings->unserialize($pref); + return $oSettings; } + public function GetSortOrder() + { + $aSortOrder = array(); + foreach($this->aColumns as $sAlias => $aColumns) + { + foreach($aColumns as $aColumn) + { + if ($aColumn['sort'] != 'none') + { + $sCode = ($aColumn['code'] == '_key_') ? 'friendlyname' : $aColumn['code']; + $aSortOrder[$sCode] = ($aColumn['sort']=='asc'); // true for ascending, false for descending + } + } + break; // TODO: For now the Set object supports only sorting on the first class of the set + } + return $aSortOrder; + } + public function Save() { if ($this->sTableId == null) return false; // Cannot save, the table is not identified, use SaveAsDefault instead diff --git a/core/dbobjectset.class.php b/core/dbobjectset.class.php index 931c8f8c2..cbd1cab21 100644 --- a/core/dbobjectset.class.php +++ b/core/dbobjectset.class.php @@ -316,6 +316,19 @@ class DBObjectSet $this->m_iLimitStart = $iLimitStart; } + public function SetOrderBy($aOrderBy) + { + if ($this->m_aOrderBy != $aOrderBy) + { + $this->m_aOrderBy = $aOrderBy; + if ($this->m_bLoaded) + { + $this->m_bLoaded = false; + $this->Load(); + } + } + } + public function GetLimitCount() { return $this->m_iLimitCount; diff --git a/js/datatable.js b/js/datatable.js index 7ee497901..65da32534 100644 --- a/js/datatable.js +++ b/js/datatable.js @@ -60,6 +60,21 @@ $(function() oParams.display_key = this.options.sViewLink; oParams.class_aliases = this.options.oClassAliases; oParams.columns = this.options.oColumns; + var iSortCol = 0; + for(var k1 in oParams.columns) //Aliases + { + for(var k2 in oParams.columns[k1]) //Attribute codes + { + if (oParams.columns[k1][k2].sort != 'none') + { + oParams.sort_col = iSortCol; + oParams.sort_order = oParams.columns[k1][k2].sort; + break; //TODO make this more generic, Sort on just one column for now + } + iSortCol++; + } + break; //TODO: DBObjectSet supports only sorting on the first alias of the set + } var sId = new String(this.element.attr('id')); var sListId = sId.replace('datatable_', ''); @@ -129,10 +144,10 @@ $(function() var oColumns = $('#datatable_dlg_'+sListId).find(':itop-fieldsorter').fieldsorter('get_params'); var iPageSize = parseInt($('#datatable_dlg_'+sListId+' input[name=page_size]').val(), 10); - oOptions = {oColumns: oColumns, iPageSize: iPageSize }; + oOptions = {oColumns: oColumns, iPageSize: iPageSize, iDefaultPageSize: iPageSize }; } this._setOptions(oOptions); - + // Check if we need to save the settings or not... var oSaveCheck = $('#datatable_dlg_'+sListId).find('input[name=save_settings]'); var oSaveScope = $('#datatable_dlg_'+sListId).find('input[name=scope]:checked'); @@ -148,6 +163,7 @@ $(function() } } this._saveDlgState(); + }, onDlgCancel: function() { @@ -166,7 +182,12 @@ $(function() var oSaveCheck = $('#datatable_dlg_'+sListId).find('input[name=save_settings]'); if (oSaveCheck.attr('checked')) { - $('#datatable_dlg_'+sListId).find('input[name=scope]').removeAttr('disabled'); + $('#datatable_dlg_'+sListId).find('input[name=scope]').each(function() { + if ($(this).attr('stay-disabled') != 'true') + { + $(this).removeAttr('disabled'); + } + }); } else { @@ -183,6 +204,7 @@ $(function() var sId = new String(this.element.attr('id')); var sListId = sId.replace('datatable_', ''); $('#sfl_'+sListId).remove(); + $('#datatable_dlg_'+sListId).remove(); // call the original destroy method since we overwrote it $.Widget.prototype.destroy.call( this ); diff --git a/pages/ajax.render.php b/pages/ajax.render.php index fb39bffb5..651f07835 100644 --- a/pages/ajax.render.php +++ b/pages/ajax.render.php @@ -174,6 +174,7 @@ try { $aColumns[$sAlias][$sAttCode]['checked'] = ($aData['checked'] == 'true'); $aColumns[$sAlias][$sAttCode]['disabled'] = ($aData['disabled'] == 'true'); + $aColumns[$sAlias][$sAttCode]['sort'] = ($aData['sort']); } }