After create: can "row" be told to expect a JSON object?

After create: can "row" be told to expect a JSON object?

atcclearsatcclears Posts: 24Questions: 7Answers: 0
edited April 2015 in Editor

https://editor.datatables.net/manual/server

I'm looking at this and it makes sense on how I can send back some additional fields after sending the new record to the server, and getting fields back such as the ID of the database record, the name of the record creator, the timestamp, and more.

The Ajax call will now return the data in a JSON structure with the name of "row", but I just realized that I'm sending it as a true object and DataTables seems to be expecting an array.

For clarity, an example of what got sent back after a successful Ajax call to the server for a create event. The structure is a valid JSON object (but not an array).

{
    "row": [
        {
            "technology_id": "3",
            "change_data_ind": "Y",
            "language_cd": "en",
            "updated_local_ts": "Apr 29, 2015 5:42pm",
            "technology_note_id": "10",
            "session_id": "75bbd3fa3068f81e8667b5b0cd24c6df",
            "created_user": "Peter Smith",
            "DT_RowId": "row_10",
            "updated_gmt_ts": "2015-04-30 00:42",
            "note_txt": "69"
        }
    ]
}

Is there a parameter here to tell DataTables to expect a true JSON object?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    DataTables can accept objects absolutely no problem. Use the columns.data option to tell it what property name to use from the data source object for each column. Almost all of the Editor examples do this.

    I guess the question then becomes, why is DataTables currently expecting arrays rather than objects like most of the examples - are you Ajax loading the table, or is it being read from the DOM?

    Regards,
    Allan

  • atcclearsatcclears Posts: 24Questions: 7Answers: 0

    In this prototype, it is already loading from the database using Ajax, and I can use Ajax on the create to send the new record to the database. Here is the columns definition:

    columns: [
      { "data": "updated_gmt_ts" },
      { "data": "updated_local_ts" },
      { "data": "created_user" },
      { "data": "note_txt" },
      { "data": "technology_id" },
      { "data": "technology_note_id" },
      { "data": "session_id" },
      { "data": "language_cd" }
    ],
    

    Should I adding more to this block to represent "row"?

    I know I'm close on this...

  • atcclearsatcclears Posts: 24Questions: 7Answers: 0
    edited April 2015

    An example when adding a new row.

    The Ajax call on create sends this JSON object to the database server, and a new record is successfully created in the database. I just copied it out of the Inspector in Chrome so please excuse the formatting.

    action:create
    data[updated_gmt_ts]:
    data[updated_local_ts]:
    data[created_user]:
    data[note_txt]:...and the shed
    data[technology_id]:1
    data[technology_note_id]:
    data[session_id]:4a6df6973327636affc3eb39a688a318
    data[language_cd]:en
    

    On that same Ajax call I currently have it responding back with a JSON object, which is just fully populated with data that exists only on the server.

    {
        "data": [
            {
                "created_user": "Peter Smith",
                "session_id": "4a6df6973327636affc3eb39a688a318",
                "language_cd": "en",
                "technology_note_id": "3",
                "change_data_ind": "Y",
                "DT_RowId": "row_3",
                "technology_id": "1",
                "note_txt": "...and the shed",
                "updated_gmt_ts": "2015-04-30 13:42",
                "updated_local_ts": "Apr 30, 2015 6:42am"
            }
        ]
    }
    

    Again, I'm wanting to have DataTables immediately show this data in the table. Either I'm close based on the documentation, or I need to have Ajax do a reload (not preferred).

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    Ah - I think I see where the problem is. In your row return (which is correct - you want to do that), it is returning an array with an object it in. You actually want it to just give an object:

    {
        "row": {
            "technology_id": "3",
            "change_data_ind": "Y",
            "language_cd": "en",
            "updated_local_ts": "Apr 29, 2015 5:42pm",
            "technology_note_id": "10",
            "session_id": "75bbd3fa3068f81e8667b5b0cd24c6df",
            "created_user": "Peter Smith",
            "DT_RowId": "row_10",
            "updated_gmt_ts": "2015-04-30 00:42",
            "note_txt": "69"
        }
    }
    

    At the bottom of every example on the Editor site is an 'Ajax data' tab which shows the format that has been used for the transaction in that example. Additionally the client / server communication protocol is documented in the manual.

    Allan

  • atcclearsatcclears Posts: 24Questions: 7Answers: 0

    I confirmed that Allan's solution was correct. I changed the program to return a JSON object (instead of a JSON array of objects) after creating the data in the database, and the Editor successfully took the response and immediately showed the fields in the table.

This discussion has been closed.