ypnos-web / cakephp-datatables

CakePHP3 Plugin for DataTables plug-in for jQuery
MIT License
27 stars 24 forks source link

Uncaught ReferenceError: dt is not defined #56

Closed jabit closed 6 years ago

jabit commented 6 years ago

Hi,

I'm trying to get working your amazing plugin but without success. I am getting this error: error console: Uncaught ReferenceError: dt is not defined

Source code:

I realized that on your DataTablesHelper.php file, on line 105 if you write this: $json = CallbackFunction::resolve(json_encode($options)); The $json var return an empty value. But if I do a little trick like this: $json = CallbackFunction::resolve(json_encode($options, JSON_PARTIAL_OUTPUT_ON_ERROR)); I get the right json. Anyway... the trick does not fix the main issue "dt is not defined" It doesn't matter where I place the cakephp.dataTables.js I get alwais the same error. Could you please help me with that? Thank you in advance, Javier
ypnos-web commented 6 years ago

So I guess you are reporting two problems at once?

  1. empty json leading to malformed dt.initDataTables('#clients-table', ); call
  2. dt not being defined

For the first problem, it would be nice if you could provide the $options that causes malformed JSON. For the second problem, you need to ensure that you include cakephp.dataTables.js before that <script> tag. There is not much more to do about it. The file defines the dt object.

jabit commented 6 years ago

Hi Ypnos,

thank you for your quick answer.

  1. empty json leading to malformed dt.initDataTables('#clients-table', ); call

DataTablesHelper.php $json = CallbackFunction::resolve(json_encode($options)); debug(json_last_error_msg()); echo "
"; debug($options); die();

View: Clients /vendor/ypnos-web/cakephp-datatables/src/View/Helper/DataTablesHelper.php (line 106) 'Type is not supported'

/vendor/ypnos-web/cakephp-datatables/src/View/Helper/DataTablesHelper.php (line 108) ` [ 'ajax' => [ 'url' => '/clients' ], 'data' => object(Cake\ORM\Query) {

    '(help)' => 'This is a Query object, to get the results execute or iterate it.',
    'sql' => 'SELECT Clients.id AS `Clients__id`, Clients.identification AS `Clients__identification`, Clients.first_name AS `Clients__first_name`, Clients.last_name AS `Clients__last_name`, Clients.state AS `Clients__state`, Clients.city AS `Clients__city`, Clients.pc AS `Clients__pc`, Clients.address AS `Clients__address`, Clients.expedition_place AS `Clients__expedition_place`, Clients.birthdate AS `Clients__birthdate`, Clients.expedition_date AS `Clients__expedition_date`, Clients.nacionality AS `Clients__nacionality`, Clients.phone AS `Clients__phone`, Clients.mobile AS `Clients__mobile`, Clients.email AS `Clients__email`, Clients.note AS `Clients__note`, Clients.blacklist AS `Clients__blacklist` FROM clients Clients ORDER BY id asc LIMIT 10 OFFSET 0',
    'params' => [],
    'defaultTypes' => [
        'Clients__id' => 'integer',
        'Clients.id' => 'integer',
        'id' => 'integer',
        'Clients__identification' => 'string',
        'Clients.identification' => 'string',
        'identification' => 'string',
        'Clients__first_name' => 'string',
        'Clients.first_name' => 'string',
        'first_name' => 'string',
        'Clients__last_name' => 'string',
        'Clients.last_name' => 'string',
        'last_name' => 'string',
        'Clients__state' => 'string',
        'Clients.state' => 'string',
        'state' => 'string',
        'Clients__city' => 'string',
        'Clients.city' => 'string',
        'city' => 'string',
        'Clients__pc' => 'string',
        'Clients.pc' => 'string',
        'pc' => 'string',
        'Clients__address' => 'string',
        'Clients.address' => 'string',
        'address' => 'string',
        'Clients__expedition_place' => 'string',
        'Clients.expedition_place' => 'string',
        'expedition_place' => 'string',
        'Clients__birthdate' => 'date',
        'Clients.birthdate' => 'date',
        'birthdate' => 'date',
        'Clients__expedition_date' => 'datetime',
        'Clients.expedition_date' => 'datetime',
        'expedition_date' => 'datetime',
        'Clients__nacionality' => 'string',
        'Clients.nacionality' => 'string',
        'nacionality' => 'string',
        'Clients__phone' => 'string',
        'Clients.phone' => 'string',
        'phone' => 'string',
        'Clients__mobile' => 'string',
        'Clients.mobile' => 'string',
        'mobile' => 'string',
        'Clients__email' => 'string',
        'Clients.email' => 'string',
        'email' => 'string',
        'Clients__note' => 'binary',
        'Clients.note' => 'binary',
        'note' => 'binary',
        'Clients__blacklist' => 'binary',
        'Clients.blacklist' => 'binary',
        'blacklist' => 'binary'
    ],
    'decorators' => (int) 0,
    'executed' => true,
    'hydrate' => true,
    'buffered' => true,
    'formatters' => (int) 0,
    'mapReducers' => (int) 0,
    'contain' => [],
    'matching' => [],
    'extraOptions' => [],
    'repository' => object(App\Model\Table\ClientsTable) {

        'registryAlias' => 'Clients',
        'table' => 'clients',
        'alias' => 'Clients',
        'entityClass' => 'App\Model\Entity\Client',
        'associations' => [
            (int) 0 => 'batches'
        ],
        'behaviors' => [],
        'defaultConnection' => 'default',
        'connectionName' => 'default'

    }

},
'deferLoading' => (int) 2,
'columns' => [
    (int) 0 => [
        'data' => 'id',
        'visible' => false,
        'searchable' => false
    ],
    (int) 1 => [
        'title' => 'DNI',
        'data' => 'identification'
    ],
    (int) 2 => [
        'title' => 'Nombre',
        'data' => 'first_name'
    ],
    (int) 3 => [
        'title' => 'Apellidos',
        'data' => 'last_name'
    ],
    (int) 4 => [
        'title' => 'Provincia',
        'data' => 'state'
    ]
],
'order' => [
    (int) 0 => [
        (int) 0 => (int) 1,
        (int) 1 => 'asc'
    ]
],
'searching' => true,
'processing' => true,
'serverSide' => true,
'deferRender' => true,
'className' => 'DataTables.DataTables',
'language' => [
    'emptyTable' => 'No data available in table',
    'info' => 'Showing _START_ to _END_ of _TOTAL_ entries',
    'infoEmpty' => 'No entries to show',
    'infoFiltered' => '(filtered from _MAX_ total entries)',
    'lengthMenu' => 'Show _MENU_ entries',
    'processing' => 'Processing...',
    'search' => 'Search:',
    'zeroRecords' => 'No matching records found',
    'paginate' => [
        'first' => 'First',
        'last' => 'Last',
        'next' => 'Next',
        'previous' => 'Previous'
    ],
    'aria' => [
        'sortAscending' => ': activate to sort column ascending',
        'sortDescending' => ': activate to sort column descending'
    ]
]

] `

  1. dt not being defined Sorry was my fault, I did not load before the script :S

Thank you, Javier

ypnos-web commented 6 years ago

Well I don't know what you are doing that leads you to your problem, but I guess at this point it would go away if you were to set 'data' in the options array to $data->all() instead of $data or whatever your result query variable is called.

jabit commented 6 years ago

