-3

How to print (based on array index) all array value?

At the moment, it print all the values.

<table>
    <thead>
       <tr>
           <td>Name</td>
           <td>Designation</td>
           <td>Total Credit Amount</td>
           <td>Total Debit Amount</td>
           <td>Related Companies</td>
       </tr>
    </thead>
    <tbody>
       <tr ng-repeat="data in relTransactions track by $index">
          <td>{{data.name}}</td>
          <td>{{data.designation}}</td>
          <td>{{data.total_credit_amount}}</td>
          <td>{{data.total_debit_amount}}</td>
          <td>
              <button ng-click="openViewMore()">View More</button>
          </td>
       </tr>
    </tbody>
</table>

I try to get array value based on id but i got all array value in that table how i fix that

7
  • 2
    What do you intend to do based on array index? Commented Mar 1, 2017 at 13:43
  • Add your js code. Commented Mar 1, 2017 at 13:45
  • 1
    Describe the problem. "It doesn't work" is not a problem statement. Tell us what the expected behavior should be. Put a brief summary of the problem in the title of your question. Commented Mar 1, 2017 at 13:51
  • @Develop Still unclear what you are asking. Commented Mar 1, 2017 at 14:04
  • let say suppose i have 4 view more button each have different response in array if i click 1st view more then i need to print 1st array respone only. now it print all array valu Commented Mar 1, 2017 at 14:06

2 Answers 2

1

As I understand from your comments, You want to show data of array only when Somebody clicks 'view more' of that array Item.

You can use ng-show based on $index. On clicking view more , change expanded index via functional call. I have written a small example have a look below.

Try on CodePen

  <html>

  <body ng-app>
        <h1>Click on view More below </h1>
    <div id="main" ng-app ng-controller="appController">
        <div ng-repeat="data in arr track by $index">
            <input type='button' value="View more" ng-click="showMore($index)"/> 
            <spna>item {{ $index }}</span>
            <span ng-show='expanded == $index'>{{data.name}}</span>
            <br><br>
            </div>
</div>

 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.min.js"></script>    
  </body>
</html>

In your controller:

function appController($scope){
    $scope.arr = [ {name:'one'},{name:'two'},{name:'three'}];

    $scope.showMore = function(i)
    {
        $scope.expanded = i;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

if you want access the array object inside the openViewMore then pass the index as parameter and access array like this.

<button ng-click="openViewMore($index)">View More</button> 
$scope.openViewMore = function(index) {
    console.log($scope.relTransactions[index])
}

OR you can pass the object as parameter to the function.

<button ng-click="openViewMore(data)">View More</button> 
$scope.openViewMore = function(data) {
    console.log(data)
}

4 Comments

it print all value in that
<table class="table table-striped table-responsive"> <tbody ng-repeat="(key, value) in relTransactions by index"> <tr ng-repeat="cList in value.relatedCompaniesList"> <td>{{cList.company_name}}</td> <td>{{cList.total_debit_amount}}</td> <td>{{cList.total_credit_amount}}</td> </tr> </tbody> </table>
Sorry this is my table
@Develop so when do you call openViewMore method inside table

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.