Rename Object Key '#' as 'id' or How to call "row.#"
Rename Object Key '#' as 'id' or How to call "row.#"
//------------------api.txt--------------------
{
"limit": 1000000,
"offset": 0,
"order": false,
"nitems": 293,
"items": [
{
"#": "001",
"name": "product #1",
"price": 1000,
"service": "service,usedcars",
"active": 1
},
{
"#": "002",
"name": "product #2",
"price": 5200,
"service": "service",
"active": 1
},
{
"#": "003",
"name": "product #3",
"price": 330,
"service": "service,usedcars",
"active": 0
}
Here is my code
$(document).ready(function () {
$('#example').DataTable({
ajax: {
url: "api.txt",
dataSrc: 'items'
},
columns: [
{ data: '#', title: "ID" },
{ data: 'name' },
{ data: 'price', render: $.fn.dataTable.render.number(',', '.', 2) },
{ data: 'service' },
{ data: 'active' }
],
columnDefs: [
{
targets: 5,
data: "action",
render: function (data, type, row, meta) {
linkEdit = '<a href="' + row.# + '">edit</a>';
linkDelete = '<a href="' + row.id + '">delete</a>';
return linkEdit + " " + linkDelete;
}
}
]
});
});
Problem: row.{key}
Replies
I presume you are getting an error message about this line:
Without a test case, as requested in the forum rules and the new question template text which you appear to have deleted without filling in, it is very difficult to be sure that is the issue.
I'm going to guess it is since it isn't valid Javascript. use
row['#']
instead ofrow.#
.Allan
Its not really clear what you are asking or what error you are getting. If
row.#
is not working mayberow['#']
will work. If you want the data to useid
instead of#
then you should do that in your server script and store the ID in the object you want instead of#
.Kevin
Thanks for helping me. (allan and kthorngren)
Wow i got it
correct
row['#']