Edit table at Angular 6

Asked

Viewed 1,137 times

0

I have had some problems, because I don’t know Angular 6 well. I have this table:

<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <table class="table table-striped table-bordered">
                <thead>
                    <tr>
                        <th>Código</th>
                        <th>Nome</th>
                    </tr>
                </thead>
                <tbody>
                    <tr *ngFor="let operator of dataSource">
                        <td>{{ operator.operatorId }}</td>
                        <td>{{ operator.name }}</td>
                    </tr>
                </tbody>
            </table>     
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-6">
        <button type="submit" class="btn btn-primary pull-right" >Atualizar</button>
    </div>
    <div class="col-md-6">
            <button type="submit" class="btn btn-primary" >Deletar</button> 
        </div>
</div>

I need to edit it, to be able to give an Update(Put or Patch). How do I? I would like to not use jquery, because I am inside Angular.

  • Hello Marcelo, you need to edit which way? fields? html structure? from what I understand you need help to create the Update function in angular, can give more information?

1 answer

2


Then, you can do the following, as you have ngFor, put a third and fourth column to use the chosen Operator, this way:

<tr *ngFor="let operator of dataSource">
  <td>{{ operator.operatorId }}</td>
  <td>{{ operator.name }}</td>
  <td><button ng-click="atualizar(operator)">Atualizar</button></td>
  <td><button ng-click="deletar(operator)">Deletar</button></td>
</tr>

In the respective column you inform the typescript method that you will call to do your update or delete, in the method you will have your http request calling put or delete. And as the buttons are inside your ng-for, you pass the chosen table Operator.

  • The question was in the Table Edition, because the user wants the editing and deletion to be done in the table/grid itself. Instead of the text, is there a way to put two images, like an "X" and a "Pencil"? But I understood what you posted.

  • Yes, I use a library of icons from this site: https://fontawesome.com/v4.7.0/icons/ just include his css, the site has Getstarted in the menu, there teaches you to put a link with their css reference, you can download the css of the icons. After imported into your html instead of text you will use the corresponding X icon or pencil. Ex: <button><i class="fa-times" Aria-Hidden="true"></i></button>

  • 1

    Gabriela, just one question: I created a printer in the App, called images and I put in two .png. files. Pencil and another delete. How do I put them in html instead of buttons.

  • If you are using a <img> it is only you to enclose it by a <a (click)="test"/><your img tag with your file . png></a>. This way the button click will be on the link, which in turn references an image inside it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.