Skip to content

Commit 8521f65

Browse files
committed
feat(js): datatables js cont;
- Specified dt column types based on ds types. - Set nowrap for column headers. - Started working on reset button state.
1 parent a1cbbf7 commit 8521f65

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

ckanext/datatablesview/assets/datatablesview.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ table.dataTable span.dtcc input::placeholder{
157157
border-top: 8px solid rgba(0, 0, 0, 0.5);
158158
}
159159

160+
#dtprv_wrapper .dt-column-header .dt-column-title{
161+
white-space: nowrap !important;
162+
}
163+
160164
.dt-view-wrapper i.fa-info-circle{
161165
cursor: help;
162166
color: darkblue;

ckanext/datatablesview/assets/datatablesview.js

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,15 @@ function load_datatable(CKAN_MODULE){
154154
'numeric',
155155
'float',
156156
'double',
157-
'money'
157+
'money',
158158
];
159159
const alphaTypes = [
160160
'text',
161-
'_text'
161+
'_text',
162162
];
163163
const dateTypes = [
164-
'timestamp'
164+
'timestamp',
165+
'date',
165166
]
166167
const colOffset = 1; // _id col
167168
const defaultSortOrder = [[0, "asc"]]; // _id col
@@ -196,10 +197,25 @@ function load_datatable(CKAN_MODULE){
196197
}];
197198

198199
for( let i = 0; i < dataDictionary.length; i++ ){
200+
/**
201+
* Available data types for DataTables JS found here:
202+
* https://datatables.net/reference/option/columns.type
203+
*
204+
* TODO: make this pluggable with CKAN_MODULE.options
205+
* somehow to allow for extensions that use more
206+
* specific DataStore types???
207+
*/
208+
let _colType = 'string';
209+
if( numberTypes.includes(dataDictionary[i].type) ){
210+
_colType = 'num';
211+
}else if( dateTypes.includes(dataDictionary[i].type) ){
212+
_colType = 'date';
213+
}
199214
availableColumns.push({
200215
"name": dataDictionary[i].id,
201216
"data": dataDictionary[i].id,
202217
"searchable": true,
218+
"type": _colType,
203219
"render": function(_data, _type, _row, _meta){
204220
return cell_renderer(_data, _type, _row, _meta, dataDictionary[i]);
205221
}
@@ -732,9 +748,11 @@ function load_datatable(CKAN_MODULE){
732748
* Set selected rows based on table saved state.
733749
*/
734750
// FIXME: row selects state...
735-
if( typeof tableState != 'undefined' && typeof tableState.selected != 'undefined' ){
736-
table.rows(tableState.selected).select();
751+
if( typeof tableState == 'undefined' || tableState == null ){
752+
return;
737753
}
754+
755+
table.rows(tableState.selected).select();
738756
}
739757

740758
function set_button_states(){
@@ -748,15 +766,34 @@ function load_datatable(CKAN_MODULE){
748766
});
749767
}
750768

769+
if( typeof tableState == 'undefined' || tableState == null ){
770+
return; // nothing changed
771+
}
772+
751773
let tableModified = false;
752774

753-
$('.dt-buttons button.resetButton').removeClass('btn-secondary').removeClass('disabled').addClass('btn-warning');
754-
// TODO: reset button control
755-
// if( typeof tableState != 'undefined' && tableState != _data ){
756-
// $('.dt-buttons button.resetButton').removeClass('btn-secondary').removeClass('disabled').addClass('btn-warning');
757-
// }else{
758-
// $('.dt-buttons button.resetButton').removeClass('btn-warning').addClass('btn-secondary').addClass('disabled');
759-
// }
775+
if( tableState.page_number != 0 ){
776+
tableModified = true;
777+
}
778+
if( tableState.page_length != pageLengthChoices[0] ){
779+
tableModified = true;
780+
}
781+
if( tableState.selected.length > 0 ){
782+
tableModified = true;
783+
}
784+
if( tableState.sort_order != defaultSortOrder ){
785+
tableModified = true;
786+
}
787+
if( tableState.compact_view != defaultCompactView ){
788+
tableModified = true;
789+
}
790+
791+
if( ! tableModified ){
792+
$('.dt-buttons button.resetButton').addClass('btn-secondary').addClass('disabled').removeClass('btn-warning').attr('disabled', true);
793+
return; // state is same as default state
794+
}
795+
796+
$('.dt-buttons button.resetButton').removeClass('btn-secondary').removeClass('disabled').addClass('btn-warning').attr('disabled', false);
760797
}
761798

762799
function set_table_visibility(){

0 commit comments

Comments
 (0)