plug lokijs with datatables

plug lokijs with datatables

tycoonstycoons Posts: 9Questions: 1Answers: 0

Hi Guys,
I am trying to integrate the base in json format of lokijs with datatables but it does not work you have an idea on how to do it?
json data are not really identical to datatables

thanks for your help

Answers

  • kthorngrenkthorngren Posts: 20,294Questions: 26Answers: 4,768

    Can you post an example of the json format returned?

    Kevin

  • tycoonstycoons Posts: 9Questions: 1Answers: 0
    edited November 2018

    '''{"filename":"Objet.json","collections":[{"name":"candidats","data":[{"candidature":"site internet","nom":"hbwcf","poste":"Collaborateur junior","service":"Service Révision PME & Grandes Entreprise","Entree_le":"2018-11-16","diplome":"xcv","entretien_le":"2018-11-10","commentaire":"xv","etat":"ne correspond pas","retour":"Proposition de poste","meta":{"revision":0,"created":1542277701035,"version":0},"$loki":1},"idIndex":[1,2,3,4,5,6,7,8],"binaryIndices":{},"constraints":null,"uniqueNames":["id"],"transforms":{},"objType":"candidats","dirty":false,"cachedIndex":null,"cachedBinaryIndex":null,"cachedData":null,"adaptiveBinaryIndices":true,"transactional":false,"cloneObjects":false,"cloneMethod":"parse-stringify","asyncListeners":false,"disableMeta":false,"disableChangesApi":true,"disableDeltaChangesApi":true,"autoupdate":false,"serializableIndices":true,"ttl":null,"maxId":8,"DynamicViews":[],"events":{"insert":[],"update":[],"pre-insert":[],"pre-update":[],"close":[],"flushbuffer":[],"error":[],"delete":[null],"warning":[null]},"changes":[]}],"databaseVersion":1.5,"engineVersion":1.5,"autosave":true,"autosaveInterval":5000,"autosaveHandle":null,"throttledSaves":true,"options":{"autoload":true,"autosave":true,"autosaveInterval":5000,"serializationMethod":"normal","destructureDelimiter":"$<\n","recursiveWait":true,"recursiveWaitLimit":false,"recursiveWaitLimitDuration":2000,"started":1542280250791},"persistenceMethod":"fs","persistenceAdapter":null,"verbose":false,"events":{"init":[null],"loaded":[],"flushChanges":[],"close":[],"changes":[],"warning":[]},"ENV":"NODEJS"}'''

    here is the json database file that I get from lokijs I parse it before integrating it into datatables into an array objectc (object0, object1, etc ..) lokijs uses an id of its own $ lokijs1 $ lokijs2 my console after the parsage of my json m well displays my table but datatables do not want

  • kthorngrenkthorngren Posts: 20,294Questions: 26Answers: 4,768
    edited November 2018

    here is the json database file that I get from lokijs I parse it before integrating it into datatables into an array objectc (object0, object1, etc ..)

    Ok, Please post an example of the parsed data and your Datatables init code. Also post any alert messages or console message you receive.

    Kevin

  • tycoonstycoons Posts: 9Questions: 1Answers: 0

    here is an example of my code with the log the invalid error message json so i understand that he can not read it
    https://gyazo.com/25c31ce4f41d41868ae26994ea0762b3

  • kthorngrenkthorngren Posts: 20,294Questions: 26Answers: 4,768

    The invalid json message is probably due to the incorrect use of the ajax option. It does expect a URL to fetch the data.

    My suggestion would be to remove the Datatables ajax config. Then in your readTextFile function use rows.add() and draw() to populate the table. Where you have console.log(test); try the following table_companies.rows.add( test ).draw();.

    Not sure if your data structure matches the columns.data config but it should work if they match. When you are initializing Datatables you have lower case d for dataTable(). You will want to change it to upper case like this: DataTable({ ... }); to get the API. See this FAQ:
    https://datatables.net/faqs/index#api

    Kevin

  • tycoonstycoons Posts: 9Questions: 1Answers: 0
    edited November 2018

    great
    we made a lot of progress here so I go with another error tn / 4 error table id unknows parametre..normal since there is no id but the lokijs parameter for id is $ lokijs: 1, $ lokijs2, etc ...

  • kthorngrenkthorngren Posts: 20,294Questions: 26Answers: 4,768

    You didn't post the actual data structure for each row so I'm not sure. Did you follow the troubleshooting steps in the link provided in the error?
    https://datatables.net/manual/tech-notes/4

    Basically your data structure doesn't match what you defined in the columns option.

    Kevin

  • allanallan Posts: 61,695Questions: 1Answers: 10,102 Site admin

    Can you show the full error message please?

    Allan

  • kthorngrenkthorngren Posts: 20,294Questions: 26Answers: 4,768
    edited November 2018

    It looks like your first two elements (rows) have different objects than the remaining rows. The error states that its looking for the property nom which doesn't appear to exists in the first row. Maybe you can eliminate the first two rows from your data?

    Kevin

  • tycoonstycoons Posts: 9Questions: 1Answers: 0

    i create new json file here
    https://petit-fichier.fr/2018/11/16/objet/
    look this log name is ok
    https://gyazo.com/6ad19a911e1ed63f83dc6ca7e4e6e8c6
    I have to miss something but I do not know what
    thank for your big help

  • kthorngrenkthorngren Posts: 20,294Questions: 26Answers: 4,768
    edited November 2018

    I'm unable to open the first link. But comparing your array of objects to the original screenshot of the Datatables config there are some differences.

    In Datatables you defined a functions column which isn't in your data. Also I the property names might be case sensitive so you need to fix Entree_le and commentaire. Both data structures need to match.

    As a side note it would be easier if you copied the text into the thread instead of using screenshots.

    Kevin

  • tycoonstycoons Posts: 9Questions: 1Answers: 0
    edited November 2018

    I pasted some of the code here

    https://codepen.io/tycoons/pen/MzvaLb
    i try to change

  • kthorngrenkthorngren Posts: 20,294Questions: 26Answers: 4,768

    The code doesn't seem to run. But I do see one issue. You have this:

    $('#table_companies').dataTable({
         "type": "GET",
        "data": "test",
        "dataType": "json",
    

    When I suggested removing the ajax I meant for you to remove all the options within which were 'type', 'data', 'dataType'.

    I made the other two changes I suggested:

     console.log(test);
      
      // add the rows
      table_companies.rows().add( test ).draw();
    
    });
      // On page load: datatable
     var table_companies = $('#table_companies').DataTable({
         //"type": "GET",
        //"data": "test",
        //"dataType": "json",
        "columns": [
    

    https://codepen.io/anon/pen/RqZaVK

    Try that and let us know.

    Kevin

  • tycoonstycoons Posts: 9Questions: 1Answers: 0

    Hi Kevin,
    I applied the patches and modify the fields to avoid being sensitive to the box but this shot so I have an error message different
    No data available in table and this error
    Uncaught TypeError: table_companies.rows(...).add is not a function

  • kthorngrenkthorngren Posts: 20,294Questions: 26Answers: 4,768

    Sorry, incorrect syntax on my part. You want to use rows.add() so it looks like this:
    table_companies.rows.add( test ).draw();

    Note the removed () after rows.

    Kevin

  • tycoonstycoons Posts: 9Questions: 1Answers: 0

    nice very good thank for your help i modifie lokijs for disable meta array
    i work fine thank you very weel my friend

This discussion has been closed.