Skip to content

Commit c58903b

Browse files
committed
Migrate datatables from client to server side
1 parent 0ed8dbc commit c58903b

File tree

2 files changed

+102
-17
lines changed

2 files changed

+102
-17
lines changed

assets/src/legacy/attributeTable.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,8 +1544,13 @@ var lizAttributeTable = function() {
15441544
myDom = '<<t>ipl>';
15451545
}
15461546

1547+
const datatablesUrl = globalThis['lizUrls'].wms.replace('service', 'datatables');
1548+
const params = globalThis['lizUrls'].params;
1549+
params['layerId'] = lConfig.id;
1550+
15471551
$( aTable ).dataTable( {
1548-
data: dataSet
1552+
serverSide: true
1553+
,ajax: datatablesUrl + '?' + new URLSearchParams(params).toString()
15491554
,columns: columns
15501555
,initComplete: function(settings, json) {
15511556
const api = new $.fn.dataTable.Api(settings);
@@ -1562,13 +1567,13 @@ var lizAttributeTable = function() {
15621567
,order: [[ firstDisplayedColIndex, "asc" ]]
15631568
,language: { url:globalThis['lizUrls']["dataTableLanguage"] }
15641569
,deferRender: true
1565-
,createdRow: function ( row, data, dataIndex ) {
1566-
if ( $.inArray( data.DT_RowId.toString(), lConfig['selectedFeatures'] ) != -1
1567-
) {
1568-
$(row).addClass('selected');
1569-
data.lizSelected = 'a';
1570-
}
1571-
}
1570+
// ,createdRow: function ( row, data, dataIndex ) {
1571+
// if ( $.inArray( data.DT_RowId.toString(), lConfig['selectedFeatures'] ) != -1
1572+
// ) {
1573+
// $(row).addClass('selected');
1574+
// data.lizSelected = 'a';
1575+
// }
1576+
// }
15721577
,drawCallback: function (settings) {
15731578
// rendering ok, find img with data-attr-thumbnail
15741579
const thumbnailColl = document.getElementsByClassName('data-attr-thumbnail');
@@ -1577,7 +1582,7 @@ var lizAttributeTable = function() {
15771582
}
15781583
}
15791584
,dom: myDom
1580-
,pageLength: 50
1585+
,pageLength: 10
15811586
,scrollY: '95%'
15821587
,scrollX: '100%'
15831588

@@ -1661,11 +1666,11 @@ var lizAttributeTable = function() {
16611666
const columns = [];
16621667
let firstDisplayedColIndex = 0;
16631668
// Column with selected status
1664-
columns.push( {"data": "lizSelected", "width": "25px", "searchable": false, "sortable": true, "visible": false} );
1665-
firstDisplayedColIndex+=1;
1669+
// columns.push( {"data": "lizSelected", "width": "25px", "searchable": false, "sortable": true, "visible": false} );
1670+
// firstDisplayedColIndex+=1;
16661671

1667-
columns.push({ "data": "featureToolbar", "width": "25px", "searchable": false, "sortable": false});
1668-
firstDisplayedColIndex += 1;
1672+
// columns.push({ "data": "featureToolbar", "width": "25px", "searchable": false, "sortable": false});
1673+
// firstDisplayedColIndex += 1;
16691674

16701675
// Add column for each field
16711676
for (var columnName in atFeatures[0].properties){
@@ -1897,11 +1902,11 @@ var lizAttributeTable = function() {
18971902

18981903
// Add row ID
18991904
line['DT_RowId'] = fid;
1900-
line['lizSelected'] = 'z';
1905+
// line['lizSelected'] = 'z';
19011906

1902-
if( selectedFeatures && $.inArray( fid, selectedFeatures ) != -1 )
1903-
line.lizSelected = 'a';
1904-
line['featureToolbar'] = `<lizmap-feature-toolbar value="${layerId + '.' + fid}" ${isChild ? `parent-layer-id="${parentLayerID}"`: ''} ${pivotId ? `pivot-layer="${pivotId}"`: ''}></lizmap-feature-toolbar>`;
1907+
// if( selectedFeatures && $.inArray( fid, selectedFeatures ) != -1 )
1908+
// line.lizSelected = 'a';
1909+
// line['featureToolbar'] = `<lizmap-feature-toolbar value="${layerId + '.' + fid}" ${isChild ? `parent-layer-id="${parentLayerID}"`: ''} ${pivotId ? `pivot-layer="${pivotId}"`: ''}></lizmap-feature-toolbar>`;
19051910

19061911
// Build table lines
19071912
for (var idx in feat.properties){
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/**
4+
* Send data to datatables ajax requests.
5+
*
6+
* @author 3liz
7+
* @copyright 2025 3liz
8+
*
9+
* @see https://3liz.com
10+
*
11+
* @license Mozilla Public License : http://www.mozilla.org/MPL/
12+
*/
13+
14+
use Lizmap\Request\WFSRequest;
15+
16+
class datatablesCtrl extends jController
17+
{
18+
public function index()
19+
{
20+
$rep = $this->getResponse('binary');
21+
$rep->outputFileName = 'datatables.json';
22+
$rep->mimeType = 'application/json';
23+
24+
// Lizmap parameters
25+
$repository = $this->param('repository');
26+
$project = $this->param('project');
27+
$layerId = $this->param('layerId');
28+
29+
// DataTables parameters
30+
$DTStart = $this->param('start');
31+
$DTLength = $this->param('length');
32+
33+
$DTOrder = $this->param('order');
34+
$DTColumns = $this->param('columns');
35+
$DTOrderColumnIndex = $DTOrder[0]['column'];
36+
$DTOrderColumnDirection = $DTOrder[0]['dir'] == 'desc' ? 'd' : '';
37+
$DTOrderColumnName = $DTColumns[$DTOrderColumnIndex]['data'];
38+
39+
$DTSearch = $this->param('search');
40+
41+
$lproj = lizmap::getProject($repository.'~'.$project);
42+
$layer = $lproj->getLayer($layerId);
43+
$typeName = $layer->getWfsTypeName();
44+
45+
$wfsparams = array(
46+
'SERVICE' => 'WFS',
47+
'VERSION' => '1.0.0',
48+
'REQUEST' => 'GetFeature',
49+
'TYPENAME' => $typeName,
50+
'OUTPUTFORMAT' => 'GeoJSON',
51+
'GEOMETRYNAME' => 'none',
52+
'MAXFEATURES' => $DTLength,
53+
'SORTBY' => $DTOrderColumnName.' '.$DTOrderColumnDirection,
54+
'STARTINDEX' => $DTStart,
55+
);
56+
57+
$wfsrequest = new WFSRequest($lproj, $wfsparams, lizmap::getServices());
58+
$wfsresponse = $wfsrequest->process();
59+
$featureData = $wfsresponse->getBodyAsString();
60+
$jsonFeatures = json_decode($featureData)->features;
61+
$data = array();
62+
foreach ($jsonFeatures as $key => $feature) {
63+
$data[] = $feature->properties;
64+
}
65+
// jLog::log(var_export(, true), 'error');
66+
67+
$returnedData = array(
68+
'draw' => (int) $this->param('draw'),
69+
'recordsTotal' => 31, // TODO: get the total number of features
70+
'recordsFiltered' => 31,
71+
'data' => $data,
72+
// 'error' => 'error',
73+
);
74+
75+
$rep->content = json_encode($returnedData);
76+
// $rep->content = $wfsresponse;
77+
78+
return $rep;
79+
}
80+
}

0 commit comments

Comments
 (0)