根据选择框选择的值过滤结果 [英] Filter results based on the select box selected value

查看:93
本文介绍了根据选择框选择的值过滤结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了很长一段时间,但仍然没有找到对我的问题的正确答案.我正在尝试根据用户将从选择框中选择的值来过滤我的结果.但是,selectbox是从数据库填充的,它的长度各不相同. 首先选择的条目将是一个空条目,因此将显示所有匹配的值.但是,当从选择框中选择了一个不同的值时,所有不匹配的图标都将消失.最简单的方法可能是使用Ajax,但目前我无法解决...

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...

我正在prestashop上进行开发,因此该项目的前端使用smarty.因此常规的PHP方式将无法解决它.

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.

因此php函数基本上将相关项的信息放入数组中,然后将其推入显示状态. (大声疾呼)

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 )

像这样:

    {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.

这是我当前的ajax代码,用于在选择框更改时执行.php文件

Here comes my current ajax code to execute a .php file while select box changes

选择框:

  <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>  

再次选择框特定于smarty,它仅填充数据库中的列表.

Again select-box is specific to smarty and it just populates a list from database.

Ajax代码:

<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>

在ajax文件中,我通过

And in ajax file i'm accesing it via

    $_POST['id']

但是接下来我该怎么办?通过一些执行的查询来更改数组值并替换先前查询的全部内容是否更容易,还是我可以基于selectbox值以某种方式对其进行过滤?

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的

推荐答案

我实际上试图通过四处走动来实现自己的解决方案.

I actually tried to achieve my solution by going around the mountain.

由于forach项目已连接到数据库中的selectbox项目,因此我能够使用简单的jquery来完成技巧并过滤它们.

Since forach items were connected to selectbox items in database, I was able to use simple jquery to do the trick and filter them.

这是代码.也许有人觉得将来有用.出于明显的原因,这进入了forach循环.

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的

这篇关于根据选择框选择的值过滤结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