angular-material-extensions / select-country

Angular Material component that allow users to select a country or nationality with an autocomplete feature
https://angular-material-extensions.github.io/select-country/
MIT License
126 stars 40 forks source link

<mat-select-country> inside a Angular Material Table #123

Closed CJiwan closed 1 year ago

CJiwan commented 1 year ago

HI, I try to shown the selected country in a Angular Material Table. In the HTML template of my componant I put :

<ng-container matColumnDef="country">
        <th mat-header-cell *matHeaderCellDef>Pays</th>
        <td mat-cell *matCellDef="let element"><mat-select-country [value]="element.country"></mat-select-country></td>
      </ng-container>

In the ts I read a list of customer from a db. With this list I create de datasource for the table :

this.customerList.forEach((customer, index)=>{
           let newCountry: Country ={
            alpha2Code: customer.country, alpha3Code: "", callingCode: "", name: "", numericCode: ""

          };
          tempList.push({
            position: index+1,
            name: customer.name,
            country: newCountry,
            language: customer.language.name
          });
          this.lastIndex = index+1;
 });
this.customersTable = tempList;

In my database, the country is the alpha2Code. The result is ; image

If I replace the [value]="element.country" by [value]="defaultValue" and defaultValue is :

  defaultCountry: Country = {
    alpha2Code: "BE", alpha3Code: "", callingCode: "", name: "", numericCode: ""

  }

I obtain the correct result image

But of course it is not what a need to do.

Thanks for your help

Rogermax commented 1 year ago

Hi,

Not sure if it's going to work, but you should send to the component an object similar to the defaultCountry, one way to achieve that could be:

<ng-container matColumnDef="country">
    <th mat-header-cell *matHeaderCellDef>Pays</th>
    <td mat-cell *matCellDef="let element">
        <mat-select-country [value]="{alpha2Code: element.country}"></mat-select-country>
    </td>
</ng-container>

Or:

<ng-container matColumnDef="country">
    <th mat-header-cell *matHeaderCellDef>Pays</th>
    <td mat-cell *matCellDef="let element">
        <mat-select-country [value]="{alpha2Code: element.country, alpha3Code: '', callingCode: '', name: '', numericCode: ''}"></mat-select-country>
    </td>
</ng-container>

Hope it helps you.

CJiwan commented 1 year ago

Hi, Unfortunately I get the same result. And no errors are generated in console which could have helped. Thanks Christian

CJiwan commented 1 year ago

Finaly that works. I prepare the entry of the datasource with :

let newEntry = {
        position: index+1,
        name: customer.name,
        country:{
          alpha2Code: customer.country, alpha3Code: "", callingCode: "", name: "", numericCode: ""
        },
        language: customer.language.name
};

in the HTML :

  <ng-container matColumnDef="country">
    <th mat-header-cell *matHeaderCellDef>Pays</th>
    <td mat-cell *matCellDef="let element"><mat-select-country [value]="element.country" [disabled]="true"></mat-select-country></td>
  </ng-container>

Thanks for your help