Javascript - Combination of variables - Filter

3

I need a tip. I'm not a programmer, but I like to take risks by automating processes, I'm sorry if the question is too basic.

I'm working with Google Maps API , and at some point I need to do a filter to target the contents of my InfoWindow .

I have a map with multiple filters, which apply to bookmarks and should extend to the contents of InfoWindow .

In this way, I'm taking the complete content from JSON ards and printing to InfoWindow , without segmenting the filters.

// Busca armario no segundo arquivo JSON
var as=$(ards).filter(function (i,n){
    return n.ARMARIO_ERB===marker.getTitle();
});

for (var i=0;i<as.length;i++)
{ .... }

I have a series of filters, which target my bookmarks, but I would like the contents of InfoWindow to show only the records for the filters.

//informação dos selects
var cluster = $( "#CLUSTER-select" ).val();
var cliente_recente = $('#cli_re-select').val();
var tipo_cli = $('#tipo_cli-select').val();
var tipo_reclama = $('#tipo_recla-select').val();

I would like a way to apply the filters at this point, testing if they are different than ZERO applies in. filter .

// Busca armario no segundo arquivo JSON
var as=$(ards).filter(function (i,n){
    return n.ARMARIO_ERB===marker.getTitle() && n.DESC_CLUSTER===cluster;
});

Go adding the && as the filters are being selected.

Is there any more practical way of doing this without having to create a bunch of% nested% by testing the combinations?

I can select only one filter, or two, three, and so on, are independent. Here's a sample of 4 only, I have around 10. How can I do this more efficiently?

----------------- Update ----------------

I came back from lunch with an idea, but it did not work right.

I've created the following structure:

var combina = [];
combina = [];

if(cluster !== '0') {
    combina.push('DESC_CLUSTER;' + cluster);
}
if(cliente_recente !== '0') {
    combina.push('CLIENTE_RECENTE;' + cliente_recente);
}
if(tipo_cli !== '0') {
    combina.push('TIPO_CLIENTE;' + tipo_cli);
}
if(tipo_reclama !== '0') {
    combina.push('TIPO_RECLAMACAO;' + tipo_reclama);
}

var filtro_add;
filtro_add = "1 = 1";

for(var i=0;i<combina.length;i++){
    var split;
    split = combina[i].split(';');
    filtro_add += " && n." + split[0] + " === " + '"' + split[1] + '"';
}

Result of IF : filtro_add

And I added this variable in my filter:

// Busca armario no segundo arquivo JSON
var as=$(ards).filter(function (i,n){
    return n.ARMARIO_ERB===marker.getTitle() && filtro_add;
});

But it did not work, it does not apply the values to the result. If I put the fields in hand it works. Example:

return n.ARMARIO_ERB===marker.getTitle() && n.DESC_CLUSTER === "CURITIBA";

How can I turn this 1 = 1 && n.DESC_CLUSTER === "CURITIBA" && n.TIPO_CLIENTE === "SOHO" ?

----------------- Update ----------------

After much research, I found an alternative, but with a poor performance and possible problems, the staff does not recommend much use. Through String

I added it to the search field.

The String looks like this: eval() : filtro_add

// Busca armario no segundo arquivo JSON
var as=$(ards).filter(function (i,n){
    return eval(filtro_add);
});

It works, but with a poor performance.

Is there any other alternative?

Thank you.

    
asked by anonymous 04.02.2016 / 15:05

1 answer

0

I was able to perform the filter.

Thinking more calmly, it seemed even obvious. Living and learning.

Here is an example of how I did it:

//informação dos selects
var cluster = $( "#CLUSTER-select" ).val();
var cliente_recente = $('#cli_re-select').val();
var tipo_cli = $('#tipo_cli-select').val();
var tipo_reclama = $('#tipo_recla-select').val();                   

// Busca armario no segundo arquivo JSON
var as=$(ards).filter(function (i,n){
var filtro_add=true;

if(cluster !== '0') {
    filtro_add = filtro_add && n.DESC_CLUSTER === cluster;
}
if(cliente_recente !== '0') {
    filtro_add = filtro_add && n.CLIENTE_RECENTE === cliente_recente;
}
if(tipo_cli !== '0') {
    filtro_add = filtro_add && n.TIPO_CLIENTE === tipo_cli;
}
if(tipo_reclama !== '0') {
    filtro_add = filtro_add && n.TIPO_RECLAMACAO === tipo_reclama;
}

    return n.ARMARIO_ERB===marker.getTitle() && filtro_add;

});
    
05.02.2016 / 19:59