Varvara Jones
Varvara Jones

Reputation: 791

Passing a row of data from a table to a modal with Angular

I have a table that displays entries for a contest (pulled from a DB with PHP and converted to a .json object to be used with AngularJS and JavaScript). I also would like to implement a modal on it, so when the "judge" clicks on each entry, they can see the details of that entry. So basically, I need to pass a single row of data to that modal (a ui.bootstrap modal).

Here's the markup for the table with ALL data. The modal is applied to the ng-repeated :

<table class="submissions">

<tr class="tb-header">
<td>id</td>
<td>wa #</td>
<td>name</td>
<td>email</td>
<td>file</td>
<td>rating</td>
<td>submitted on</td></tr>

    <tr ng-click="open()" ng-repeat="row in rows track by $index">

        <td>{{ row.id }}</td>
        <td class="wa-num">{{ row.wa_num }}</td>
        <td>{{ row.name }}</td>
        <td>{{ row.email }}</td>
        <td id="submitted-file">{{ row.file }}</td>
        <td>{{ row.rating }}</td>
        <td>{{ row.submitted }}</td>

    </tr>


</table>

And here's the controller that controls that entire page AND the modal:

.controller('dashboard',['$scope', '$rootScope', '$location', '$modal', 'loginService', 'getEntries',
          function($scope, $rootScope, $location, $modal, loginService, getEntries){

          $scope.open = function () {

              var modalInstance = $modal.open({
                  templateUrl: '/partials/submission_mod.html',
                  controller: ['$scope', '$modalInstance', function($scope, $modalInstance){
                      $scope.modalInstance = $modalInstance;

                      $scope.cats = "Submission info goes here.";
                  }]
              });
          };

          var entries = getEntries.entries();

              entries.save(
                  function(result){
                      console.log(result);

                      //$scope.rows = [];
                      $scope.rows = result.entries;
                      console.log($scope.rows);


                  },
                  function(result) {
                      console.log(result);
                  }

              );
    }])

And here's the modal's markup (which is not pulling in anything at the moment for some reason, not even hardcoded "cats"):

<div class="modal-entry">{{ cats }}</div>
<button class="btn btn-primary btn-modal" ng-click="modalInstance.close()">Close</button>

Question being: how can I pass the data to that modal? How to target it so that it only pulls the row that's clicked on, etc?

Any guidance is highly appreciated.

Upvotes: 0

Views: 5791

Answers (2)

rcruz
rcruz

Reputation: 837

The best way is to pass the row as a parameter to your modal function.

<tr ng-click="open(row)" ng-repeat="row in rows track by $index">
  ...
</tr>

And in your funcition, recive the row:

$scope.openModal = function (row) {
    var modalInstance = $modal.open({
        templateUrl: '/partials/submission_mod.html',
        controller: 'ModalCtrl',
        resolve: {
            cat: function () {
                return row;
            }
        }
    });
};

And then pass to your controller:

.controller('ModalCtrl', function ($scope, $modalInstance, cat) {
    $scope.cat = cat;
});

Upvotes: 1

oliveromahony
oliveromahony

Reputation: 471

plinkr has some information on how to do that from the Angular Bootstrap Directives Docs

Something like this with resolve:

var modalInstance = $modal.open({
  templateUrl: 'myModalContent.html',
  controller: 'ModalInstanceCtrl',
  size: size,
  resolve: {
    items: function () {
      return $scope.items;
    }
  }
});

Upvotes: 1

Related Questions