Create table component with Knockout

Asked

Viewed 105 times

2

I have the following code using Ajax to fetch the data in a Restful and loading the fields in an array Listusers knockout:

  $.ajax({
    type: "GET",
    url: "http://192.168.15.4/api/usuarios",
    contentType: "application/javascript",
    dataType: "json",
    beforeSend: function () {
        $('#imageLoad').show();
        $('#imageError').hide();
    },
    success: function (result) {
        var observableData = ko.mapping.fromJS(result);
        var array = observableData();
        self.ListaUsuarios(array);

        // Method to filter the assets table based on user input.  Computed observable is throttled to ensure it doesn't kick in too quickly.
        self.filteredAssets = ko.computed(function () {
            debugger;
            var filter = self.filter();
            if (!filter) {
                return self.ListaUsuarios();
            } else {
                return ko.utils.arrayFilter(self.ListaUsuarios(), function (item) {
                    return item.login().indexOf(filter) > -1;
                });
            }
        }).extend({
            throttle: 500
        });
        self.selectedAsset = ko.observable();
        self.selectAsset = function (item) {
            self.selectedAsset(item);
        };
    })

Done this I load a table into the html page using the array Listusers previously loaded with the Knockout Mapping, specifying the fields I desire.

  <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th align=center width=60 style="display: none">Código</th>
                <th>Nome/Razão social</th>
                <th>Login</th>
                <th>CPF/CNPJ</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody data-bind="foreach: {data: ListaUsuarios()}">
            <tr>
                <td style="display: none" data-bind="text: id"></td>
                <td data-bind="text: pessoa.razao_social"></td>
                <td data-bind="text: login"></td>
                <td data-bind="text: pessoa.cpf_cnpj"></td>
                <td data-bind="text: email"></td>
            </tr>
        </tbody>
    </table>

That code works perfectly. But I wanted to create a template (component) with this table so that I didn’t have to repeat the code for each different table I had, for example the table would be only:

 <tbody data-bind="foreach: {data: ListaUsuarios()}">
      <tr>
         //aqui a table seria criada dinamicamente atraves do componente com template criado no knockout.
      </tr>
 </tbody>

The rest would be in charge of the component that loads the template, I tried to do this using some examples such as : http://jsfiddle.net/mbest/pfmk1k2e/ but without success

the received json is:

[{"pessoa":{"id":1,"tipo":"J","razao_social":"INTELIDER","nome_fantasia":"INTELIDER LTDA","cpf_cnpj":"10999558000186","rg_insc_estadual":"132456789"},"id":1,"login":"gleyson","senha":"123456","email":"[email protected]","ativo":"S"},{"pessoa":{"id":11,"tipo":"J","razao_social":"I9MAKER LTDA","nome_fantasia":"INTELIDER LTDA","cpf_cnpj":"00000000000000","rg_insc_estadual":"123456"},"id":11,"login":"sistemas","senha":"123456","email":"[email protected]","ativo":"S"}]"

1 answer

3


For some reason knockout components do not work inside a table other than through the use of "virtual Elements" Feature. So I made this jsFiddle to illustrate how your use would look. I used the JSON data you provided in the question.

I hope I’ve helped!

Edit: Added code in response

ko.components.register('user-component', {
    viewModel: function(params) {
    	  debugger;
        this.id = params.id;
        this.login = params.login;
        this.pessoa = params.pessoa;
        this.email = params.email;
    },
    template:
        '<td data-bind="text: id"></td>' +
        '<td data-bind="text: pessoa.razao_social"></td>' +
        '<td data-bind="text: login"></td>' +
        '<td data-bind="text: pessoa.cpf_cnpj"></td>' +
        '<td data-bind="text: email"></td>'
});

function Product(name, rating) {
    this.name = name;
    this.userRating = ko.observable(rating || null);
};

function MyViewModel() {
    this.products = ko.observableArray([
    	{
      	"pessoa":{
        	"id":1,
          "tipo":"J",
          "razao_social":
          "INTELIDER",
          "nome_fantasia":"INTELIDER LTDA",          
          "cpf_cnpj":"10999558000186",
          "rg_insc_estadual":"132456789"
        },
        "id":1,
        "login":"gleyson",
        "senha":"123456",
        "email":"[email protected]",
        "ativo":"S"
      },
      {
        "pessoa":{
        	"id":11,
          "tipo":"J",
          "razao_social":"I9MAKER LTDA",
          "nome_fantasia":"INTELIDER LTDA",
          "cpf_cnpj":"00000000000000",
          "rg_insc_estadual":"123456"
        },
        "id":11,
        "login":"sistemas",
        "senha":"123456",
        "email":"[email protected]",
        "ativo":"S"
        }
    ]);
}
 
ko.applyBindings(new MyViewModel());
table td, table th {
  border: 1px solid #CCC;
  padding: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<table>
<thead>
  <tr>
    <th>ID</th>
    <th>RAZÃO SOCIAL</th>
    <th>LOGIN</th>
    <th>CPF/CNPJ</th>
    <th>EMAIL</th>
  </tr>
</thead>
<tbody data-bind="foreach: products">
  <tr>
    <!-- ko component: { name:"user-component", params: $data } --><!-- /ko -->
  </tr>
</tbody>
</table>

  • thanks for the help brother!

  • Opa, good that helped, any doubt I’m available! Hug!

  • Leandro, it would be nice if you put your code here in Stackoverflow, so if the link breaks someday the answer still helps people. Still keep the jsFiddle link so people can edit...

  • Ah blz @Kaduamaral, I will edit and put now. Thanks for the tip!

  • Ready @Kaduamaral .. thanks again for the tip.

Browser other questions tagged

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