Removing quotation marks from ajax data

Removing quotation marks from ajax data

lschneidermanlschneiderman Posts: 17Questions: 5Answers: 0

Everything is working perfectly with my dataTable except that some of the data appears in quote marks:

https://newsinteractive.post-gazette.com/fish-fry-pittsburgh/

How do I remove the quotation marks please?

$('#results').DataTable({
"processing": true,
searching: true,
"ajax": {
"url": "./php/get_data.php",
"type": "POST",
"dataSrc": ""
},
columns: [
{ title: "Venue", data: "venue" },
{ title: "Address", data: "address" },
{ title: "Neighborhood", data: "neighborhood" },
{ title: "Phone", data: "phone"},
{ title: "Times", data: "times" },
{ title: "Menu", data: "menu" },
{ title: "Notes", data: "notes" }
]
});

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,300Questions: 26Answers: 4,945
    Answer ✓

    One option is to use columns.render with a regex expression to remove the " from the string. Maybe something like this str.replace(/^"|"$/g, '').

    Kevin

  • lschneidermanlschneiderman Posts: 17Questions: 5Answers: 0

    @kthorngren How would I stringify the data?

  • lschneidermanlschneiderman Posts: 17Questions: 5Answers: 0

    @kthorngren I got some of it to work:

    columns: [
    { title: "Venue", data: "venue", render: function ( data, type, row, meta ) {
    return data.replace(/"/g, "");
    } },
    { title: "Address", data: "address" },
    { title: "Neighborhood", data: "neighborhood" },
    { title: "Phone", data: "phone"},
    { title: "Times", data: "times", render: function ( data, type, row, meta ) {
    return data.replace(/"/g, "");
    }

            },
            { title: "Menu", data: "menu" },
            { title: "Notes", data: "notes" }
            ]
    

    But when I try to apply the same code to the menu or notes data, I get the message "data.replace is not a function" in my console log. Any insight into why data.replace works with two types of data, but not others?

  • kthorngrenkthorngren Posts: 21,300Questions: 26Answers: 4,945

    Looks like you might have some non-string data:

    Maybe a boolean value?

    Maybe null instead of blank strings in Notes?

    You might need to use if statements to do some type check and handle the data appropriately.

    You can use columnDef for the columns.render function since it will be the same for multiple columns. Something like this:

      "columnDefs": [ {
          "targets": [ 0, 2 ],  // List all the columns to apply this function
          "render": function ( data, type, row, meta ) {
             if ( data === null ) {. // Do some type checking
               return "";
             }
             return data.replace(/"/g, "");
            } 
        } ]
    

    The above may have syntax errors as I didn't test it :smile:

    Kevin

  • lschneidermanlschneiderman Posts: 17Questions: 5Answers: 0

    Thanks, Kevin!

Sign In or Register to comment.