多次选择后保持HTML下拉列表的过滤

时间:2018-09-13 12:58:30

标签: javascript php sql-server ajax post

我目前有5个单独的HTML下拉框。每当我选择一个时,它就会过滤其他四个。但是,在做出另一个选择之后,在过滤第二个选择之后的下拉列表时,它实质上会忽略第一个选择。我可以为每种选择可能性编写查询,但这显然不理想。是否有一种方法可以将所有内容组合到一个查询中,从而可以过滤其他下拉列表,而不管是否只有一个选择存在3-4个不同的选择?我只需要保持下拉列表在可能存在的所有不同选择之间相互过滤即可。

Index.php代码(实际上只是我的php文件中的javascript代码:

// ----- Function that sends values to index-ajax.php which filters the dropdowns -----
$(document).ready(function(){


    $("#collector, #date, #status, #repcode, #balancegroup").change(function(e){
    showUser(
      $("#collector").val()
      ,$("#date").val()
      ,$("#status").val()
      ,$("#repcode").val()
      ,$("#balancegroup").val()
    );
  });

    $("#collector").change(function(e){
        $.post('index-ajax.php',{filter:'Name',by:$(this).val()},function(data){
            $("#date .choice").hide();
            $("#status .choice").hide();
            $("#repcode .choice").hide();
            $("#balancegroup .choice").hide();
            $.each(data, function(key,row) {
                $("#date option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item ) != -1;
                }).show();
                $("#status option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item1 ) != -1;
                }).show();
                $("#repcode option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item2 ) != -1;
                }).show();
                $("#balancegroup option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item3 ) != -1;
                }).show();
            });
        },"JSON");
    });

    $("#date").change(function(e){
        $.post('index-ajax.php',{filter:'Date',by:$(this).val()},function(data){
            $("#collector .choice").hide();
            $("#status .choice").hide();
            $("#repcode .choice").hide();
            $("#balancegroup .choice").hide();
            $.each(data, function(key,row) {
                $("#collector option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item ) != -1;
                }).show();
                $("#status option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item1 ) != -1;
                }).show();
                $("#repcode option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item2 ) != -1;
                }).show();
                $("#balancegroup option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item3 ) != -1;
                }).show();
            });
        },"JSON");
    });

    $("#status").change(function(e){
        $.post('index-ajax.php',{filter:'Status',by:$(this).val()},function(data){
            $("#collector .choice").hide();
            $("#date .choice").hide();
            $("#repcode .choice").hide();
            $("#balancegroup .choice").hide();
            $.each(data, function(key,row) {
                $("#collector option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item ) != -1;
                }).show();
                $("#date option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item1 ) != -1;
                }).show();
                $("#repcode option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item2 ) != -1;
                }).show();
                $("#balancegroup option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item3 ) != -1;
                }).show();
            });
        },"JSON");
    });

    $("#repcode").change(function(e){
        $.post('index-ajax.php',{filter:'Report Code',by:$(this).val()},function(data){
            $("#collector .choice").hide();
            $("#date .choice").hide();
            $("#status .choice").hide();
            $("#balancegroup .choice").hide();
            $.each(data, function(key,row) {
                $("#collector option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item ) != -1;
                }).show();
                $("#date option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item1 ) != -1;
                }).show();
                $("#status option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item2 ) != -1;
                }).show();
                $("#balancegroup option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item3 ) != -1;
                }).show();
            });
        },"JSON");
    });

    $("#balancegroup").change(function(e){
        $.post('index-ajax.php',{filter:'Balance Group',by:$(this).val()},function(data){
            $("#collector .choice").hide();
            $("#date .choice").hide();
            $("#status .choice").hide();
            $("#repcode .choice").hide();
            $.each(data, function(key,row) {
                $("#collector option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item ) != -1;
                }).show();
                $("#date option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item1 ) != -1;
                }).show();
                $("#status option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item2 ) != -1;
                }).show();
                $("#repcode option").filter(function(i){
                    return $(this).attr("value").indexOf( row.item3 ) != -1;
                }).show();
            });
        },"JSON");
    });

});

Index-ajax.php代码:

<?php

$host="xxxxxx";
$dbName="xxxxxxxxx";
$dbUser="xxxxxxxx";
$dbPass="xxxx";
$dbh = new PDO("sqlsrv:Server=$host;Database=$dbName", $dbUser, $dbPass);
//$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );


if (isset($_POST['filter']) and isset($_POST['by'])) {

    $results = array();

    if (!empty($_POST['by'])) {
// -----------------------------------------------------
        if ($_POST['filter'] == 'Name') {
            $sql = "SELECT DISTINCT [Date Group - Filter] as item,
                   [Line Status] as item1,
                   [Parent Report Code] as item2,
                   [Balance Group - Filter] as item3
            FROM [Test_Spec_Bill]
            WHERE [Collector Name] = ? AND [Next Bill Date] = 'Yes'"; }
// -----------------------------------------------------
        if ($_POST['filter'] == 'Date') {
            $sql = "SELECT DISTINCT [Collector Name] as item,
                   [Line Status] as item1,
                   [Parent Report Code] as item2,
                   [Balance Group - Filter] as item3
            FROM [Test_Spec_Bill]
            WHERE [Date Group - Filter] = ? AND [Next Bill Date] = 'Yes'"; }
// -----------------------------------------------------
        if ($_POST['filter'] == 'Status') {
            $sql = "SELECT DISTINCT [Collector Name] as item,
                    [Date Group - Filter] as item1,
                    [Parent Report Code] as item2,
                    [Balance Group - Filter] as item3
            FROM [Test_Spec_Bill]
            WHERE [Line Status] = ? AND [Next Bill Date] = 'Yes'"; }
// -----------------------------------------------------
        if ($_POST['filter'] == 'Report Code') {
            $sql = "SELECT DISTINCT [Collector Name] as item,
                   [Date Group - Filter] as item1,
                   [Line Status] as item2,
                   [Balance Group - Filter] as item3
            FROM [Test_Spec_Bill]
            WHERE [Parent Report Code] = ? AND [Next Bill Date] = 'Yes'"; }
// -----------------------------------------------------
        if ($_POST['filter'] == 'Balance Group') {
            $sql = "SELECT DISTINCT [Collector Name] as item,
                   [Date Group - Filter] as item1,
                   [Line Status] as item2,
                   [Parent Report Code] as item3
            FROM [Test_Spec_Bill]
            WHERE [Balance Group - Filter] = ? AND [Next Bill Date] = 'Yes'"; }
// -----------------------------------------------------
        $stmt = $dbh->prepare($sql);
        $stmt->execute(array($_POST['by']));
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $results[] = $row; }
    }

    echo json_encode( $results );
    exit;
}

?>

0 个答案:

没有答案