button disable not working
button disable not working
montoyam
Posts: 568Questions: 136Answers: 5
I am having trouble getting the button.disable to work. I am guessing I have the wrong syntax somewhere? I am getting the 'got here' alert, so I know the if statement is correct.
var SubmissionNotesTable = $('#SubmissionNotes').DataTable({
dom: 'Bfrtip',
ajax: {
url: 'api/SubmissionNotes',
type: 'post',
data: function (d) {
var selected = SubmissionsTable.row({ selected: true });
if (selected.any()) {
d['SubmissionIDFilter'] = selected.data().Submissions.SubmissionID;
}
}
},
columns: [
{ data: "SubmissionNotes.SubmissionNote" },
{ data: "SubmissionNotes.EnteredBy" },
{ data: "SubmissionNotes.RecordAdded" },
{
data: "Attachments.FileName",
render: function (data, type, row) {
return data? "<a href=" + row.Attachments.WebPath + " target='_blank'>" + data + "</a>":null;
}
},
],
select: { style: 'single' },
//order: [[2, 'asc']],
lengthChange: false,
buttons: [
{ extend: 'create', editor: SubmissionNotesEditor, enabled: false},
{ extend: 'edit', text: 'Edit/View', editor: SubmissionNotesEditor },
{ extend: 'remove', editor: SubmissionNotesEditor }
]
});
/**************************************************/
if (permissionID != 1) {
alert("got here");
SubmissionNotesTable.button('remove:name').disable();
}
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
you need to assign a name to the remove button. Something like
{ extend: 'remove', editor: SubmissionNotesEditor, name: 'remove' }
Here is one with name and className --- name is like #id and className is like .class - if you know what I mean ...
{ extend: "create", editor: ctrEditor, className: "editorOnly notDuplicate", name: "createContract"},
Forgot to mention: Your code might not work for a differen reason as well: You would need to use an event. Javascript is asynchronous ... I know it is a pain. So, you should execute your "permissionsID" code only on "init" because that makes sure table initialization has been completed when your code is being executed. Check the data tables event list. (It is late; no links. You'll find it in the docs.)
https://datatables.net/reference/event/init
No, permissionsID is loaded in an ajax code and on success I 'draw' the datatables:
```
$(document).ready(function () {
userNameCookie = readCookie('userNameCookie');
$.ajax({
url: "api/GetPermissionsID",
dataType: 'json',
method: "POST",
success: function (response) {
// returns: {"Table":[{"PermissionID":1}]}
if (response.Table.length !== 0) {
permissionID = response.Table[0].PermissionID;
} else { permissionID = 0; }
I have added a name for the button, but it still is not being disabled. the alert popup is working so I know it gets that far.
ok, just wrap your permissionId logic in an event. Your alert is probably triggered at a point when table initialization hasn't been completed. But Javascript doesn't "wait" ... hence you need to have an event telling you that the data table is ready.
that did not seem to work either. here is most of the js in case there is something else going on...warning...there is a lot and I'm sure some of the code could be re-written more efficiently!!
Your code to disable the button works in this example:
http://live.datatables.net/pasigebo/1/edit
Its been simplified for the example. Does the
alert("got here");
fire? Doesn't look likepermissionID
is a global variable so not sure where it would get the value.Kevin
yes, the alert is successfully triggered. I just removed the if() condition and the button still does not disable. How odd is that.
I have used buttons.destroy successfully in another application, but you can't destroy a single button, just the collection, right?
Are you trying to disable or remove? If remove then use
button().remove()
. I just realized that you can disable theremove
button but it will automatically be enabled when selecting a row. If you want to leave it on the page but keep it disabled then you will need to add your if statement and disable code into theselect
event.Kevin
oh my goodness, thank you so much. I didn't know about remove and didn't realize that the typical enable/disable code was overriding my code. I don't care if it is disabled or removed, whatever works, and it looks like remove is the answer.
so, with what you showed me about assigning a name to a button, this now works: