2

I'm loading the tree-view structure as shown below when the user clicks on the parent/child it spit on the console.log but i'm not sure how to pass the clicked parent/child node to apex controller

       $scope.my_tree_handler = function (branch) {
            console.log('you clicked on', branch);
            //how to pass the branch to controller ?????? 
        }

Screen shot:

enter image description here This is the code:

<script>
    var myApp = angular.module('myApp', ['treeGrid']);

    // controller
    myApp.controller('treeGridController', function ($scope, $timeout) {
        var tree;
        var rawTreeData = {!JSONData};
        var myTreeData = getTree(rawTreeData, 'Id', '{!relationshipFieldName}');

        $scope.tree_data = myTreeData;
        $scope.my_tree = tree = {};
        $scope.expanding_property = {
            field: "Name",
            displayName: "Name",
            sortable : true,
            filterable: true
        };
        $scope.col_defs = {!FieldsJSON};
        $scope.my_tree_handler = function (branch) {
            console.log('you clicked on', branch);
            //how to pass the branch to controller ?????? 
        }
        //..........
</script>

I have read that you can use apex:actionFunction but in my situation I need to set the values user have clicked.

EDIT: I have tried using javascript remoting function but getting the following error:

Visualforce Remoting Exception: Method 'mymethod' not found on controller RN_sObjectHierarchy. Check spelling, method exists, and/or method is RemoteAction annotated.

$scope.my_tree_handler = function (branch,scope) { 
                  Visualforce.remoting.Manager.invokeAction(
                   "RN_sObjectHierarchy.mymethod", branch.param,
                        function(result, event) {
                            if(result) {
                                scope.$apply(function(){
                                scope.result = result;

                                });
                                // something more to do?
                            } else {
                                console.log("There was an error");
                            } 
                        },
                   {escape: false} // No escaping, please
              );
        }

Controller:

@RemoteAction
public static void mymethod(String name){
    system.debug('///' +  name);
}
4
  • This might help get you started, I showed how to add a RemoteAction to your module a while back: salesforce.stackexchange.com/questions/112617/…
    – Adrian Larson
    Dec 5, 2016 at 21:13
  • Make sure the method is in same controller which is defined in visualforce page also it had proper annotation, return type etc. as per javascript specification
    – Ashwani
    Dec 5, 2016 at 23:01
  • yes the method is in the same controller, do you see anything wrong with the above code? that i posted in EDIT section
    – Nick
    Dec 5, 2016 at 23:03
  • if you want to give it a shot, here is the github github.com/RatanPaul/sObjectHierarchy
    – Nick
    Dec 5, 2016 at 23:06

2 Answers 2

2

You can use javascript remoting function inside the Angular function:-

$scope.my_tree_handler = function (branch,scope) { 
      Visualforce.remoting.Manager.invokeAction(
                   "ApexClassName.classMethod", branch.param,
                        function(result, event) {
                            if(result) {
                                scope.$apply(function(){
                                scope.result = result;

                                });
                                // something more to do?
                            } else {
                                console.log("There was an error");
                            } 
                        },
                   {escape: false} // No escaping, please
      );
}

Note the scope.$apply. This is how you set the value again. Remoting functions are asynchronous so results won't be reflected as soon as you click. It will be dependent on how soon you get the result.

You need to make sure that you always have $scope instance in function.

4
  • This approach won't work within a Static Resource, however. If you're developing a sophisticated Javascript application, it probably shouldn't be all shoved in one Visualforce Page. But that's just my opinion, man.
    – Adrian Larson
    Dec 5, 2016 at 21:28
  • It works! there is no expression binding. It depends on how good we structure/design javascript approach in application. @AdrianLarson
    – Ashwani
    Dec 5, 2016 at 21:30
  • @Ashwani: I have updated my question and I have tried but did not work, i pasted detail.
    – Nick
    Dec 5, 2016 at 22:59
  • 1
    @AdrianLarson Since the controller method argument is a regular string, you can just do window.classMethodFQN = '{!$RemoteAction.classMethod}' on your Visualforce page/component, then call it with Visualforce.remoting.Manager.invokeAction(classMethodFQN, ...) in a static resource. window can of course be replaced with scope or any other object. Dec 6, 2016 at 1:38
0

Here is how I able to get the selected row in the controller through JS remoting.

$scope.my_tree_handler = function (branch,scope) {         
            Visualforce.remoting.Manager.invokeAction('RN_sObjectHierarchy.mymethod', branch,
            function(result, event){
              //alert(result);
              //alert(event);
            },
            {escape: false}
            );
    }

Controller Class:

@RemoteAction
public static void mymethod(String branch){
    system.debug('///selectedRow:' +  branch);              
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .