Problem to read my JSON text file

Problem to read my JSON text file

JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

Hello everyone,

To begin, i'm french, so excuse me for my english's mistakes...

I'm using DataTables to display the result of a SQL request.

Here, some screen to help u to understand :

First : https://gyazo.com/09c14f2e96db5cb649e183fa27f2319a You can search by yourself some contacts. It's link with a table in my database.
Second : https://gyazo.com/98bb4d13fdc514ed62080107bf016d6c When you choose your contact, you've a second menu where you have the important part here, "Historique".

Third : https://gyazo.com/b4a3ceb4c62941c1c15096a47daddfa9 When you click on "Historique", it display some informations about the contact.

The result of my request about the "Historique" is wrote in JSON on a text file.

Everything's good here... BUT when i want to change contact, and i go to "Historique", it is not updated . The historic displayed is always the same whereas my txt file, is updated !!!! So, each time, my JSON is good, with the good informations, but not updated on my historic menu.

Here, some code to help you to understand what is my mistake :

/* My dataTable config */
```$('#historique').click(function() {
var table = $('#example').DataTable( {
"language": {
"lengthMenu": "Voir _MENU_ enregistrements par page",
"sInfo": "Réultat des _START_ sur _END_ enregistrement sur un total de _TOTAL_",
"sSearch": "Rechercher"
},
"ajax": "actionsFile.txt",
"ajax": "actionsFile.txt",
"paging": false,
"retrieve": true,
"columns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data": "NOM" },
{ "data": "TYPE" },
{ "data": "DATEFIN" },
{ "data": "DATE_PREVU" }
],
"order": [[1, 'asc']]
} );

});```

How can i refresh my historic menu? Have I some parameters to add?

