Rename Object Key '#' as 'id' or How to call "row.#"

Rename Object Key '#' as 'id' or How to call "row.#"

JuJu_RangerJuJu_Ranger Posts: 2Questions: 0Answers: 0

//------------------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

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    I presume you are getting an error message about this line:

    linkEdit = '<a href="' + row.# + '">edit</a>';

    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 of row.#.

    Allan

  • kthorngrenkthorngren Posts: 21,327Questions: 26Answers: 4,949

    Its not really clear what you are asking or what error you are getting. If row.# is not working maybe row['#'] will work. If you want the data to use id instead of # then you should do that in your server script and store the ID in the object you want instead of #.

    Kevin

  • JuJu_RangerJuJu_Ranger Posts: 2Questions: 0Answers: 0

    Thanks for helping me. (allan and kthorngren)
    Wow i got it

    correct
    row['#']

This discussion has been closed.