0

Using the following JSON as an example and assuming I have set this data equal to $scope.people:

[
    {
        "personId": 1,
        "name": "Thomas",
        "age": 39
        "friends": [
            {
                "friendId": 1,
                "nickName": "Lefty"
            },
            {
                "friendId": 2,
                "nickName": "Morty"
            },
            {
                "friendId": 3,
                "nickName": "Gomer"
            }
        ]
    },
    {
        "personId": 2,
        "name": "George",
        "age": 27,
        "friends": [
            {
                "friendId": 1,
                "nickName": "Tommy"
            },
            {
                "friendId": 2,
                "nickName": "Bobby"
            },
            {
                "friendId": 3,
                "nickName": "Joe"
            }
        ]
    }
]

I'm dynamically creating buttons for each person.

<div class="form-group" data-ng-repeat="person in people">
    <button type="button" class="btn btn-default form-control"
        data-ng-click="friends(person.personId)">
            {{person.name}}
    </button>
</div>

What I'm trying to figure out is how to load the object data into Bootstrap panel (see HTML below) depending on the button clicked:

<div class="panel panel-primary" data-ng-hide="!friends">
    <div class="panel-heading">
        <h4 class="panel-title">{{person.name}}<span class="pull-right">{{person.age}}</span></h4>
    </div>
    <div class="panel-body">
        <table class="table table-striped">
            <tr data-ng-repeat="friend in person.friends">
                <td>{{$index + 1}}</td>
                <td>{{friend.nickName}}</td>
            </tr>
        </table>
    </div>
</div>

I was thinking that on my controller, I set a $scope variable = the friends array ($scope.friends), but I'm unsure how exactly to do that based on personId.

2 Answers 2

1

You need to create a $scope variable that holds the selectedPerson and assign the correct person to the selected person when clicking the button

$scope.SelectMe = function (p) {
        $scope.selectedPerson = p;
    }

You can then reference the selectedPerson when populating your panel

I have put together a quick fiddle for you. http://jsfiddle.net/cseignc/wt8w01b4/

Let me know if this is what you needed.

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

1 Comment

true but he wanted to do it using id doesn't he?
0

you can use a dictionary instead of array: use indexBy() function from underscore.js http://underscorejs.org/#indexBy

then you can do something like

$scope.peopleById = _.indexBy(people, "personId");

and in the html file

<tr data-ng-repeat="(id, friend) in person.friends">
<!-- here you can do peopleById[id] -->

Comments

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.