Thanks for u help, and thanks to take the time to read me (even if it's complicated ahah)

Ju.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,791Questions: 1Answers: 10,512 Site admin

    Hi Jum

    It sounds like you need to use the ajax.reload() API method to reload the JSON. However, it is worth pointing out that you are reading from a static file. Has that been updated with the new data?

    Thanks,
    Allan

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

    Hi Allan,

    Thanks for ur interest on my question,

    I tried to used the ajax.reload() API method to reload my JSON but it doesn't work. Here is my problem, my text file is updated with the new data, but not refresh on my historic menu. I just need to refresh the DataTables component, to load again the same file.

    Can u help me? Thanks a lot

  • allanallan Posts: 63,791Questions: 1Answers: 10,512 Site admin

    I'd need a link to the page so I can debug what is happening please.

    Allan

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

    It's not online, not this part because it's not operationnal... I'm sorry.

    I'm gonna try to explain again as i can.

    On my first picture on my first post, you've a first menu with "Rechercher, Fiche, Créer son contact, Créer une note". Only the "Rechercher" element is important here. When i fill the input with a company name, it gave me some choices with an autocompletion, right.

    When i choose the company i want, you have a second menu with my second picture with "Détail, Interlocuteurs, Créer une action pour, Historique". Each part of this menu have a different function, but you have to pay attention on the "Historique" part.

    When i click on it, it display a DataTable with some data related to the company. I've my SQL request to fill my txt file and i give that file to the DataTable "Ajax" attribute.

    It's always good for the first company. But when i want to change (when i click on Rechercher") a second time, on my second menu, the historic is the historic of the first company whereas in my text file, the datas are updated with a valide JSON.

    How can i refrest the DataTable component? That is the problem...

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

    I'm really disappointed about it.. Can u help me sir?

  • allanallan Posts: 63,791Questions: 1Answers: 10,512 Site admin

    How can i refrest the DataTable component? That is the problem...

    You would use ajax.reload() as I mentioned before. If that isn't working for you, I would need a link to a page showing the issue so it can be debugged.

    I don't see any use of ajax.reload() in your code above, so I'm not even sure how you are using it.

    Allan

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

    Ok, i'm gonna try this and if it's not working, i'll give you a link if i can.

    I've one more question. Is it possible to give a variable instead of a text file for the dataTable?

    It's really nice to have ur comments. Thanks a lot,

    Julien

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

    I've find the response to my last question about the variables.

    I'll try the ajax.reload() API method and i call you back.

    Thanks a lot

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

    Hey,

    So, i tried, and it doesn't work, i don't know why, but i'm working on it.

    However, i'm a rookie and i've some difficult with the simple example on the server side (https://datatables.net/examples/data_sources/server_side.html). I would like to call my json variable from my php, to my javascript. But i don't need the connexion to the database.

    Is there a simple way to just send to my javascript, my php variable with the JSON content?

    Thanks,

  • allanallan Posts: 63,791Questions: 1Answers: 10,512 Site admin

    As I requested before, please link to the page showing the issue. Without that I can't debug the problem you are seeing. Use JSFiddle or http://live.datatables.net if you can't host your own sample page.

    As I noted, ajax.reload() is the way to reload the JSON. There is an example of how to use it in the documentation.

    Allan

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

    I don't know how can i host it. In http://live.datatables.net it's complicated because i've so much php includes everywhere...

    I'm really sorry to disturb you with all theses problems, but i want to keep the DataTables plugin because i'ts very useful ! (Congrats for that Allan !)

    Thanks,

    Julien

  • allanallan Posts: 63,791Questions: 1Answers: 10,512 Site admin

    Here is a very trivial example showing how to use ajax.reload(): http://live.datatables.net/ditohoti/1/edit .

    I'm afraid I am unable to offer any further assistance unless you provide the information that I would need to be able to help you! I don't even know what the problem is with your code because I don't know what code you are using!

    Allan

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

    Allan,

    I tried to give you more informations with http://live.datatables.net/ditohoti/3/edit

    Do you know how I can send a PHP variable with my JSON to my JS file? To replace the "actionsFile.txt"?

    The example given in the website is not exactly what I want and I don't know how i can modify it...

    Thanks, again and again for ur precious help.

    Julien

  • allanallan Posts: 63,791Questions: 1Answers: 10,512 Site admin

    Your example doesn't run. It doesn't actually even load DataTables.

    I'm not sure why you would put ajax.reload() immediately after you initialise the table. That would just immediately reload the data which seems completely redundant. You would call that function when you need to load the table's data again.

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

    I know that my example doesn't run, i've too much file with too much includes PHP everywhere, i can't put all my files on these website...

    I just write the important part of my problem... I'm really sorry to waste your time.

    You're example with the "Reload data" button works ! I have to refresh it dynamically so i've to find the correct place to do it. I think I can fix it now, thanks !

    I've juste one more problem, as i said before. I would like to send a PHP variable with my JSON to my JS file to replace the "actionsFile.txt". How can i do that?

    Sorry again for my poor english, I do my best...

    Thanks A LOT

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

    SOOOOOOO, my refresh is now operationnal ! Thanks Allan !!!

    One problem, and i'll give you 18918916548949$, i promise !

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

    Ok, i was happy too early...

    My refresh is good, maybe too much.
    When i click on next or Previous, it redirect me automatically on my first page results'

    Any idea?

  • allanallan Posts: 63,791Questions: 1Answers: 10,512 Site admin

    Without a link to a page showing the issue I can't say what the problem is I'm afraid.

    Allan

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

    Maybe a link to have a better explanation about how to pass my php variable to my js? :/

    Have a good weekend Allan,

    Julien

  • allanallan Posts: 63,791Questions: 1Answers: 10,512 Site admin

    Have a look at initComplete. It gives you the JSON object that was loaded from the server, so you can access it that way.

    Happy to provide an example under the priority support options.

    Allan

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

    Hey Allan,

    I hope you had a great weekend.

    All my problems are resolved.. To discover a new one.

    I've my Ajax request to get my datas like that :

    $('a#historique').click(function() {
    var table = $('#example').DataTable( {
    "language": {
    "lengthMenu": "Voir MENU enregistrements par page",
    "sInfo": "Résultat des START sur END enregistrement sur un total de TOTAL",
    "sSearch": "Rechercher"
    },
    "ajax": "test.php",
    "paging": true,
    "processing": true,
    "serverSide": true,
    "retrieve": true,
    "columns": [
    {
    "className": 'details-control',
    "orderable": false,
    "data": null,
    "defaultContent": ''
    },
    { "data": "NOM" },
    { "data": "TYPE" },
    { "data": "DESTINATAIRE" },
    { "data": "DATE_PREVU" }
    ],
    "order": [[3, 'desc']]
    } );
    $('a#historique').on( 'click', function () {
    alert('reload');
    table.ajax.reload();
    } );

    But now, on the dataTable content, the length menu, the pagging, the sSearch text area are disabled. Why?

    Thanks,

    Julien

  • allanallan Posts: 63,791Questions: 1Answers: 10,512 Site admin
    Answer ✓

    Does your test.php fully implement server-side processing?

    Allan

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0
    edited April 2016

    ```<?php
    // include
    require '_inc_config.php';

    // Récupère le contact id stocké dans la variable de session (cf autocomplete2.php)
    $contact_id = $_SESSION['contact_id'];
    
    $data3 = $var->getListeDernieresActions($contact_id);
    
    // Formate la variable $toJson en JSON valide pour l'envoyer au DataTables
    $toJson = '{"data":';
    $toJson .= $data3;
    $toJson .= '}';
    // Fin du formatage
    
    // Affiche le contenu dans une page html, ensuite récupérer par l'appel Ajax
    echo $toJson;
    
    <?php >``` ?>

    Here is my test.php

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0

    Ok, I've find my problem.

    Thanks again for these days of help. You're great !

    Julien,

This discussion has been closed.