how to add JS variable in defaultContent ?

how to add JS variable in defaultContent ?

calaverocalavero Posts: 5Questions: 3Answers: 0

hi, i wanna pass an input value to my django url which located in defaultContent. However i couldnt fit it in. I've tried doing " + file_id +" thing but didnt work.

var fault_id = $('#file_id').val()

let table = $('#datatables').DataTable({
    "processing": false,
    "serverSide": false,
    "paging": false,
    "searching": true,
    "dom": 'lrtip',
    "ajax": {
        "url": "/api/file",
        "type": "GET",
        "dataSrc": '',
    },
    "initComplete":function( settings, json){

        checkInitialFault();
            // call your function here
    },
    "drawCallback": function( settings ) {
        checkInitialFault();
    },
    "columns": [
        {"data":null,

        "defaultContent": '<label class="checkbox checkbox-lg"> </label>',
        },
        {"data": "id"},
        {"data": "filenamel"},
        {"data": "user.first_name"},
        {"data": "created_date"},
        {
            "data": null,
            "defaultContent": '<a href="{% url "download_file" ???file_id??? %}"><i style="font-size: 1.50rem !important;" class="fa fa-download"></i></a>'
        }
    ],

});

any help would appreciated.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    Answer ✓

    Where does file_id come from? You might be better off using columns.render, as that way you can pass it in as just another column and render it in that function,

    Colin

  • calaverocalavero Posts: 5Questions: 3Answers: 0
    edited September 2021

    it comes from a hidden value, <input id="file_id" name="hidden_file_id" type="text" style="visibility: hidden">

    Actually i'm trying to put the value from '{"data":"id"}' into defaultContent tag. i just store it in a hidden field and passing it through js, but this may not be the best solution. Do you know how can i use that value in other column definitions?

  • calaverocalavero Posts: 5Questions: 3Answers: 0

    okay i fixed this thanks to your suggestion @colin .

    i've updated that data field as below:

    {
                "data": "id",
                "render": function ( data, type, row, meta ) {
                return '<a href="'+ data +'"><i style="font-size: 1.50rem !important;" class="fa fa-download"></i></a>';
                },
            }
    
Sign In or Register to comment.