Home > Blockchain >  Set the value of a variable from a value found in a table cell within an Ajax call using jQuery Data
Set the value of a variable from a value found in a table cell within an Ajax call using jQuery Data

Time:10-19

How can I set the value of a variable (or to be more specific - a button attribute in each row) from a value found in another cell on the same row? The data in this example is being passed from an MVC controller to an Ajax success function:

$.ajax({
            type: "POST",
            url: '@Url.Action("GetData", "Home")',
            cache: false,
            retrieve: true,
            data: data,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            defaultContent: true,
            success: successFunc,
            error: errorFunc
        });
        function successFunc(data) {
            var firstName; //I'll need to set this variable with the name found in each row and assign that value to each Edit button attribute.
            $("#tblMyTable").DataTable(
                {
                    "info": true,
                    "data": data.list,
                    "responsive": true,
                    "autoWidth": false,
                    "bAutoWidth": false,
                    "retrieve": true,
                    columns: [
                        { 'data': "ID", }, 
                        { 'data': "First_Name" }, //This is the value I need. How can I set the value of the variable firstName using this data so that each button contains the attribute data-name with the value of the name column found in the current row?
                        { 'data': "Last_Name" },
                        { 'data': 'Edit', defaultContent: '<button type="button" data-name="'   firstName   '" href="javascript:;">Edit</button>' }

                    ]
                });

        }
        function errorFunc() {

        }
    }

CodePudding user response:

Using the render function within columnDefs worked for me. columnDefs should follow the columns option:

"columnDefs": [
    {
        "targets": [3],
        "render": function (data, type, row, meta) {
            {
                return '<button type="button" data-name="'   row.First_Name   '" href="javascript:;">Edit</button>'
            }
        }
    }
]
  • Related