Skip to content

Commit 107897f

Browse files
committed
SORTCOL further dev
- Further dev and docs for @INSTANCETABLE-SORTCOL - Fixes for @INSTANCETABLE-PAGESIZE
1 parent 639feb7 commit 107897f

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

InstanceTable.php

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class InstanceTable extends AbstractExternalModule
4545
const ACTION_TAG_HIDECHOICEVALUES = '@INSTANCETABLE[-_]HIDECHOICEVALUES';
4646
const ACTION_TAG_HIDEFORMSTATUS = '@INSTANCETABLE[-_]HIDEFORMSTATUS';
4747
const ACTION_TAG_HIDEFORMINMENU = '@INSTANCETABLE[-_]HIDEFORMINMENU';
48-
const ACTION_TAG_ORDER = '@INSTANCETABLE[-_]ORDER'; // Specify custom sort order, e.g. '@INSTANCETABLE_ORDER=2:asc' for column 2 ascending
48+
const ACTION_TAG_SORTCOL = '@INSTANCETABLE[-_]SORTCOL'; // Specify custom sort order, e.g. '@INSTANCETABLE_ORDER=2:asc' for column 2 ascending
4949
const ACTION_TAG_PREFILL = '@INSTANCETABLE[-_]PREFILL';
5050
const ADD_NEW_BTN_YSHIFT = '0px';
5151
const MODULE_VARNAME = 'MCRI_InstanceTable';
@@ -311,15 +311,14 @@ protected function setTaggedFields() {
311311

312312
// pick up option for custom sort order
313313
$matches = array();
314-
if (preg_match("/".self::ACTION_TAG_ORDER."\s*=\s*'?(\d+):(\w+)'?\s?/", $fieldDetails['field_annotation'], $matches)) {
315-
$orderColumn = intval($matches[1]);
316-
$orderDirection = strtolower($matches[2]) === 'asc' ? 'asc' : 'desc';
317-
$repeatingFormDetails['order_column'] = $orderColumn;
318-
$repeatingFormDetails['order_direction'] = $orderDirection;
314+
if (preg_match("/".self::ACTION_TAG_SORTCOL."\s*=\s*[\"']?(\d+)(:asc|:desc)?[\"']?\s?/i", $fieldDetails['field_annotation'], $matches)) {
315+
$sortColumn = intval($matches[1]);
316+
$sortDirection = (array_key_exists(2, $matches)) ? strtolower($matches[2]) : null;
317+
$repeatingFormDetails['sort_column'] = ($sortColumn>1) ? $sortColumn-1 : 0; // use 1-based indexing in config, DT uses 0-based indexes for column refs
318+
$repeatingFormDetails['sort_direction'] = ($sortDirection==':desc') ? 'desc' : 'asc';
319319
} else {
320-
// Default order
321-
$repeatingFormDetails['order_column'] = 1;
322-
$repeatingFormDetails['order_direction'] = 'desc';
320+
$repeatingFormDetails['sort_column'] = 1;
321+
$repeatingFormDetails['sort_direction'] = 'asc';
323322
}
324323

325324
// pick up option for field prefilling in new instances
@@ -717,9 +716,6 @@ protected function insertJS() {
717716
var langNo = '<?php echo js_escape($this->lang['design_99']);?>';
718717
var config = <?php echo json_encode($this->taggedFields, JSON_PRETTY_PRINT);?>;
719718
var taggedFieldNames = [];
720-
var lengthVal;
721-
var lengthLbl;
722-
var lengthChange;
723719
var JSMO = <?=$jsmo_name?>;
724720

725721
function init() {
@@ -730,28 +726,28 @@ function init() {
730726
.append(taggedField.markup);
731727
switch(taggedField.page_size) {
732728
case 0:
733-
lengthVal = [-1, 10, 25, 50, 100];
734-
lengthLbl = ["<?=$lang['docs_44']?>", 10, 25, 50, 100]; // "ALL"
735-
lengthChange = true;
729+
taggedField.lengthVal = [10, 25, 50, 100, -1];
730+
taggedField.lengthLbl = [10, 25, 50, 100, "<?=$lang['docs_44']?>"]; // "ALL"
731+
taggedField.lengthChange = true;
736732
break;
737733
case -1:
738-
lengthVal = [-1];
739-
lengthLbl = ["<?=$lang['docs_44']?>"]; // "ALL"
740-
lengthChange = false;
734+
taggedField.lengthVal = [-1];
735+
taggedField.lengthLbl = ["<?=$lang['docs_44']?>"]; // "ALL"
736+
taggedField.lengthChange = false;
741737
break;
742738
default:
743-
lengthVal = lengthLbl = [taggedField.page_size];
744-
lengthChange = false;
739+
taggedField.lengthVal = lengthLbl = [taggedField.page_size];
740+
taggedField.lengthChange = false;
745741
}
746742
var thisTbl;
747743
if (isSurvey) {
748744
thisTbl = $('#'+taggedField.html_table_id)
749745
.DataTable( {
750746
"stateSave": true,
751747
"stateDuration": 0,
752-
"lengthMenu": [lengthVal, lengthLbl],
753-
"lengthChange": lengthChange,
754-
"order": [[taggedField.order_column, taggedField.order_direction]], // Sort the column (defined in action tag).
748+
"lengthMenu": [taggedField.lengthVal, taggedField.lengthLbl],
749+
"lengthChange": taggedField.lengthChange,
750+
"order": [[taggedField.sort_column, taggedField.sort_direction]],
755751
"columnDefs": [{
756752
"render": function (data, type, row) {
757753
let val = data;
@@ -768,6 +764,9 @@ function init() {
768764
if (!taggedField.show_instance_col) {
769765
thisTbl.column( 0 ).visible( false );
770766
}
767+
if (taggedField.page_size!==0) {
768+
thisTbl.page.len(taggedField.page_size).draw();
769+
}
771770
}
772771
else {
773772
if (taggedField.hide_form_in_menu) {
@@ -779,9 +778,9 @@ function init() {
779778
.DataTable( {
780779
"stateSave": true,
781780
"stateDuration": 0,
782-
"lengthMenu": [lengthVal, lengthLbl],
783-
"lengthChange": lengthChange,
784-
"order": [[taggedField.order_column, taggedField.order_direction]], // Sort the column (defined in action tag).
781+
"lengthMenu": [taggedField.lengthVal, taggedField.lengthLbl],
782+
"lengthChange": taggedField.lengthChange,
783+
"order": [[taggedField.sort_column, taggedField.sort_direction]],
785784
"columnDefs": [{
786785
"render": function (data, type, row) {
787786
let val = data;
@@ -799,6 +798,9 @@ function init() {
799798
if (!taggedField.show_instance_col) {
800799
thisTbl.column( 0 ).visible( false );
801800
}
801+
if (taggedField.page_size!==0) {
802+
thisTbl.page.len(taggedField.page_size).draw();
803+
}
802804
});
803805
}
804806
});

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ If you wish to provide the buttins that enable access to individual instances of
5454
* `@INSTANCETABLE-HIDECHOICEVALUES`: Suppress the display of choice field values and show only choice labels.
5555
* `@INSTANCETABLE-HIDEFORMSTATUS`: Suppress display of the form status field in data entry view. (The form status field is always suppressed in survey mode.)
5656
* `@INSTANCETABLE-HIDEFORMINMENU`: Hide the link to the repeating form in the Data Collection section of the project page menu.
57+
* `@INSTANCETABLE-SORTCOL=n[:direction]`: Specify the column index for the default table sort. The index number should be a positive integer, with <code>1</code> being the first column (i.e. instance number, which is always present even if hidden using `@INSTANCETABLE-HIDEINSTANCECOL`). Direction is optional and can be `asc` (default) or `desc` (case-insensitive) for ascending or descending respectively. The default sort in the absence of this tag is on the first column in ascending order, i.e. `@INSTANCETABLE-SORTCOL=1:asc]`. Note that your browser will remember any custom sorting that you apply, therefore this setting only applies a default sort when you first view an instance table.
5758
* `@INSTANCETABLE-PREFILL=rptformvar=[pagevar]`: Have fields on new instances pre-filled with data from the current form (or elsewhere on the record) using `fieldname=value` pairs in the URL in a manner similar to survey form field pre-filling.
5859

5960
### Tags Used for Fields on a Repeating Form

config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
"description": "Use <code>@INSTANCETABLE-HIDEFORMINMENU</code> to hide the link to the repeating form in the Data Collection section of the project page menu."
6666
},
6767
{
68-
"tag": "@INSTANCETABLE_ORDER",
69-
"description": "Use <code>@INSTANCETABLE_ORDER</code> to hide the link to the repeating form in the Project Setup section of the project page menu. i.e.<code>@INSTANCETABLE_ORDER=3:desc</code> to sort the table by the third column in descending order. The direction can be 'asc' or 'desc' (case-insensitive).If the action tag is not provided, it will default to sorting by column 1 in descending order"
68+
"tag": "@INSTANCETABLE-SORTCOL",
69+
"description": "Use <code>@INSTANCETABLE-SORTCOL=n:direction</code> to specify the column index for the default table sort. The index number should be a positive integer, with <code>1</code> being the first column (usually the instance number). Direction is optional and can be <code>asc</code> (default) or <code>desc</code> (case-insensitive) for ascending or descending respectively. The default sort in the absence of this tag is on the first column in ascending order, i.e. <code>@INSTANCETABLE-SORTCOL=1:asc]</code>"
7070
},
7171
{
7272
"tag": "@INSTANCETABLE-PREFILL",

0 commit comments

Comments
 (0)