0

Hi I am using angularjs ui bootstrap accordion. Here i want to call when user click on accordion call one function and get user

Here is the code please help me thnks in advance

$scope.load_userdata= function(user_id) {
    	console.log(user_id);
    }
<uib-accordion close-others="oneAtATime">
		<div uib-accordion-group class="panel-default" ng-repeat="user in users">
		   <uib-accordion-heading ng-click="load_userdata(user.id)">
			    <div class="table">
			    	<div style="width: 50%">
			    			 @{{user.name}} 			         
			    	</div>
			    	<div>
				      Load user data
				     </div>
				   </div>
	      </uib-accordion-heading>
		</div>
</uib-accordion>

data and display it

1 Answer 1

1

I don't really understand what the problem is. I copied your template in the snippet below and added a simple controller with a working example.

The angular-bootstrap module is not included so you don't get the proper rendering. But it shows you how to define the function in your controller for your template to work.

function ctrl($scope) {
    // dummy users data for the snippet purpose
    $scope.users = [{
            name: 'Joe',
            id: '1', 
            data : 'Married to Jane',
        },
        {
            name: 'John',
            id: '2',
            data : 'Has 3 children',
        },
        {
            name: 'Jack',
            id: '3',
            data : 'Owns a boat',
        },
        {
            name: 'Bob',
            id: '4',
            data : 'Speaks swedish',
        }
    ];

    $scope.load_userdata = function(user_id) {
        console.log("Loading user#%s data", user_id);
        
        // looking for the right user : 
        let user = $scope.users.find(x => x.id === user_id);

        // and toggling the dataLoaded property in the user object
        user.dataLoaded = true;        
    }
}
<!DOCTYPE html>
<html ng-app>
   <head>
      <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.5/angular.min.js"></script>
      <meta charset=utf-8 />
      <title>Angular JS Demo</title>
   </head>
   <body ng-controller="ctrl">
      <uib-accordion close-others="oneAtATime">
         <div uib-accordion-group class="panel-default" ng-repeat="user in users">
            <uib-accordion-heading ng-click="load_userdata(user.id)">
               <div class="table">
                  <div style="width: 50%">
                     @{{user.name}} 			         
                  </div>
                  <div>
                     Load user data
                  </div>
               </div>
            </uib-accordion-heading>
            <div ng-show="user.dataLoaded">
               User's details : 
               {{user.data}} 			 
            </div>
          </div>
      </uib-accordion>

   </body>
</html>

Sign up to request clarification or add additional context in comments.

4 Comments

Surely the <div ng-show="dataLoaded"> should be inside the accordion, aka a <div> inside <div uib-accordion-group>
ehmm, sorry for being annoying, but it's not right now either. The <div ng-show="dataLoaded"> should be right after this tag </uib-accordion-heading> ... I don't think you'd want a user's data displayed in the accordion heading
@RogerC its what expect thank you, I think here is one issue if clicks in-side accordion<div>user data</div> it functions calls again. But i want only when clicks on header
@krishnaganisetti It should be fixed now that protozoid pointed it out. I moved the user's data div out of the header.

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.