KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Configurable AJAX Parameter Name for DataTables #3463

Open APP opened this topic on 5 days ago - 1 comments

APP commented 5 days ago

Problem

DataTables currently hardcodes the parameter name id for identifying table instances in AJAX requests (line 417 in DataTables.php):

"function(d) {
    d.id = '{$this->name}';
    // ...
}"

This causes conflicts with frameworks that use id as a reserved parameter (e.g., for page IDs, record IDs). Validation errors occur when the framework expects id to be an integer but receives a string like "DataTables_1".

Proposed Solution

Add a new configuration option ajaxIdParam (default: 'id') to allow users to customize the parameter name:

DataTables::create([
    'serverSide' => true,
    'ajaxIdParam' => 'dtId', // Custom parameter name instead of 'id'
    // ... other options
]);

Implementation

File: vendor/koolreport/datagrid/DataTables.php

Change 1: Line ~417 (AJAX data function)

// BEFORE:
'data' => "function(d) {
    d.id = '{$this->name}';

// AFTER:
$ajaxIdParam = Util::get($this->params, 'ajaxIdParam', 'id');
'data' => "function(d) {
    d.{$ajaxIdParam} = '{$this->name}';

Change 2: Line ~1508 (Server-side request parsing)

// BEFORE:
$id = Util::get($request, 'id', null);

// AFTER:
$ajaxIdParam = Util::get($this->params, 'ajaxIdParam', 'id');
$id = Util::get($request, $ajaxIdParam, null);

Benefits

  1. Backward compatible: Defaults to 'id' - existing code works unchanged
  2. Simple: Single configuration option, ~4 lines of code
  3. Solves real-world issue: Eliminates parameter name conflicts with frameworks
  4. Minimal risk: Only affects parameter name, not functionality

Use Case

// Works with frameworks that reserve 'id' parameter
DataTables::create([
    'serverSide' => true,
    'ajaxIdParam' => 'tableId', // No conflict with framework's 'id' parameter
    'ajaxUrl' => '?action=getData',
    // ...
]);

Urgency

Affects integration with many PHP frameworks (Laravel, Symfony, custom CMSes) that use id as a standard parameter.

Current Workaround: Manual patching of vendor files (not ideal for composer updates)

As a long time pro customer we really would like this to be fixed.

Sebastian Morales commented 45 mins ago

Wow, it is such a perfect feedback every dev would like to receive. We have nothing more to say other than a big thank you and this fix will definitely be added to the next release of KoolReport Pro and Datagrid package. Regards,

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None