Problem with append after ajax
Problem with append after ajax
i have a problem with an ejax result, i have my datatable with their rows,
NUM. | COGNOME E NOME | Schede ESERCIZI Errori | Schede ESAME Errori |
All rows are loading correct but i have a problem with row.cells[3] because i have 2 php page how response of ajax to compile the 3 fields of column, "schede" and "esami" are compiled with this code:
"rowCallback": function(row, data, index) {
$.post("ajax/mediaweb/schedeesamiallievi.php", { "id_allievo": data.Codice, "id_sedeClienteNeca": data.id_sedeClienteNeca }, function(data) {
data = JSON.parse(data);
row.cells[3].innerHTML = data.html;
setGiudizio(row, data, 0);
});
the third fields ("Errori") is a response ajax of this:
$.post("ajax/mediaweb/MediaEsame.php", { "id_allievo": data.Codice, "id_sedeClienteNeca": data.id_sedeClienteNeca }, function(data) {
data = JSON.parse(data);
});
data= MediaErrori: 0 , <div style="position: absolute;top: 0;right: 0px;width: 50px;text-align: left;">' . $mediaEsameFormattato . </div>
how can i append this last data to first data with processing html of div?
Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Replies
Hi @Maxilboss93 ,
First, I would suggest using
cell().data()
rather thanrow.cells[3].innerHTML
, since DataTables wouldn't know about the change and wouldn't sort/search correctly.If you're just appending, can't use get the existing value first - either with
cell().data()
or yourrow.cells[3].innerHTML
.If that doesn't help, we're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin
$(document).ready(function() {
Hort to use cell().data()?
There's example on its page that are worth looking at. Also, see here.
Sorry but i want to take the data one for the row in all the position of row.cells[3] but if i use the append the table append all data in all rows, how i take one for rows data in addition to data that there was first?
For example i want, (your example) in the same field ("london, 35"), in my case i want in the same field 2 result of 2 different ajax.
one results of this php page
<?php
require('../../includes/config.php');
require('../../includes/necaDB.php');
$id_allievo = $_POST['id_allievo'];
$id_sedeClienteNeca = $_POST['id_sedeClienteNeca'];
$sql = 'SELECT
sync.id_allievo AS id_allievo,
COUNT(sync.id_allievo) AS Schede,
TRUNCATE(AVG(sync.errori), 4) AS Media,
LI.Domande AS Domande,
IFNULL(LI.errori, 0) Errori
FROM sync
LEFT JOIN allievo A ON A.id_allievo = sync.id_allievo
LEFT JOIN clientescuola CS ON CS.id_clienteScuola = A.id_clienteScuola
LEFT join listato LI ON LI.tipo = A.listatoQuiz
WHERE ((sync._Stato <> "D") AND (sync._Stato <> "X") AND (sync.tipo = "TipoEsame"))
AND CS.id_sedeClienteNeca = ' . $id_sedeClienteNeca . '
AND sync.id_allievo = ' . $id_allievo . '
GROUP BY sync.id_allievo';
// $file = fopen('schedeEsami.log', 'w');
// fwrite($file, 'Start ' . date("c") . PHP_EOL);
$rows = NecaDB::arraySQL(null, $sql);
// fwrite($file, 'After Query ' . date("c") . PHP_EOL);
$result = array(
'html' => '-',
'Schede' => '0',
'Media' => '0',
'Errori' => '0'
);
if (isset($rows[0])) {
$rows[0]['Media'] = floatval($rows[0]['Media']);
$rows[0]['Schede'] = intval($rows[0]['Schede']);
$rows[0]['Errori'] = intval($rows[0]['Errori']);
if($rows[0]['Domande']==0){
$percentualeEsameDomande=0;
}
else{
$percentualeEsameDomande = ($rows[0]['Media']/ $rows[0]['Domande']) * 100;
$percentualeEsameDomande = round( $percentualeEsameDomande, 1, PHP_ROUND_HALF_DOWN);
/* if ($percentualeSchedaEsame> 100%){
}
//fwrite($file, 'After Elab ' . date("c") . PHP_EOL);
//fclose($file);
echo json_encode($result, true);
and second result in this php other page:
<?php
require('../../includes/config.php');
require('../../includes/necaDB.php');
$id_allievo = $_POST['id_allievo'];
$id_sedeClienteNeca = $_POST['id_sedeClienteNeca'];
$sql= "SELECT AVG(Media) as MediaErrori FROM (SELECT errori as Media FROM sync WHERE id_allievo= ". $id_allievo . " AND tipo = 'TipoEsame' ORDER BY
sync
.dataFine
DESC Limit 15)as MediaErrori";$rows = NecaDB::arraySQL(null, $sql);
if (isset($rows[0])) {
$rows[0]['MediaErrori'] = floatval($rows[0]['MediaErrori']);
$nM = $rows[0]['MediaErrori'];
$result['MediaErrori'] = $rows[0]["MediaErrori"];
}
if ($nM == 0) {
$nM = "";
$mediaEsameFormattato = "";
} else {
$mediaEsameFormattato = number_format($nM, 1);
$mediaEsameFormattato = round( $mediaEsameFormattato, 1, PHP_ROUND_HALF_DOWN);
}
echo json_encode($result, true);
The result of this code: row.cells[3].append(data.html);
its this:
6 (there is a graphic)
"'<div 'style="position: relative;top: 0;right: 0px;width: 50px;text-align: left;">24.2</div>'"
but i want : 6 ( graphic) 24
i changed the element , i Load the div empty in the first php page and after i use
var mediaesame = document.getElementsByClassName("MediaEsame");
$(mediaesame).append(data.html);
but if i use this method the table load all results in all rows.
That's a lot of code, but the easiest thing here would be to create that test page that I requested before, or link to your page. That way we can see what's going on and understand it better.
Cheers,
Colin
nothing, i have the solutions, i used:
var celle = row.cells[3].innerHTML;
celle = celle.replace('</div>', '</div>
"')
and this is the solution.