jLln - Make dynamic query by taking 'Select' values

Asked

Viewed 100 times

1

I’m using jLinno to search for in a Array of Objetos
I would like this search to be dynamic, taking the values of Selects

my example:

<select id="firt"><option>Select 1</option></select>
<select id="last"><option>Select 2</option></select>
<select id="phone"><option>Select 3</option></select>

myNewArray = jlinq.from( array ).sort( "date" )
             .equals( "first", $( "#first" ).find( "option:selected" )  
             .equals( "last",  $( "#last" ).find( "option:selected" )
             .equals( "phone", $( "#phone" ).find( "option:selected" )
             .group( "state" );

but I want something like this:

myNewArray = jlinq.from( array ).sort( "date" )
             //for each selected option
             .group( "state" );

1 answer

1


You can mount the query as if it were text and then run through the function eval(). Example:

function montaQuery() {
  var query = 'jlinq.from( array ).sort( "date" )';
  
  $('select').each(function(index) {
    query += '.equals( "' + this.id + '", "' + this.value + '")';
  });
  
  query += '.group( "state" );'
  
  $('#query').html(query);
  
  //myNewArray = eval(query);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="first"><option>Select 1</option></select>
<select id="last"><option>Select 2</option></select>
<select id="phone"><option>Select 3</option></select>

<br/>
<input type="button" value="Prepara a query!" onclick="montaQuery()"/>

<p id="query"></p>

An observation, the ID your first select is spelled wrong. Was firt, arranged for first.

To execute the query just do the command:

myNewArray = eval(query);
  • worked perfect! thank you very much!!

Browser other questions tagged

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