How to pass a variable that hold data into datatables js?

How to pass a variable that hold data into datatables js?

jiamaozhengjiamaozheng Posts: 10Questions: 1Answers: 0

From python code: data = json.dumps(json.loads(pandas.DataFrame(data).to_json(orient='records')))

How to pass data variable into the datatables javascript:
"ajax": { "url": "??????????", "dataSrc": '' }

Thanks,

Replies

  • jiamaozhengjiamaozheng Posts: 10Questions: 1Answers: 0
    edited October 2017

    I tried the following syntax, but it returns a empty table.
    "ajax": { "url":JSON.parse(data), "dataSrc": '' },

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    I've used Pandas but not enough to know it in detail. However I use Python with Datatables. Looks like you are converting the Pandas data to json with .to_json then decoding with json.loads then encoding again with json.dumps.

    I'm not sure what the data structure is with pandas.DataFrame(data).to_json(orient='records'). But you could potentially just return that to the Datatable.

    Are you using a Python based web server?

    For ajax to work you will need a web server to return the data. Your ajax config would look like this:
    "ajax": { "url":"/get_pandas_data", "dataSrc": '' }

    /get_pandas_data would be the URL your Python web server hosts to process the Pandas data and returns it with something like this:
    return pandas.DataFrame(data).to_json(orient='records')

    The "dataSrc": '' option tells Datatables that the data is not in a data object in the returned JSON.

    Hope this gets you started.

    Kevin

  • jiamaozhengjiamaozheng Posts: 10Questions: 1Answers: 0

    Kevin,

    The solution you provided works, a common way to get json data via ajax, but that is not what I want.

    I would like to return html template and pass data at the same time. So I need to pass that data variable to Datable js "ajax": { "url": "??????????", "dataSrc": '' }.

    Yes, I used bottle python web framework.

    I would be very appreciated if you could provide more hints. Thanks,

  • kthorngrenkthorngren Posts: 21,147Questions: 26Answers: 4,918

    The ajax.data allows you to pass data to the server. The doc page has a few examples.

    Kevin

  • jiamaozhengjiamaozheng Posts: 10Questions: 1Answers: 0

    Hi Kevin, I will give it a try. Thank you very much for your help. Jiamao

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

    Maybe you can use something like this, at first load the data which hold your URL and then use it in your next Ajax request:

    $(document).ready(function() {
        $.ajax(
        {
            url: `Ajax-call-to-get-your-url`,
            dataType: `JSON`,
            success: function (json)
            {
                var my-url = json.url;   // or whatever
    
                var editor = new $.fn.dataTable.Editor({
                    ajax: {type: `POST`, url: my-url},
    

    In this example, you passed an URL parameter in the DataTables Java script which you can use for further processing.

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

    I forgot to write that you can send data and templates at the same time. Just add them to the response of the first Ajax call and you have them:

            success: function (json)
            {
                var my-url = json.url;   // or whatever
                var my-data = json.data;   // or whatever
                var my-templates = json.templates;   // or whatever
    
  • jiamaozhengjiamaozheng Posts: 10Questions: 1Answers: 0

    Hi Tester2017, thanks for your help and advice. The template I mentioned is the HTML file like .tpl. I would be very appreciated IF you could provide some concrete examples. Thanks, Jiamao

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

    OK, I am sorry. I do not understand what you are obtaining. Maybe you can try to explain your question a little bit more with an abstract example of what you really want or else wait till someone pass who understand better your question.

This discussion has been closed.