0

I have a data grid which is getting used across the application. I am getting currently selected rowid using the below code.

In HTML:

<tbody>
   <tr *ngFor="let ddata of tableData.data; let i = ddata" (click)="setClickedRow(ddata)" [class.active]="ddata.discountauthorizationid == selectedRow">
      <td *ngFor="let dr of tableData.record | paginate: { itemsPerPage: pageItem, currentPage: p }">{{ddata[dr]}}</td>
   </tr>
</tbody>

And in component.ts file I am doing this:

this.selectedRow = ddata.discountauthorizationid;

console.log("You selected!",ddata.discountauthorizationid);
this.dataService.changeMessage(ddata.discountauthorizationid);

Now I want to make this access completely dynamic and I have the primary id defined like this

@Input() primaryid: string

I want to access this data.(some_primaryid) just like we access the array using key and assign key using variable. Is it possible? If so how?

2
  • 1
    could you show us your data? how could we tell if data.(some_primaryid) would work on your data or not without any example. Your question is incomplete, give us some sample data and show us what type of result you want. Commented Jan 23, 2018 at 13:35
  • Sorry for lack of clarity but I have situation where the name of the element will differ with every the module. so it would be like discountid or accountid or userid and So on. I have already a variable called primaryid which is holding the name of the primaryid. so if I want to access data.accountid , instead of hard coding I want to use data.[primaryid] or something. Commented Jan 23, 2018 at 13:41

3 Answers 3

1

Here is how you can access the value.

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.primaryId = "userId";
    
    $scope.data = {
       "userId": 1,
       "accountId": 23,
       "testId": 5
    };

  
$scope.changeId = ()=>{
    
   $scope.primaryId = "accountId";
};
}
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="myApp">
<div ng-controller="MyCtrl">
   {{primaryId}} is :  {{data[primaryId]}}!
 <button ng-click="changeId()">
  Change ID
 </button>
</div>
</body>
</html>

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

2 Comments

Perfect one. I was looking like this. was Just testing it. Thanks for the confirming my solution. Your answer is precise clear and true :-)
I have added a button as well for you to show how to access it dynamically
0

After trying many things. I am able to do it like this. ddata[primary_id_var]. I tried ddata[{{primary_id_var}}] but it didn't worked.

Comments

0

Update, according to your comment:

class Data {
    public some_primary_id: string;
}

let id = 'some_primary_id'; // id would be your input parameter
let data = <Data>{ some_primary_id: '123' };

alert(data[id]); // 123

Previous Comment: I'm not sure if I get your question right but according to your last sentence:

want to access this data.(some_primaryid) just like we access the array using key and assign key using variable. Is it possible? if so How?

Yes it is possible, you can access properties like that:

console.log(ddata['some_primaryid']);
ddata['some_primaryid'] = '123';

1 Comment

Yes you got my point right but instead of static name 'some_primaryid' can I use ddata[primary_id_var] where primary_id_var holds name of the primary id .

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.