add buttons to multiple tables

add buttons to multiple tables

montoyammontoyam Posts: 568Questions: 136Answers: 5

I need to only show add/edit/delete buttons depending on the user permissions. I found the following post that seems will do the trick:
https://datatables.net/forums/discussion/comment/160764/#Comment_160764

however, I have multiple tables per page. Is there a way to add buttons multiple tables without hard coding the table names?

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586
    Answer ✓

    You would need to hard code the names, as each table would have its own Editor instance.

    Colin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    but the buttons are added to the datatable instances, not the editors, right?

                var applicationtable = $('#Applications').DataTable({
                    dom: 'Bfrtip',
                    ajax: {
    
  • kthorngrenkthorngren Posts: 20,247Questions: 26Answers: 4,761

    I think Colin is referring to this code to add the Editor buttons:

            buttons: [
                { extend: "create", editor: editor },
                { extend: "edit",   editor: editor },
                { extend: "remove", editor: editor }
            ]
    

    You need to provide the editor instance variable.

    Kevin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    ah, yes. I realized this as I was starting to add buttons. thanks.

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    I didn't realize you need to add each button separately (or if there is a way to add create, edit, delete in one command I couldn't figure it out). In my situation, it is either all or nothing with the buttons. they don't have the need to have only some users add, some only edit.

    So, instead I did the inverse. All the buttons are created as normal, then:

            if (allowEdits == 0) {
                applicationtable.buttons().destroy();
                contactstable.buttons().destroy();
                dependencytable.buttons().destroy();
            }
    
  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    That's one way of doing it - the other is to just not declare originally if that user doesn't have permission.

    Colin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    so you can put a function inside of buttons: [] ??

    What is the syntax for this?

  • colincolin Posts: 15,142Questions: 1Answers: 2,586
    Answer ✓

    You could do something like this.

    Colin

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    oh, this is much cleaner. Thanks.

This discussion has been closed.