Here my controller: public function index() { $columns = [ [ 'field' => 'Clients.id', 'data' => 'id', 'visible' => false, 'searchable' => false, ], [ 'title' => ('DNI'), 'field' => 'Clients.identification', 'data' => 'identification' ], [ 'title' => ('Nombre'), 'field' => 'Clients.first_name', 'data' => 'first_name' ], [ 'title' => ('Apellidos'), 'field' => 'Clients.last_name', 'data' => 'last_name', ], [ 'title' => ('Provincia'), 'field' => 'Clients.state', 'data' => 'state' ], [ 'title' => ('Ciudad'), 'field' => 'Clients.city', 'data' => 'city' ], [ 'title' => ('C.P.'), 'field' => 'Clients.pc', 'data' => 'pc' ], [ 'title' => ('Direción'), 'field' => 'Clients.address', 'data' => 'address' ], [ 'title' => ('Fecha de nacimiento'), 'field' => 'Clients.birthdate', 'data' => 'birthdate' ], [ 'title' => ('Nacionalidad'), 'field' => 'Clients.nacionality', 'data' => 'nacionality' ], [ 'title' => ('Teléfono'), 'field' => 'Clients.phone', 'data' => 'phone' ], [ 'title' => ('Móvil'), 'field' => 'Clients.mobile', 'data' => 'mobile' ], [ 'title' => ('Email'), 'field' => 'Clients.email', 'data' => 'email' ] ]; $data = $this->DataTables->find('Clients', 'all', [ 'order' => ['id' => 'asc']], $columns); $this->set('columns', $columns); $this->set('data', $data); $this->set('_serialize', array_merge($this->viewVars['_serialize'], ['data'])); }

Here my view: <?php $options = [ 'ajax' => [ 'url' => $this->Url->build() ], 'data' => $data->all(), 'deferLoading' => $data->count(), 'columns' => $columns, 'order' => [1, 'asc'], 'scrollX' => true, 'responsive' => true ]; echo $this->DataTables->table('clients-table', $options, ['class' => 'display responsive nowrap', 'style' => 'width: 100%;']); ?> I get allwais the same error:

cakephp.dataTables.js:12 Uncaught TypeError: Cannot read property 'columns' of undefined at Object.dt.initDataTables (cakephp.dataTables.js:12) at clients:441

ypnos-web commented 6 years ago

please use github syntax to format the code properly so I can read it, thanks.

jabit commented 6 years ago

PLease, check it here: http://jadev.joyerianadia.com/clients

thank you

jabit commented 6 years ago

Let me know if you need something more, if you want I can send you a file with the code.

jabit commented 6 years ago

Ok, I have news, the main issue is gone because I have settled well the database column, I had a blob row that I changget to TEXT and start to show the table.

But now when I try to order, search or something else throws this error:

DataTables warning: table id=clients-table - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

And when I try to search writing more than two characters throws that: jquery-2.2.4.min.js:4 GET http://jadev.joyerianadia.com/clients?draw=4&columns%5B0%5D%5Bdata%5D=id&columns%5B0%5D%5Bname%5D... 500 (Internal Server Error)

Sorry I know I'm being a pain in the ass but I need to have that working asap and I'm wasting the time with this error, so please I beg you help me with that.

Thank you in advance

ypnos-web commented 6 years ago

When you get a 500 you should use your browser's developer tools (e.g. F12 in Chrome) to have a look at that response. In this case it tells that the count() method on your Query object is failing:

Cannot convert value of type `string` to bool

⟩ Cake\Database\Type\BoolType->toDatabase
CORE/src/Database/TypeConverterTrait.php, line 37
⟩ Cake\Database\Statement\StatementDecorator->cast
CORE/src/Database/Statement/PDOStatement.php, line 69
⟩ Cake\Database\Statement\PDOStatement->bindValue
CORE/src/Database/ValueBinder.php, line 147
⟩ Cake\Database\ValueBinder->attachTo
CORE/src/Database/Connection.php, line 333
⟩ Cake\Database\Connection->Cake\Database\{closure}
CORE/src/Core/Retry/CommandRetry.php, line 68
⟩ Cake\Core\Retry\CommandRetry->run
CORE/src/Database/Connection.php, line 337
⟩ Cake\Database\Connection->run
CORE/src/Database/Query.php, line 219
⟩ Cake\Database\Query->execute
CORE/src/ORM/Query.php, line 957
⟩ Cake\ORM\Query->_performCount
CORE/src/ORM/Query.php, line 907
⟩ Cake\ORM\Query->count
ROOT/vendor/ypnos-web/cakephp-datatables/src/Controller/Component/DataTablesComponent.php, line 257

Also, when you look at your response to sorting, again you can see it in browser, it is actually giving an HTML response to a JSON request. Are you using Cakephp RequestHandlerComponent to automatically deliver JSON?