I have a lot of checkboxs like those :
The user will check one param of each checkbox to make the search.
How can I do that relative search informed by the user ? Isn't obligatted to select one of each checkbox.
My JS:
var checkeds = new Array();
$("input[name='marcar[]']:checked").each(function (){
checkeds.push( $(this).val());
});
var obj = $("#paramsPesquisa");
if($(obj).find("input[id='cd_seq_pedido']:checked").length > 0){
var cd_seq_pedido = $(obj).find("input[id='cd_seq_pedido']:checked").val();
}else{
var cd_seq_pedido = "";
}
My Ajax:
$.ajax({
type: "POST",
url: "/pedidoOnline/index.php/Pedidos/checkbox_marcados",
data: {
'marcar':checkeds,
'cd_seq_pedido': cd_seq_pedido,
'cd_pedido': cd_pedido
},
success: function(data){
console.log(data);
}
});
On my controller, how can I make this query ?
$pesquisa = $this->PesquisaPedOnline->find('all', array(
'fields' => array('cd_seq_pedido', 'cd_pedido', 'ds_cpl_tamanho_interno'),
'conditions' => array(?????)
));
If need more information, I'll put, sorry if I couldn't explain clearly.
When you build the conditions array in Cake you need to specify what field each of the checkbox values corresponds with. For example:
$conditions = array(
'ModelName.fieldname1' => 'value1', // a single checkbox value
'ModelName.fieldname2' => 'value2, value3, value4', // multiple checkbox values
// etc...
);
To be able to construct the $conditions
array like this you need to pass more information to the controller in your ajax call. I would suggest constructing a JSON object so you can build something like {'fieldname1':'value1, value2'}
.
Also you may want to consider how to remove items that the user unchecks.
Use the search plugin: https://github.com/cakedc/search
The name is a little misleading, it actually implements the PRG pattern and deals with the transformation of a POST request into search conditions.
This is a lot more clean than doing a huge amount of if-else checks on the POST data manually. Just take a look at the examples and tutorials, it's pretty straight forward.