function ajax and async on Datatable Editor
function ajax and async on Datatable Editor
Hi Allan,
I think that a have a problem related to async using function ajax call on Datatable editor 1.4. I explain:
I’ve on index.html I have two pages jqm:
In page-1 I´ve a form that get (nombreUsuario) and password and assign into respective vars.
In page-2 , I want to use the previous (nombreUsuario) for to get table that match with this nombreUsuario via AJAX on Datatable Editor 1.4.
My problem is that in page-1 the AJAX call is executed before that I enter (nombreUsuario) , therefore when pass to page-2 the table is empty , due the AJAX call was executed with variable empty , without (nombreUsuario) entry.
I´m working in base on https://datatables.net/reference/option/ajax .
How can I do to the AJAX call wait or postpone ?. How to do for AJAX call wait until user enter your credentials?
My code:
index.html
<div data-role="page" id="page0">
<form id="formulario" >
<label> Usuario </label>
<input type="text" id="nombredeusuario" name="nombredeusuario">
<label> Password </label>
<input type="password" id="clave" name="clave" >
<input type="submit" value="Login" id="botonLogin">
</form>
...
<div data-role="page" id="page1">
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Establecimiento</th>
<th>Telefono</th>
<th>E-mail</th>
<th>Nº de Plazas</th>
<th>Precio medio</th>
<th>E-mail (responsable)</th>
<th>Prueba (1) / Contrato (2)</th>
</tr>
</thead>
....
JS:
(form)
$('#formulario').submit(function() {
// recolecta los valores que inserta el usuario
var datosUsuario = $("#nombredeusuario").val()
var datosPassword = $("#clave").val()
...
JS (datatable editor 1.4)
$('#example').DataTable( {
//check. start
initComplete: function(settings, json) {
alert( 'DataTables has finished its initialisation.' );
},
//check.end
dom: "Tfrtip",
ajax: {
async: false, // don't work
url:"http://tripntry.com/_country/spain/b.cartasconmenu.front.back.demo/alacarte/php/clientes.php",
type: "POST",
data: function ( d ) {
d.site = $("#nombredeusuario").val(); // work but must to wait !
//d.site = datosUsuario; // ( same above)
//d.site = "34280001"; // only test. works - (of course)
},
....
PHP (server side)
include( "../../php/DataTables.php" );
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Validate;
$site=$_POST['site'];
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'clientes' )
->fields(
Field::inst( 'tipo' )->validator( 'Validate::numeric' ), //this field another use
Field::inst( 'site' )->validator( 'Validate::notEmpty' )
)
->where( 'site', $site)
->process( $_POST )
->json();
Answers
Sounds like you want to delay the initialisation of the second "page" once the first table has fully loaded. Is that correct?
if so, use
initComplete
to execute a function when the first table has fully loaded.Regards,
Allan
Yes, is a delay question , but the problem are that I need delay the initialization of the table on page-2 and there are only a table. The page-1 is a simple form. The initialization of table on page-2 must to wait that the form be filled and send it, and after should to start the initialization of datatable. Any idea?. Thank you very much!
What is filling in your form? You would need to modify your code to not create the DataTable until the form is populated, but I can't say what that change would be without knowing your code.
Allan
Hi Allan, the code of page-1 (page0) :
JS:
And code of page-2 (page1):
and your JS:
Thank you for you help!!
Why not initialise the DataTable just after that?
Yes!, I confirm it's a way resolve the question. There are a collateral issue: when navigate to next pages (there are 4 jqm pages) when came back at this page, the table is empty. I don't know if it's because the initialization happen again or related to DOM (I don't know ) , but the content of variable (datosUsuario) "disappear" when you navigate across the pages and after came back. My solution to that: to use LocalStorage (jquery) to keep the variable.
Allan, Thank you very much !!