3

I am using the following with npm:

"dependencies": {
    "angular": "1.6.4",
    "datatables.net": "1.10.19",
    "datatables.net-buttons": "1.5.3",
    "datatables.net-buttons-dt": "1.5.3",
    "angular-datatables": "0.6.2",
    // and more that is not question related
}

When I now create a DataTable like angular-datatables tell in the examples, everything is working great.
For example:

vm.dtOptions = DTOptionsBuilder
    .newOptions()
    // data fetch and processing animation and ...
    .withButtons([
        'colvis',
        'copy',
        'print',
        'excel',
        'pdf',
        {
            text: 'Some button',
            key: '1',
            action: function (e, dt, node, config) {
                alert('Button activated');
            },
            className: 'someCustomCssClass',
        },
    ])
    .withBootstrap();
vm.dtColumns = [
    DTColumnBuilder.newColumn('id').withTitle('ID').withClass('text-danger'),
    DTColumnBuilder.newColumn('firstName').withTitle('First name'),
    DTColumnBuilder.newColumn('lastName').withTitle('Last name')
];

Now I want to modify the custom Button "Some button".
I want the button to have a custom HTML attribute.

Current the Button gets rendered like this:

<button class="dt-button someCustomCssClass ng-scope" tabindex="0" aria-controls="DataTables_Table_0" type="button">
    <span>Some button</span>
</button>

But the button should have an "access" attribute in order to hide/show the button depending on the user roles.
So it should be for example:

<button access="ROLE_ADMIN" class="dt-button someCustomCssClass ng-scope" tabindex="0" aria-controls="DataTables_Table_0" type="button">
    <span>Some button</span>
</button>

But how can I add a new attribute to a button?
Adding a custom CSS class is easy with "className", but a completely new attribute?

Thanks for the help and
greetings

UPDATE

Currently I am appending a completly custom button like this:

var myEl = angular.element( document.querySelector( '.dataTables_wrapper > .dt-buttons' ) );
myEl.append('<button with all my needs></button>');
$compile(myEl.contents())(scope);

This is working, but now I can not use the DataTable information and the integration itself is very poor.
Also it is a workaround and no good solution at all!
I really hope the init that @davidkonrad is mentioned can solve this.

1 Answer 1

1

Use the init callback for buttons you want to enrich with custom attributes :

.withButtons([
    'colvis',
    'copy',
    'print',
    'excel',
    'pdf',
    {
        text: 'Some button',
        key: '1',
        action: function (e, dt, node, config) {
            alert('Button activated');
        },
        className: 'someCustomCssClass',
        //-----------------------------------
        init: function(dt, node, config) {
            node.attr('access', "ROLE_ADMIN")
        }
    },
])
Sign up to request clarification or add additional context in comments.

4 Comments

with this the attribute gets added, but my directive will not trigger. so in the DOM everything looks great, but without working functionality. Currently I use a service that can $compile the buttons with a timeout. But this is not a nice solution. So is there maybe a other way? the init itself is a great first step.
@christopher2007, Sad to say no. if you want to add directives to buttons you do not have any other options than $compile with a $timeout. DT is jQuery to the extreme, angular-datatables is a wrapper that do the dirty work so it not conflict with angularjs digestion cycles; buttons is yet another layer on top of that which is not possible to angularish' or wrap into a directive because it is managed from within the DT core. In fact, customising the buttons extension besides manipulating options etc. is also impossible in the jQuery version (unless you rewrite the extension yourself)
ok, th anks a lot. I was afraid to hear exact that answer :) Maybe you know a good dt alternative in pure AngularJS? Otherwise I got myself a dirty workaround. Not proud at all ...
@christopher2007, Thank you for accepting the answer! I have tried "all" "native" angularjs directive attempts to do something like DataTables, but DT is still by far the best in the world. It beats everything in speed and flexibility. If L-Lin not have made the wrapper directives for DT I would have made them myself (or at least tried :) I can live with the $timeouts in the code, and that we sometime need to make "ugly" jQuery that conflicts with the declarative "angular way". It is not pretty, but on the bottom line it is mostly a cosmetic or "aesthetic" issue, the end user dont care :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.