Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have Angular ng-table where I load Json data to $data variable and display in the table.

function ngTable(){
var app = angular.module('main', ['ngTable']).
controller('DemoCtrl', function($scope, ngTableParams) {
    var data = [{name: "Moroni", age: 50},
                {name: "Tiancum", age: 43},
                {name: "Jacob", age: 27},
                {name: "Nephi", age: 29},
                {name: "Enos", age: 34},
                {name: "Tiancum", age: 43},
                {name: "Jacob", age: 27},
                {name: "Nephi", age: 29},
                {name: "Enos", age: 34},
                {name: "Tiancum", age: 43},
                {name: "Jacob", age: 27},
                {name: "Nephi", age: 29},
                {name: "Enos", age: 34},
                {name: "Tiancum", age: 43},
                {name: "Jacob", age: 27},
                {name: "Nephi", age: 29},
                {name: "Enos", age: 34}];

    $scope.tableParams = new ngTableParams({
        page: 1,            // show first page
        count: 10           // count per page
    }, {
        total: data.length, // length of data
        getData: function($defer, params) {
            $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
        }
    });
});
}

Additionally I have Java Script function returning some other Json data - function is called on button click.

function ReturnJson() {
 var json = []; 
 $('body').on('click','button#test', function(){
   var id = $(this).attr("value").val();
   json = id;
  });
 return json;
}

How do I replace var data content with ReturnJson() every time on button click action ?

share|improve this question
add comment

1 Answer

Simple assignment will work, but remember to reload the grid:

$scope.loadData = function () {
  $scope.data = $scope.ReturnJson();
  $scope.tableParams.reload();
}

$scope.ReturnJson = function () {
  var json = [{name: "bb", age: 200},{name: "aaa", age: 100}];
  return json;
}

Here is a working demo: http://plnkr.co/edit/Jw41uCmHGAfjkuoLIQor?p=preview

share|improve this answer
    
I'm not sure how to take advantage of your solution, because my java script function is called on Jquery onclick event and there is some ajax request inside ... But definitely +1 for the constructive response. –  Bill Watson Jun 26 at 7:32
    
Also my java script function is defined outside of the controller... –  Bill Watson Jun 26 at 7:39
    
I am suggesting that you use angular's ng-click to call the function. Angular has $http for ajax. It really is easier not to mix angular and jquery. –  j.wittwer Jun 26 at 13:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.