add buttons to multiple tables

add buttons to multiple tables

montoyammontoyam Posts: 149Questions: 38Answers: 3

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: 8,085Questions: 0Answers: 1,357
    Answer ✓

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

    Colin

  • montoyammontoyam Posts: 149Questions: 38Answers: 3

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

                var applicationtable = $('#Applications').DataTable({
                    dom: 'Bfrtip',
                    ajax: {
    
  • kthorngrenkthorngren Posts: 8,197Questions: 25Answers: 1,844

    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: 149Questions: 38Answers: 3

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

  • montoyammontoyam Posts: 149Questions: 38Answers: 3

    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: 8,085Questions: 0Answers: 1,357

    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: 149Questions: 38Answers: 3

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

    What is the syntax for this?

  • colincolin Posts: 8,085Questions: 0Answers: 1,357
    Answer ✓

    You could do something like this.

    Colin

  • montoyammontoyam Posts: 149Questions: 38Answers: 3

    oh, this is much cleaner. Thanks.

Sign In or Register to comment.