StrugglingCoder
StrugglingCoder

Reputation: 5011

How to get the data of the clicked row in Angular JS

I have a few table rows and in the last column of the respective rows there are two operations that can be performed.

        <div class="embedded-list-table-wrapper embedded-medium migration-status-list-container">
            <table class="list-table embedded-list-table no-truncate-td">
                <thead>
                    <tr>
                        <th>
                            <a href="#list-table" class="table-sort table-sort-desc">
                                <span class="table-sort-text">Account Name</span>
                                <span class="table-sort-indicator"></span>
                            </a>
                        </th>
                        <th>
                            <a href="#list-table" class="table-sort table-sort-desc">
                                <span class="table-sort-text"># Batches Completed</span>
                            </a>
                        </th>

                        <th style="width:123px;">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="item in vm.items | limitTo : vm.pageSize : (vm.currentPage-1)*vm.pageSize">
                        <td class="table-link"><a  class="ng-binding" href="" target="_blank" ng-click="vm.setTenantId($event)">{{item.accountName}}</a></td>
                        <td class="table-text ng-binding">{{item.completedBatches}}</td>
                        <td class="table-input">
                            <div class="btn-group">
                                <div class="dropdown">
                                    <div class="cog dropdown-toggle" ng-click="batch.showSettings = !batch.showSettings"></div>
                                    <ul class="dropdown-menu visible" ng-show="batch.showSettings">
                                        <li><span class="dropdown-category">Manage</span></li>
                                        <li class=""><a href="" ng-click="vm.removeAccount(item.accountName)">Remove from Panel</a></li>
                                        <li class=""><a href="#" ng-click="vm.setEncoreLink()">View</a></li>
                                    </ul>
                                </div>
                            </div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>

Now this <a>" in View button will redirect me to a url that will look something like https://encore.rackspace.com/accounts/<tenant-id> . The <tenant-id> will come from first column Account Name value that we can split something like :

            vm.setEncoreLink = function($event){
                debugger;
                var tenant_id_text = $event.currentTarget.text;
                var tenant_id = tenant_id_text.substring(tenant_id_text.lastIndexOf("#")+1,tenant_id_text.lastIndexOf(")"));
                var encoreUrl = "https://encore.rackspace.com/accounts/"+tenant_id;
                $window.location.href=encoreUrl;
            } 

Provided the $event is passed from the first column value. How to access that and pass that so that tenant_id can be accessed ? Or is there any other way out ?

Upvotes: 0

Views: 75

Answers (1)

Paul
Paul

Reputation: 36319

Ok, so it looks to me like the text you're trying to derive from is actually '{{item.accountName}}', correct? Also you weren't actually explicit, but from your sample code I'm assuming you're trying to do the setEncoreLink function.

So given that you can just do this:

<li class=""><a href="#" ng-click="vm.setEncoreLink(item)">View</a></li>

And then your function can be

vm.setEncoreLink = function(item){
                debugger;
                var tenant_id_text = item.accountName;
                var tenant_id = tenant_id_text.substring(tenant_id_text.lastIndexOf("#")+1,tenant_id_text.lastIndexOf(")"));
                var encoreUrl = "https://encore.rackspace.com/accounts/"+tenant_id;
                $window.location.href=encoreUrl;
            } 

Upvotes: 2

Related Questions