Employing ng-click within a displayed column of Datatable

Currently, I am utilizing a plain Datatable in AngularJS without any Angular-Datatable libraries. Everything appears to be working well except for one issue.

Here is the datatable code snippet:

$scope.siInfoTable = $('#siInfoTable').DataTable({
            'columns': [
                {
                    "render": function (data, type, row, meta) {
                        data =  '<button class="btn btn-primary btn-sm" ng-click="test()">TEST</button>';
                        return data;
                    }
                }]
        });

This code is executed within a $scope.load method and is loaded before the page finishes rendering. However, when a user clicks on the button generated in the datatable, it does not seem to recognize the ng-click directive.

Is there a solution to this issue?

Answer №1

If you want to avoid issues, it is recommended to utilize the angular-datables angular library.

  1. Any changes in your controller or directive may not be reflected in the output HTML.
  2. You might encounter repeated errors in your application.

To simplify the process and address any problems you are encountering, consider implementing angular data tables.

Referenced from Angular Datatables Documentation


    angular.module('datatablesSampleApp', ['datatables', 'datatables.buttons']).
    controller('sampleCtrl', function($scope, DTOptionsBuilder) {
        $scope.dtOptions = DTOptionsBuilder.newOptions()
          .withPaginationType('full_numbers') 
          .withDisplayLength(2)
          .withOption('order', [1, 'desc']).withButtons([{
        text: '<button class="btn">Some button</button>',
        key: '1',
        action: function(e, dt, node, config) {
          alert('Button activated');
        }
      }]);
    });
<!DOCTYPE html>
<html>

<head> 
    <link rel="stylesheet" href="//cdn.datatables.net/1.10.1/css/jquery.dataTables.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.9/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-datatables/0.6.2/angular-datatables.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-datatables/0.6.2/plugins/buttons/angular-datatables.buttons.min.js"></script>
    <script src="script.js"></script>
</head>

<body ng-app="datatablesSampleApp">
    <div ng-controller="sampleCtrl">
        <table datatable dt-options="dtOptions">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>First name</th>
                    <th>Last name</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>Foo</td>
                    <td>Bar</td>
                </tr>
                <tr>
                    <td>123</td>
                    <td>Someone</td>
                    <td>Youknow</td>
                </tr>
                <tr>
                    <td>987</td>
                    <td>Iamout</td>
                    <td>Ofinspiration</td>
                </tr>
            </tbody>
        </table>
    </div>
</body>

</html>

Answer №2

Make sure to compile your HTML first before assigning it to data by injecting $compile into your controller.

