jLinq - Perform dynamic query picking values from 'Select'

1

I'm using jLinq to search for 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" );
    
asked by anonymous 24.06.2015 / 20:03

1 answer

1

You can mount the query as if it were text and then run through the eval() function. 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><selectid="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>

One note, the ID of your first select is spelled wrong. I was firt , I set it to first .

To execute the query, just do the following:

myNewArray = eval(query);
    
24.06.2015 / 20:36