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"}]"
thanks for the help brother!
– Gleyson Silva
Opa, good that helped, any doubt I’m available! Hug!
– Leandro Simões
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...
– KaduAmaral
Ah blz @Kaduamaral, I will edit and put now. Thanks for the tip!
– Leandro Simões
Ready @Kaduamaral .. thanks again for the tip.
– Leandro Simões