Combobox dependents
Combobox dependents
Hello,
I am trying a functionality that I do not know if it is possible in Datatable Editor.
I have two fields that are edited with combobox: titulo_id and capitulo_id of the tb_tutorial table.
The editor is a template: '#customForm'
<div id="customForm">
<fieldset>
<editor-field name="tb_tutorial.titulo_id"></editor-field>
<editor-field name="tb_tutorial.capitulo_id"></editor-field>
</fieldset>
</div>
The data of the list that appears in the combobox:
Field::inst( 'tb_tutorial.titulo_id' )
->options( Options::inst()
->table( 'tb_palabra' )
->value( 'id' )
->label( 'palabra' )
),
Field::inst( 'tb_palabra.palabra' ),
Field::inst( 'tb_tutorial.capitulo_id' )
->options( Options::inst()
->table( 'tb_capitulo' )
->value( 'id' )
->label( 'capitulo' )
),
Field::inst( 'tb_capitulo.capitulo' ),
->leftJoin( 'tb_palabra', 'tb_palabra.id', '=', 'tb_tutorial.titulo_id' )
->leftJoin( 'tb_capitulo', 'tb_capitulo.id', '=', 'tb_tutorial.capitulo_id' )
THE QUESTION: Is it possible to make the combobox list for 'tb_tutorial.capitulo_id' filter based on the id of the previous combobox: 'tb_tutorial.titulo_id'.
Thank you
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Replies
Yes this is possible. We call it "cascading select". There is a detailed blog post about how to do it here.
Allan
Thanks Allan, it seems that is what I am looking for.
PS: thanks to Colin for editing the text and giving it a better format.
Do not adapt the example "Cascade lists in the editor" to my project with Datatable Php. Looked at the editor examples folder: examples / api but it is not included. Is it possible to have the complete example to study its operation?
Hi @jalape ,
Yep, it's only on the forum as a blog post. You could look at the page source for that post, the client side code is all there for the example. The server-side code is explained in the post itself,
Cheers,
Colin
I have the two necessary elements, but I can not get it to work.
datatable_tutirial.js
datos_tutorial2.php
Before trying to get the cascade to work with Select2, try it with just
select
(just to reduce the surface area for where things could be going wrong).With that done, what happens when you run your code? Do you get any errors? It would be useful to see the page you are working on if you can link to it please.
Allan
It does not generate any errors, but the editor remains in a constant activation loop (see the image).
I am still working in local, but I have reduced the files of the app and the tables of the database as an example. You have to add the folders css and js in the folder:
cascading\plugin\editor (version Editor-PHP-1.9.0) to make it fully functional.
javierlasen.es/archivos/cascading.zip
Are you able to give me a link to your page please? That should only happen if:
dependent()
and don't return a value (you aren't using it with a callback, so its not this).Allan
http://www.javierlasen.es/recursos/login/admin_tutorial.php
datatable_tutorial.js
datos_tutorial.php
datos_tutorial2.php
Hi @jalape ,
I'm seeing an error on that page. When you edit an item, the console shows this error:
This is the reason why the activation loop is constant, it's waiting for
dependent()
to get that successful response.To debug, it would be worth checking the server script to see why it's failing.
Cheers,
Colin
Add this at the top of your PHP file (inside the
<?php
of course) to have it dump any errors:Allan
Thanks Colin and Allan for your time:
Colin: The error in the console is caused by a badly deactivated line, it is solved and it no longer gives an error, but it is not related to the problem with the use of the dependent()
Allan: I have added the error line:
At the beginning of the PHP file, both in "datos_tutorial.php" and in "datos_tutorial2.php", but nothing happens.
You can see it in the link:
http://www.javierlasen.es/recursos/login/admin_tutorial.php
Hi @jalape ,
The 500 error is still being returned by the server - if you look at the network tab, you'll see the request is failing. I do think this is related to the issue -
dependent()
is calling that script (php/p_datos/datos_tutorial2.php
) which is failing.Cheers,
Colin
Thanks Colin,
Effectively the file data_tutorial2.php gives an error of type 500. I think it is related to the server, since localhost does not give that error. In any case it is not the cause of the malfunction of dependent (). In local does not work either.
It would be possible to have the files on the page: “Cascading lists in Editor”. https://datatables.net/blog/2017-09-01. Comparing the two structures, it would be easier to check where the error is.
Sure - this is the code used:
Odd that adding the two lines I suggested didn't show anything on the output. What version of PHP is your server running? Also, have you checked the server's error logs to see if there is anything reported there?
Allan
I understand that datatables/mysql.php is the connection to the Database. In my case it is called conexion.php and it has this structure:
conexion.php
$sql_details I guess it's the variable of the connection, in my case $mysqli.
I also have doubts with data_ajax (line 5) and with the variable $ db (line 8) that is not defined.
It still does not work.
How is the file that contains the table and configure the editor box of the example?
I can not make it work. Would it be possible to have the example files cascaded in the Editor?
javierlasen@hotmail.com
Thank you.
datatables_ajax
is the table name to read the data from.That's the database resource. In your case that's the return from your
conexion
function.Allan
Thank you Allan,
With: $sub['db'] = 'tb_tutorial'; it still does not work.
Any change in "datos_tutorial2.php" does not change anything, it can be a problem with the call:
I have changed its position in the structure of the file, but it does not work either.
When it "does not work" do you get any errors? If you could link to the page, that would help in my debugging of the problem.
Allan
Thanks Allan,
The address is:
http://www.javierlasen.es/recursos/login/admin_tutorial.php
When opening the editor box, a type 500 error occurs in the data_tutorial2.php file.
You are getting
500 Internal Server Error
. This error is coming from your server. You will need to look at your server logs to see why this error is occurring.Kevin
Thank you kthorngren,
Using Xampp as a local server, the error is the same.
Kevin already told you