0

Hi everybody I'm trying to convert a jquery app to Ionic. I used an array of json object to switch from a set of value to others on UI change event. This is part of the table I used

var table = {};
    table['0']  = {stipendio:716.73,base:531.52,ivmg:185.21,f025:7.21,f2650:14.42,f5170:21.63,f71:28.84};
    table['1']  = {stipendio:716.73,base:531.52,ivmg:185.21,f025:7.21,f2650:14.42,f5170:21.63,f71:28.84};
    table['2']  = {stipendio:899.44,base:638.39,ivmg:261.05,f025:8.08,f2650:16.16,f5170:24.24,f71:32.32};
...
...

and then I used a selector to choose which one of the row to use:

selected = $("#seniority").val()
paramsToUse = table[selected]; 

So I could use paramsToUse as a json object to get the value I needed.

How can I get the same result with Typescript?

7
  • 1
    I think your code is also valid typescript code as long as your have imported $. Commented Jul 29, 2017 at 14:53
  • what you mean importing $? Commented Jul 29, 2017 at 14:58
  • ` import * as $ from 'jquery';` Commented Jul 29, 2017 at 15:00
  • 1
    @aristotll the actual explanation is quite complicated and cannot be expressed in a comment. Basically the way typescript handles interop imports is fundamentally broken. They're planning to fix it but it will take a long long time Commented Jul 29, 2017 at 15:14
  • 2
    @aristotll see github.com/Microsoft/TypeScript/issues/16093 for more information Commented Jul 29, 2017 at 15:20

3 Answers 3

1

You can use Ionic's select to choose a table:

// On your .html file
<ion-list>
  <ion-item>
    <ion-label>Table</ion-label>
    <ion-select [(ngModel)]="tablePosition" (ngModelChange)="selectTable()">
      <ion-option value="0">First</ion-option>
      <ion-option value="1">Second</ion-option>
      <ion-option value="2">Third</ion-option>
    </ion-select>
  </ion-item> 
</ion-list>

// On your .ts file
private table: object[];
private paramsToUse: object;
private tablePosition: number;

private selectTable() {
  this.paramsToUse = table[this.tablePosition];
}

Assuming that your table could be an array of objects

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

4 Comments

It looks like an array, i.e. ['0'] and ['1'], etc (wonder why he's not using [val, val2, val3, ...]). Otherwise he could have an array of keys to use with the object and use that in combination with *ngFor. Though, can *ngFor iterate by object keys as well?
thank you guys, but the question is not on the component to use. I can't find the way to translate the javascript array of json I posted up here into Typescript.
Hey @NikRubblers , all Javascript is good typescript, so you should be fine. Another way to do it would be something like this: myArray = [ val1, val2, val3 ], or in your case table = [ {...}, {...}, {...} ]. In Ionic, if you used something like that array, you could then use *NgFor. If you want more on that I can write up an answer.
whatever sintax I use in Typescript it gives me errors. I need ad array of json {key:value,key:value,key:value,...} objects but I can't find the way to get it
0

In your comment you wanted to know how to create an array instead of using the table (sudo-array) you did in your answer, but I'll also show you how to easily extend it with NgFor.

Your table looks like this:

var table = {};
table['0']  = {stipendio:716.73,base:531.52,ivmg:185.21,f025:7.21,f2650:14.42,f5170:21.63,f71:28.84};
table['1']  = {stipendio:716.73,base:531.52,ivmg:185.21,f025:7.21,f2650:14.42,f5170:21.63,f71:28.84};
table['2']  = {stipendio:899.44,base:638.39,ivmg:261.05,f025:8.08,f2650:16.16,f5170:24.24,f71:32.32};

In Typescript, the more optimal version would look more akin to this:

// let is better than var because scoping issues
let table = [
  // You can put objects directly into arrays, like this. It automatically assigns indexes
  // so if you need to manually assign them it will be more complicated.
  {stipendio:716.73,base:531.52,ivmg:185.21,f025:7.21,f2650:14.42,f5170:21.63,f71:28.84},
  {stipendio:716.73,base:531.52,ivmg:185.21,f025:7.21,f2650:14.42,f5170:21.63,f71:28.84},
  {stipendio:899.44,base:638.39,ivmg:261.05,f025:8.08,f2650:16.16,f5170:24.24,f71:32.32},
  // ...etc
];

If you are using it on a Component, though, I would assume you'd use this.table instead of let table, but I don't know the rest of your code.

Later, in your code, you can use the wonderful NgFor to use your select (taken from @akz92 's answer)

<ion-list>
  <ion-item>
    <ion-label>Table</ion-label>
    <ion-select [(ngModel)]="tablePosition" (ngModelChange)="selectTable()">
      <ion-option *ngFor="let value of table; i = index" [value]="i">{{value.stipendio}}</ion-option>
    </ion-select>
  </ion-item> 
</ion-list>

Comments

0

Ok guys thank you everybody.

I've got it working using

table = [
{stipendio:716.73,base:531.52,ivmg:185.21,f025:7.21,f2650:14.42,f5170:21.63,f71:28.84},
{stipendio:716.73,base:531.52,ivmg:185.21,f025:7.21,f2650:14.42,f5170:21.63,f71:28.84},
{stipendio:716.73,base:531.52,ivmg:185.21,f025:7.21,f2650:14.42,f5170:21.63,f71:28.84}
]

much easier than I thought.

Thank again everybody

2 Comments

Just so you know, this is also standard in Javascript as well, not just Typescript :)
yes this is! the syntax I used before wasn't though! ;)

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.