0

I searching and I found out that this code is what I need right now. But there is something wrong about it. When I clicked the alertme button it's alert is -1 which is wrong. When I click 1 Brick med it's alert -1 it should be 0 because the no and name is in sectionA. When I want t click 3 Frank Joemar Timbang it should alert 1 because he is in sectionB? Aney help? suggestions? TIA

var stud = angular.module("stud", []);
stud.controller("StudentsController", function ($scope) {
	'use strict';
  
  
  $scope.alertMe = function (studentGroup) {
    alert($scope.students.indexOf(studentGroup));
};
    $scope.sectionA = [
      {
        no:1,
        name:'Brick Med',
      },
      {
        no:2,
        name: 'Colin Christopher',
      },
    ];
      
     $scope.sectionB = [
      {
        no:3,
        name: 'Frank Joemar Timbang',
      },
      {
        no:4,
        name: 'Curtis Zaymond',
      }
      ];
    
    $scope.students = [
      $scope.sectionA,
      $scope.sectionB
    ];

    	

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html data-ng-app="stud">
<head lang="en">
	<meta charset="utf-8">
	<title>Students</title>
</head>
<body data-ng-controller="StudentsController" data-ng-init="init()">
  <div id="container">
   </div>
  <div class="container-table">
    <table border="1" width="100%">
        <thead>
            <tr>
                <td>Students</td>
                <td>Alert</td>
            </tr>
        </thead>
       <tbody ng-repeat="studentGroup in students">
    <tr ng-repeat="student in studentGroup">
        <td>{{student.no}} {{student.name}}</td>
        <td><button ng-click="alertMe($parent.$index)">Alert me!</button></td> 
    </tr>
</tbody>
</table>
</div>
</body>
</html>

2 Answers 2

2

Okay so your problem is that you are using indexOf() when you want to just be using studentGroup as the index.

$scope.alertMe = function (studentGroup) {
     alert($scope.students[studentGroup]); // returns object object in your array
     alert($scope.students[studentGroup][0]); /// returns sectionA object1
     alert($scope.students[studentGroup][0].name); /// returns sectionA object1 name
};

And I fixed up your html so it is easier to read but your original stuff should still work.

   <tbody ng-repeat="studentGroup in students" ng-init="studentsIndex = $index">
    <tr ng-repeat="student in studentGroup">
        <td>{{student.no}} {{student.name}}</td>
        <td><button ng-click="alertMe(studentsIndex)">Alert me!</button></td> 
    </tr>
    </tbody>

If this isn't what you want and you really want to alert the index, let me explain how indexOf works. That method will take a search parameter, and that is used to search an array for that element data. It is returning -1 right now because you give it the index and it searches for that index as the element data in index 0 and 1. Steps: Does whatever index = $scope.sectionA , Nope move on, Does whatever index = $scope.sectionB, nope move on. Done, did not find search parameter return -1.

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

2 Comments

Thank you for clarifying me about the how the index0f works. I run your code but it's alert me [object, object],[object, object].
If you look at my entire answer you will see I gave instructions how to get the name property out of those objects. You must apply those techniques to get the proper information you want from those objects.
1

The problem is with the indexOf. studentGroup is set correctly to 0 for the first 2 rows and 1 for the next 2. If you want the section array you should use

$scope.alertMe = function (studentGroup) {
    alert($scope.students[studentGroup]);
};

1 Comment

I tried your code but it's alert me [object, object],[object, object].

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.