Disable Delete button

Jagjit Saini 106 Reputation points
2021-06-03T10:56:05.007+00:00

Hi

I want if IsActive = "N" then Delete button of that row should get Disabled.

$('#tblLocation').DataTable({
"ajax": {
"url": '/Location/List',
"type": "get",
"datatype": "json",
dataSrc: ""
},
paging: true,
sort: true,
pageLength: 10,
searching: true,
"columns": [
{ "data": "Id", "autoWidth": true },
{ "data": "Description", "autoWidth": true },
{ "data": "IsActive", "autoWidth": true },
{
"data": "Id", "render": function (data) {
return "<a class='btn btn-primary btn-sm' data-Id=" + data + " id='btnEdit'><i class='fa fa-pencil'></i> Edit</a> <a class='btn btn-danger btn-sm' data-Id=" + data + " id='btnDelete' style='margin-left:5px' ><i class='fa fa-trash'></i> Delete</a>";
}
}
]
});

Thanks

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,311 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yijing Sun-MSFT 7,071 Reputation points
    2021-06-04T01:33:31.54+00:00

    Hi @Jagjit Saini ,
    You could use render in the datatable columns. "style="pointer-events: none" could disabled the button.
    Just like this:

    102262-test.txt

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Michael Taylor 49,246 Reputation points
    2021-06-03T14:45:57.093+00:00

    Given your specific example you can do it just like you're doing the data-id attribute. Something like this (you'll need to play around with it):

    "render": function (data) {
    var isActive = data["IsActive"] === true;
    
    if (isActive) {
       return "<a class=... > Delete</a>
    } else
    {
       <span>Delete</span>
    }
    }
    

    The point here is that you really cannot disable an anchor. It doesn't even make sense. You can google for why disabling an anchor is not the correct choice. Instead use an anchor if you need it to be active or a span to just display the anchor text if you don't. You can use CSS to ensure the styling lines up.

    0 comments No comments

  2. Duane Arnold 3,216 Reputation points
    2021-06-03T15:16:04.817+00:00

    @Jagjit Saini

    IMHO, you need to send 'data' into a JavaScript function looping over the Jason array in 'data' and build the HTML manually using a HTML table and enable or disable the btn based on the property in the Json object's property.

    0 comments No comments