Ajax and custom html ?

Ajax and custom html ?

AlexolAlexol Posts: 11Questions: 4Answers: 0
edited July 2018 in Free community support

Hello, pardon my english. I'm newbie in datatables and I ask you a question :

$('#example').DataTable( {
    serverSide: true,
    ajax: '/data-source'
} );

Imagine data source return this JSON :
{
"draw": 1,
"recordsTotal": 2,
"recordsFiltered": 2,
"data": [
[
"1",
"Google",
"www.google.fr"
],
[
"2",
"Blabla",
"www.blabla.fr"
]
]
}

I want put this data in my html that (no indented, but sample):

<tr><td>1</td><td><a href="www.google.fr">Google</a></td></tr>
<tr><td>2</td><td><a href="www.blabla.fr">Blabla</a></td></tr>

Thank you for your answer

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,129Questions: 26Answers: 4,916
    Answer ✓

    You would use columns.render to build the links. The docs have an example.

    Kevin

  • AlexolAlexol Posts: 11Questions: 4Answers: 0
    edited July 2018

    Thank you,

    I have an another problem with Flask Jinja 2 (if you know...)...

    "columns":
                    [
                        {
                            "data": "pk_book",
                            "render": function(data){
                                 return '<a href="{{ url_for('bp.book', pk_book='+data+') }}/'+data+'"> '+data+'</a>';
                            }
                        },
    

    Obsiousvly, Jinja transform before Javascript working, beacause url is: http://127.0.0.1/book/+data+

    How can I change this ? Thank you

  • AlexolAlexol Posts: 11Questions: 4Answers: 0

    Bad edit in my previous post

    I would write this :
    return '<a href="{{ url_for('bp.book', pk_book='+data+') }}"> '+data+'</a>';

  • AlexolAlexol Posts: 11Questions: 4Answers: 0

    UP

  • kthorngrenkthorngren Posts: 21,129Questions: 26Answers: 4,916
    edited July 2018

    I'm not familiar with the url_for function of Flask but you probably need to escape some of the single quotes with back slashes, for example:

    return '<a href="{{ url_for(\'bp.book\', pk_book=\'+data+\') }}"> '+data+'</a>';
    

    This way the single quotes are apart of the string.

    Kevin

  • AlexolAlexol Posts: 11Questions: 4Answers: 0
    edited July 2018

    Ok, but the problem is url_for working before javascript, so, the variable in pk_book is... "+data+"...

  • AlexolAlexol Posts: 11Questions: 4Answers: 0

    url_for is a simple function which return a link.

  • allanallan Posts: 63,116Questions: 1Answers: 10,397 Site admin

    Sounds like you need to have Flask evaluate that function before the Javascript is executed. I think you'd probably need to ask in a Flask forum for how to do that.

    Allan

This discussion has been closed.