0

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.

7
  • probably because your function in controller is called GetFiler, though in template you are referencing GetFilter Commented Feb 5, 2019 at 10:13
  • @LuninRoman: ohhh, my mistake. let me try and check Commented Feb 5, 2019 at 10:17
  • @LuninRoman: nothing happened, I tried adding an alert but that too is not firing Commented Feb 5, 2019 at 10:19
  • @BN, Please make sure that you bind the controller. Commented Feb 5, 2019 at 10:31
  • @Sultan: yes I have binded it. jsfiddle.net/e3gu2hca. for more info have a look here jsfiddle.net/e3gu2hca/1 Commented Feb 5, 2019 at 10:32

3 Answers 3

1

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 = {};
})
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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>

8 Comments

my button is in another div, so will it work as per your answer ?
need to stay in a common ancestor that has the ng-controller directive to be able to access the method
cant do the way u mentioned as my button is outside that div. have a look at my whole html part. jsfiddle.net/e3gu2hca
I am getting this error angular.js:15536 Error: [$controller:ctrlreg] The controller with the name 'dtAssignVendor' is not registered.
i checked your code and you have multiple ng-app directives in your template, you need to refactor your code and put the table and the button inside a common ancestor with the ng-app="grdContrl" and ng-controller="dtAssignVendor"
|

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.