Removing quotation marks from ajax data
Removing quotation marks from ajax data
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
One option is to use
columns.render
with a regex expression to remove the"
from the string. Maybe something like thisstr.replace(/^"|"$/g, '')
.Kevin
@kthorngren How would I stringify the data?
@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, "");
}
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?
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 thecolumns.render
function since it will be the same for multiple columns. Something like this:The above may have syntax errors as I didn't test it
Kevin
Thanks, Kevin!