0

I want to customize JSON data from back-end before show it with datatables. First I want to replace the spaces ( ) in column artist.name with underscore symbol (_). Then, I want to hyperlink each row in that column with a wiki link like my code.

HTML:

        <table id="albums" class="table table-striped table-bordered" style="width:100%" >
            <thead>
                <tr>
                    <th>Rank</th>
                    <th>Artist</th>
                    <th>Album name</th>
                    <th>Year</th>
                    <th>Genres</th>
                </tr>
            </thead>
        </table>

Datatables-JS:

"columns": [
        {"data": "rank", "searchable": false},
        {
            "data": "artist.name".replace(/\s+/g, '_'),      // ex: 'The Beatles' => 'The_Beatles'
            "name": "artist.name".replace(/\s+/g, '_'),

            "render": function (data, name) {

                    data = '<a href= "https://en.wikipedia.org/wiki/' + name + '">' + name + '</a>';  // render to: https://en.wikipedia.org/wiki/The_Beatles

                return data;
            } 
        },
        {"data": "name"},
        {"data": "year"},
        {"data": "genres", "name": "genres.name", "sortable": false},
    ]

JSON data:

{
"data": [
    {
        "DT_RowId": "row_1",
        "DT_RowAttr": {
            "data-pk": 1
        },
        "rank": 1,
        "name": "Sgt. Pepper's Lonely Hearts Club Band",
        "year": 1967,
        "artist_name": "The Beatles",
        "genres": "Psychedelic Rock, Rock & Roll",
        "artist": {
            "id": 2,
            "name": "The Beatles"
        }
    },
    {
        "DT_RowId": "row_2",
        "DT_RowAttr": {
            "data-pk": 2
        },
        "rank": 2,
        "name": "Pet Sounds",
        "year": 1966,
        "artist_name": "The Beach Boys",
        "genres": "Pop Rock, Psychedelic Rock",
        "artist": {
            "id": 3,
            "name": "The Beach Boys"
        }
    }  
    ...      
],
"recordsFiltered": 15,
"recordsTotal": 15,
"draw": 1

}

It does not work. Can anyone help me? Thanks.

4
  • can you show your json data? Commented Jul 23, 2018 at 8:37
  • I have added it. Commented Jul 23, 2018 at 8:45
  • It does not make sense. Why would you change the data attribute to a none existing property? There is no The_Beatles property in the JSON. The result would be an error. And even if there were a The_Beatles index it does not add up, since you would in fact try to change the name or title of the column multiple times according to row data. Columns can only have one title and one name. Commented Jul 23, 2018 at 9:04
  • OK, I get it but what about the render (hyperlink) in column? @davidkonrad Commented Jul 23, 2018 at 9:48

2 Answers 2

1

OK, thanks, everyone. I fixed it with this change.

"columns": [
        {"data": "rank", "searchable": false},            
        {
            "data": "artist.name",      
            "name": "artist.name",
            "render": function (data, type, row, meta) {
                data = '<a href= "https://en.wikipedia.org/wiki/' + data.split(' ').join('_') + '">' + data + '</a>';
                return data;
            }           
        },
        {"data": "name"},
        {"data": "year"},
        {"data": "genres", "name": "genres.name", "sortable": false},
    ]
Sign up to request clarification or add additional context in comments.

2 Comments

Great you figured it out! Remember to mark your answer as accepted. Perhaps it can help future visitors.
Sure I will, @davidkonrad.
0

You can use .replace(/ /g,"_"); or .split(' ').join('_'); functions for that as answered here: Replacing spaces with underscores in JavaScript?

1 Comment

I think this is also depending on datatables properties. And the way I define like that inside columns option not right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.