app.controller('mycontroller', ['$compile', ..., function($compile, ...)

Afterwards, you can proceed to compile your HTML like this:

data =  $compile('<button class="btn btn-primary btn-sm" ng-click="test()">TEST</button>')($scope);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm encountering an issue with the error message [ng:areq] stating that the argument 'employeeObj' is not a function and is undefined. I am currently unable to identify the missing component causing this error

I encountered an Error: [ng:areq] Argument 'employeeObj' is not a function, got undefined, and I'm struggling to identify what I missed. Can anyone provide assistance? <html xmlns="http://www.w3.org/1999/xhtml"> <head> < ...

What is the best way to maintain the structure of HTML content in a jQuery AJAX call?

I'm currently working on a project that involves implementing AJAX, and I've chosen jQuery as the JavaScript Library. Here's the HTML code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/x ...

determining the number of td elements in a row of a table located within an iframe

When I interact with a cell in a table within an iframe, I want to determine the number of cells in that row. If a cell is actually a span element, I would like to consider it as part of the cell count. The iframe may contain multiple tables without any s ...

Experiencing a 400 error while transitioning from ajax to $http?

I am facing an issue while trying to make a GET request using $http instead of ajax. The call functions perfectly with ajax, but when attempting the same with $http, I encounter a 400 (Bad Request) error. I have referenced the Angular documentation and bel ...

Creating a dropdown navigation menu using jQuery

I have been experimenting with creating a customized Drop Down menu using ul li and Jquery, following this helpful Tutorial. Here is the sample HTML Code for testing: <div id="dd" class="wrapper-dropdown-3" tabindex="1"> <span>OS</span> ...

Leveraging the power of JavaScript Math methods to dictate the Co-ordinates of HTML Canvas .fillRect

Greetings to everyone! I have dedicated my entire evening to understanding how to implement the (Math.floor(Math.random()) function as the coordinates for the .fillRect method on an HTML canvas element. Despite searching through this website and various ...

Difficulty with replacing colors in an image on certain devices when using HTML5 Canvas

I have created a 2d RTS HTML5 / Javascript game that utilizes images to represent the player's units and buildings. To achieve different variations of these images with different colors, I use a script that replaces certain colors in the original imag ...

The data being transmitted by the server is not being received accurately

Hey there! I've recently started using express.js and nodejs, but I've encountered an issue where my server is sending me markup without the CSS and JS files included. const express = require('express'); const app = express(); const htt ...

Centering <th> elements is key for achieving a visually appealing print layout

Is there a way to center align the header and body of a table when printing it? I am using datatable. Here is how it currently looks: Check out this screenshot I have tried adding classes to the th tags in the HTML table, but they still appear aligned t ...

Ways to attach JQuery UI Sortable widget to html content fetched through Ajax requests?

Here's a straightforward question for you. Take a look at my JavaScript/jQuery code snippet below: $('body .combo-table').sortable({ handle: '.grabber', opacity: 0.9, axis: 'y', start: function (e, ui) { ...

From creating a simple jQuery fiddle, let's delve into the world

Here is a code snippet I'm trying to transition from jQuery to an Angular directive. Visit this link to view the original code: http://jsfiddle.net/rhtr1w04/ Below is my implementation in app.js: angular.module('app',[]).directive('an ...

Implementing jQuery form validator post anti-SPAM verification?

I am facing what seems like a straightforward JavaScript issue, but my knowledge in this area is still limited. Following a successful implementation of basic anti-SPAM feature that asks the user to solve a simple math problem, how can I integrate jQuery& ...

Information gathered from checkboxes and dropdown menus

When gathering information from my form fields, I know how to capture data from input fields using this code: 'formData' : { 'timestamp' : '<?php echo $timestamp;?>', 'token' : '<?php echo md5(&a ...

jQuery user interface Dialog buttons

Is it possible to access the button within a click event handler when creating a dialog with buttons like: buttons: { 'button text': function(){ // do something }, In this ...

How can I retrieve the id of a Jquery Object (this) in a JavaScript file?

I have been experimenting with a content carousel slider for my project. Everything has been working smoothly so far. However, I now need to include the same slider twice on a single page. To prevent conflicts caused by similar class names used by the Jav ...

What is the best way to combine jQuery objects in order to apply CSS styles collectively?

If I have a custom function that takes in multiple jQuery objects: customFunction($('.content'), $('h1'), $('input[type=button]')); The function is defined as follows: function customFunc ...

Achieving the functionality of keeping the search query box and alphabetical search options visible on the page even after performing a search and displaying the results can be done by

I have set up a PHP page with search query field and alphabetical search options. When submitting the query, the results are displayed on the same page; however, I would like the results to show in the same location as the alphabetical search. Currently, ...

Limit entry to a webpage based on the user's category

Greetings, I have successfully implemented a login feature using C# webservice and ajax calls. However, there seems to be a security issue that I need assistance with. In my application, I have two types of users - admin and regular user. The problem arise ...

JavaScript can be used to deactivate the onclick attribute

Is there a way to deactivate one onclick event once the other has been activated? Both divs are initially hidden in the CSS. I'm new to programming, so any help is appreciated. Markup <a id="leftbutton" href="#" onclick="showDiv(this.id); return ...

Assign a value to an element based on a specific attribute value

I'm working with the following element <input type="text" size="4" name="nightly" value="-1"> My goal is to update the value to 15.9 specifically when name="nightly" Here's what I've attempted so far: document.getElementsByName(&ap ...