@@ -91,7 +91,20 @@ public function redcap_survey_page_top($project_id, $record, $instrument, $event
91
91
$ this ->initHook ($ record , $ instrument , $ event_id , true , $ group_id , $ repeat_instance );
92
92
$ this ->pageTop ();
93
93
}
94
-
94
+
95
+ public function redcap_module_ajax ($ action , $ payload , $ project_id , $ record , $ instrument , $ event_id , $ repeat_instance , $ survey_hash , $ response_id , $ survey_queue_hash , $ page , $ page_full , $ user_id , $ group_id ) {
96
+ $ this ->initHook ($ record , $ instrument , $ event_id , false , $ group_id , $ repeat_instance );
97
+ if ($ action == "get-data " ) {
98
+ $ event = intval ($ payload ["event_id " ]);
99
+ $ form = $ this ->framework ->escape ($ payload ["form_name " ]);
100
+ $ fields = $ this ->framework ->escape ($ payload ["fields " ]);
101
+ $ filter = $ payload ["filter " ];
102
+ $ hideChoiceValues = ($ payload ["hide_vals " ]=='1 ' );
103
+ $ data = $ this ->getInstanceData ($ record , $ event , $ form , $ fields , $ filter , true , $ hideChoiceValues );
104
+ return $ data ;
105
+ }
106
+ }
107
+
95
108
protected function initHook ($ record , $ instrument , $ event_id , $ isSurvey , $ group_id , $ repeat_instance ) {
96
109
global $ Proj , $ lang , $ user_rights ;
97
110
$ this ->Proj = $ Proj ;
@@ -246,9 +259,6 @@ protected function setTaggedFields() {
246
259
reset ($ includeVars );
247
260
$ repeatingFormDetails ['var_list ' ] = $ includeVars ;
248
261
249
- $ ajaxUrl = $ this ->getUrl ('instance_table_ajax.php ' );
250
- $ filter = htmlspecialchars (str_replace ("' " ,self ::REPLQUOTE_SINGLE ,str_replace ('" ' ,self ::REPLQUOTE_DOUBLE ,$ filter )), ENT_QUOTES );
251
-
252
262
if (preg_match ("/ " .self ::ACTION_TAG_SCROLLX ."/ " , $ fieldDetails ['field_annotation ' ])) {
253
263
$ repeatingFormDetails ['scroll_x ' ] = true ;
254
264
} else {
@@ -276,7 +286,12 @@ protected function setTaggedFields() {
276
286
$ repeatingFormDetails ['hide_choice_values ' ] = false ;
277
287
}
278
288
279
- $ repeatingFormDetails ['ajax_url ' ] = $ ajaxUrl ."$ hideVals&record= {$ this ->record }&event_id= $ eventId&form_name= $ formName&filter= $ filter&fields= " .implode ('| ' ,$ includeVars );
289
+ $ repeatingFormDetails ["ajax " ] = [
290
+ "event_id " => $ eventId ,
291
+ "form_name " => $ formName ,
292
+ "filter " => $ filter ,
293
+ "fields " => $ includeVars
294
+ ];
280
295
$ repeatingFormDetails ['markup ' ] = '' ;
281
296
282
297
$ this ->taggedFields [] = $ repeatingFormDetails ;
@@ -420,7 +435,6 @@ public function getInstanceData($record, $event, $form, $fields, $filter, $inclu
420
435
$ this ->user_rights = &$ user_rights ;
421
436
$ this ->isSurvey = (PAGE ==='surveys/index.php ' );
422
437
$ instanceData = array ();
423
- $ filter = str_replace (self ::REPLQUOTE_SINGLE ,"' " ,str_replace (self ::REPLQUOTE_DOUBLE ,'" ' ,$ filter ));
424
438
425
439
// find any descriptive text fields tagged with @FORMINSTANCETABLE=form_name
426
440
@@ -615,6 +629,8 @@ protected function makeOntologyDisplay($val, $service, $category) {
615
629
616
630
protected function insertJS () {
617
631
global $ lang ;
632
+ $ this ->framework ->initializeJavascriptModuleObject ();
633
+ $ jsmo_name = $ this ->framework ->getJavascriptModuleObjectName ();
618
634
?>
619
635
<style type="text/css">
620
636
.<?php echo self ::MODULE_VARNAME ;?> tbody tr { font-weight:normal; }
@@ -633,8 +649,10 @@ protected function insertJS() {
633
649
var lengthVal;
634
650
var lengthLbl;
635
651
var lengthChange;
652
+ var JSMO = <?= $ jsmo_name?> ;
636
653
637
654
function init() {
655
+ // console.log('Instance Table', config);
638
656
config.forEach(function(taggedField) {
639
657
taggedFieldNames.push(taggedField.field_name);
640
658
$('#'+taggedField.field_name+'-tr td:last')
@@ -654,7 +672,9 @@ function init() {
654
672
lengthVal = lengthLbl = [taggedField.page_size];
655
673
lengthChange = false;
656
674
}
657
- var thisTbl = $('#'+taggedField.html_table_id)
675
+ var thisTbl;
676
+ if (isSurvey) {
677
+ thisTbl = $('#'+taggedField.html_table_id)
658
678
.DataTable( {
659
679
"stateSave": true,
660
680
"stateDuration": 0,
@@ -673,11 +693,36 @@ function init() {
673
693
"targets": "_all"
674
694
}]
675
695
} );
676
- if (!taggedField.show_instance_col) {
677
- thisTbl.column( 0 ).visible( false );
696
+ if (!taggedField.show_instance_col) {
697
+ thisTbl.column( 0 ).visible( false );
698
+ }
678
699
}
679
- if (!isSurvey) {
680
- thisTbl.ajax.url(taggedField.ajax_url).load();
700
+ else {
701
+ JSMO.ajax('get-data', taggedField.ajax).then(function(data) {
702
+ thisTbl = $('#'+taggedField.html_table_id)
703
+ .DataTable( {
704
+ "stateSave": true,
705
+ "stateDuration": 0,
706
+ "lengthMenu": [lengthVal, lengthLbl],
707
+ "lengthChange": lengthChange,
708
+ "columnDefs": [{
709
+ "render": function (data, type, row) {
710
+ let val = data;
711
+ if ($.isPlainObject(data)) {
712
+ if (data.hasOwnProperty(type)) { // e.g. sort, filter for dates
713
+ val = data[type];
714
+ }
715
+ }
716
+ return val;
717
+ },
718
+ "targets": "_all"
719
+ }],
720
+ "data": data
721
+ } );
722
+ if (!taggedField.show_instance_col) {
723
+ thisTbl.column( 0 ).visible( false );
724
+ }
725
+ });
681
726
}
682
727
});
683
728
@@ -714,12 +759,14 @@ function refreshTableDialog() {
714
759
,function() {
715
760
// refresh all instance tables (e.g. to pick up changes to multiple forms across repeating event
716
761
$('.'+tableClass).each(function() {
717
- $ (this).DataTable().ajax.reload( null, false ); // don't reset user paging on reload
762
+ performTableRefresh (this);
718
763
});
719
764
},langYes
720
765
);
721
766
}
722
767
768
+
769
+
723
770
return {
724
771
addNewInstance: function(record, event, form, linkFld, linkIns) {
725
772
var ref = (linkFld=='')?'':'&link_field='+linkFld+'&link_instance='+linkIns;
@@ -729,6 +776,12 @@ function refreshTableDialog() {
729
776
editInstance: function(record, event, form, instance) {
730
777
instancePopup('View instance', record, event, form, instance);
731
778
return false;
779
+ },
780
+ getConfig: function() {
781
+ return config;
782
+ },
783
+ getJSMO: function() {
784
+ return JSMO;
732
785
}
733
786
}
734
787
})(window, document, jQuery, app_path_webroot, pid, simpleDialog);
@@ -743,7 +796,20 @@ function refreshTables() {
743
796
function actuallyRefreshTables() {
744
797
var tableClass = '<?php echo self ::MODULE_VARNAME ;?> ';
745
798
$('.'+tableClass).each(function() {
746
- $(this).DataTable().ajax.reload( null, false ); // don't reset user paging on reload
799
+ performTableRefresh(this);
800
+ });
801
+ }
802
+
803
+ function performTableRefresh(tbl) {
804
+ var id = tbl.id;
805
+ var config = <?= self ::MODULE_VARNAME ?> .getConfig();
806
+ var jsmo = <?= self ::MODULE_VARNAME ?> .getJSMO();
807
+ var it = config.filter(function(x) { return x.html_table_id == id; })[0];
808
+ jsmo.ajax('get-data', it.ajax).then(function(data) {
809
+ var dt = $(tbl).DataTable();
810
+ dt.clear();
811
+ dt.rows.add(data);
812
+ dt.draw();
747
813
});
748
814
}
749
815
@@ -912,4 +978,4 @@ public function redcap_every_page_before_render($project_id) {
912
978
}
913
979
}
914
980
}
915
- }
981
+ }
0 commit comments