问题
I have been searching for quite some time, but I still haven't found a decent answer to my question. I'm trying to filter my results based on the value what the user will select from the selectbox. However selectbox is populated from the database and it's lenght varies. First select entry will be an empty one, so all of the matching values will be displayed. But when a different value has been selected from the selectbox all the non-matching itmes need to dissapear. Easyest way to do that is probably with Ajax but I can't get my head around it at the moment how to...
I'm developing on prestashop so the front-end side of the project is using smarty. So regular PHP ways just won't solve it.
So php function basicly takes the info of the related items in to the array and then pushes it to smarty for displaying. ( in a forach )
Like this:
{foreach from=$projectsArray key=id item=projectOpen}
{/foreach}
It's really easy to access all of the info from there and build the structure inside the array.
Here comes my current ajax code to execute a .php file while select box changes
Select box:
<select name="projects" id="projects" class="projects" size="{$project_options|@count eq 1}">
<option value="blank" selected="selected">All productsSelected</option>
{html_options values=$project_values output=$project_options}
</select>
Again select-box is specific to smarty and it just populates a list from database.
Ajax code:
<script type="text/javascript">
$(document).ready(function()
{
$("#projects").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: baseDir + 'modules/blockprojekt/ajax-product_view_all.php',
data: dataString,
cache: false,
success: function(html){
$("#rooms_view_container").html(html);}
});
});
});
</script>
And in ajax file i'm accesing it via
$_POST['id']
But what should I do there next? Is it easier to change the array values by some executed query and replace the whole content of the pervious query or can I just filter it somehow based on the selectbox value ?
BR's
回答1:
I actually tried to achieve my solution by going around the mountain.
Since forach items were connected to selectbox items in database, I was able to use simple jquery to do the trick and filter them.
Here is the code. maybe someone finds it useful in the future. This for obvious reasons goes inside forach loop.
<script type="text/javascript">
$(function(){
$('#projects').change(function() {
if($(this).val() == 'blank'){
$('.project_room_{$projectOpen.id_projekt_rooms}').hide();
$('.rooms_view_container').show();
}
else {
$('.project_room_{$projectOpen.id_projekt_rooms}').hide();
var selectVal = $('#projects :selected').val();
$('.project_room_'+selectVal).show(); }
});
});
</script>
BR's
来源:https://stackoverflow.com/questions/21385067/filter-results-based-on-the-select-box-selected-value