HEEN
HEEN

Reputation: 4721

Call an AngularJS function on button click is not working

I have a requirement where I want to call an AngularJS function on button click. So I tried like below

var app2 = angular.module('grdContrl', ['datatables']);
app2.controller('dtAssignVendor', ['$scope', '$http', 'DTOptionsBuilder', 'DTColumnBuilder',
    function ($scope, $http, DTOptionsBuilder, DTColumnBuilder) {

        $scope.GetFiler = function () {
            var strZone = $('#SAPExecutive_R4GState').val();
            var strUtility = $('#ddlUtility').val();

            $scope.dtColumns = [
                DTColumnBuilder.newColumn(null, '').renderWith(function (data, type, full) {
                    return '<input type="checkbox" class="check" data-object-id="' + full.objectid + '">'
                }),
                DTColumnBuilder.newColumn("MAINTENANCEZONENAME", "MAINTENANCEZONENAME"),
                DTColumnBuilder.newColumn("MAINTENANCEZONECODE", "MAINTENANCEZONECODE")
            ]
            $scope.dtOptions = DTOptionsBuilder.newOptions().withOption('ajax', {
                url: AppConfig.PrefixURL + "/App/GetMPFilter",
                type: "POST",
                data: JSON.stringify({ strZone: strZone, strUtility: strUtility }),
            })
            .withPaginationType('full_numbers')
            .withDisplayLength(10);
        }        
}])
<button class="btn btn-default customBtn" ng-click="GetFilter();">
    <i class="fa fa-filter" aria-hidden="true"></i>
  Filter
</button>


---------------------------

<div ng-app="grdContrl">
    <div class="flTable" id="dtAssignVendor">
        <table id="assignVender" class="mp myTable table table-striped table-bordered" cellspacing="0" width="100%">                            
        </table>
    </div>
</div>

I want this to work on the above mentioned button click.

Upvotes: 0

Views: 544

Answers (3)

Mayank Vadiya
Mayank Vadiya

Reputation: 1451

You can not get filter values, because filter button is outside of controller and if you are using datatable then also add dt-option, dt-columns and dt-instance into <table> as attributes.

You have do like this way

<div ng-app="grdContrl" ng-controller="dtAssignVendor"> <!-- COMMON ANCESTOR-->
  <button class="btn btn-default customBtn" ng-click="GetFilter();">
    <i class="fa fa-filter" aria-hidden="true"></i> Filter
  </button>
  <div class="flTable" id="dtAssignVendor">
     <table id="assignVender" class="mp myTable table table-striped table-bordered" cellspacing="0" width="100%" datatable="" dt-options="dtOptions" dt-columns="dtColumns"  dt-instance="dtInstanceNonInvProduct">                            
     </table>
  </div>
</div>

Also inject into controller.

app2.controller('dtAssignVendor',function ($scope, $http, DTOptionsBuilder, DTColumnBuilder,DTInstances) {
       $scope.GetFiler = function () {
     //get input values into scope instead of javascript variable
    //var strZone = $('#SAPExecutive_R4GState').val();
    //var strUtility = $('#ddlUtility').val();
    $scope.strZone = $scope.SAPExecutive_R4GState;
    $scope.strUtility = $scope.ddlUtility;
    //redraw table on button click
    $scope.dtInstanceNonInvProduct.DataTable.draw();

}  
$scope.dtOptions = DTOptionsBuilder.newOptions().withOption('ajax', {
    url: AppConfig.PrefixURL + "/App/GetMPFilter",
    type: "POST",
    data: JSON.stringify({ strZone: $scope.strZone, strUtility: $scope.strUtility }),
})
.withPaginationType('full_numbers')
.withDisplayLength(10);
$scope.dtColumns = [
    DTColumnBuilder.newColumn(null, '').renderWith(function (data, type, full) {
        return '<input type="checkbox" class="check" data-object-id="' + full.objectid + '">'
    }),
    DTColumnBuilder.newColumn("MAINTENANCEZONENAME", "MAINTENANCEZONENAME"),
    DTColumnBuilder.newColumn("MAINTENANCEZONECODE", "MAINTENANCEZONECODE")
]

$scope.dtInstanceNonInvProduct = {};
})

Upvotes: 1

Karim
Karim

Reputation: 8632

As far as i see you did not bind the controller to your template, you need to use the ng-controller directive and wrap the button inside it (and correct the spelling error on $scope.getFilter as raised in the comments)

<div ng-app="grdContrl" ng-controller="dtAssignVendor"> <!-- COMMON ANCESTOR-->
  <button class="btn btn-default customBtn" ng-click="GetFilter();">
    <i class="fa fa-filter" aria-hidden="true"></i> Filter
  </button>


 ---------------------------
  <div class="flTable" id="dtAssignVendor">
     <table id="assignVender" class="mp myTable table table-striped table-bordered" cellspacing="0" width="100%">                            
     </table>
  </div>
</div>

Upvotes: 0

Avantika Saini
Avantika Saini

Reputation: 792

The template of your code should be something like this:

<div ng-app="myApp">
    <div controller="ctrl1">
        <button ng-click="someFunInCtrl1()">Click Me</button>
        ...
    </div>
    <div controller="ctrl2">
        <button ng-click="someFunInCtrl2()">Click Me</button>
        ...
    </div>
</div>

You are unable to call GetFilter function because you are calling it from outside controller scope. As @Karim said

you need to use the ng-controller directive and wrap the button inside it

Upvotes: 0

Related Questions