jquery按其他人

时间:2017-12-08 18:54:07

标签: php jquery join filter

我在连接多个选择框时遇到问题,特别是在过滤器中 第一:我有一个结果joignin我想要的所有数据

UserID, UserName, CountryID, CountryName, SectionID, SectionName
  1,  User 01, 5, Germany, 10, Section 01

第二:我有三个选择框

<select name="users">UserID,UserName</select>
<select name="countries">CountryID,CountryName</select>
<select name="sections">SectionID,SectionName</select>

每个选择..我按不同的值填充数据

我想要的是,对于这个3选择框的每次更改,我想过滤选项的数据[例如excel的过滤器]

                        <div class="col-md-3 border-right">
                                <div id="partnersfilter" class="hide mbot15">
                                    <label for="partnersfilter"><?php echo 'Partners'; ?></label><br />
                                    <select class="selectpicker" id="partnersfilter" name="partnersfilter" data-width="100%" onchange="filters_report(this, 'partnersfilter'); return false;" data-none-selected-text="<?php echo 'Filter by one or multiple partners ...'; ?>" multiple data-live-search="true" data-actions-box="true">
                                        <?php                                         
                                        foreach ($filter_partners as $partner) { ?>
                                            <option value="<?php echo $partner['userid']; ?>">
                                                <?php echo $partner['company']; ?>
                                            </option>
                                        <?php } ?> 
                                    </select>
                                </div>
                        </div>

                        <div class="col-md-3 border-right">
                                <div id="countriesfilter" class="hide mbot15">
                                    <label for="countriesfilter"><?php echo 'Countries'; ?></label><br />
                                    <select class="selectpicker" id="countriesfilter" name="countriesfilter" data-width="100%" onchange="filters_report(this, 'countriesfilter'); return false;" data-none-selected-text="<?php echo 'Filter by one or multiple countries ...'; ?>" multiple data-live-search="true" data-actions-box="true">
                                        <?php foreach ($filter_country as $country) { ?>
                                            <option value="<?php echo $country['country']; ?>">
                                                <?php echo $country['short_name']; ?>
                                            </option>
                                        <?php } ?>
                                    </select>
                                </div>
                        </div>

                        <div class="col-md-3 border-right">
                                <div id="sectionsfilter" class="hide mbot15">
                                    <label for="sectionsfilter"><?php echo 'Sections'; ?></label><br />
                                    <select class="selectpicker" id="sectionsfilter" name="sectionsfilter" data-width="100%" onchange="filters_report(this, 'sectionsfilter'); return false;" data-none-selected-text="<?php echo 'Filter by one or multiple sections ...'; ?>" multiple data-live-search="true" data-actions-box="true">
                                        <?php foreach ($filter_sections as $section) { ?>
                                            <option value="<?php echo $section['sectionid']; ?>">
                                                <?php echo $section['section']; ?>
                                            </option>
                                        <?php } ?>
                                    </select>
                                </div>
                        </div>
JS函数

function filters_report(e, filter) {

    var data = {};

   data.partnersfilter = $('select[name="partnersfilter"]').val();
    data.countriesfilter = $('select[name="countriesfilter"]').val();
    data.sectionsfilter = $('select[name="sectionsfilter"]').val();
 
    var partnersfilter_area = $('select[name="partnersfilter"]');
    var countriesfilter_area = $('select[name="countriesfilter"]');
    var sectionsfilter_area = $('select[name="sectionsfilter"]');

    var selectbox = filter;
    
    $.post(admin_url + 'reports/filters_report', data).done(function(response) {

            response = JSON.parse(response);

            partnersfilter_area.empty();
            partnersfilter_area.selectpicker('refresh');

            countriesfilter_area.empty();
            countriesfilter_area.selectpicker('refresh');

            sectionsfilter_area.empty();
            sectionsfilter_area.selectpicker('refresh');
   
            $.each(response.result_partners, function(i, obj) {
               partnersfilter_area.append('<option value="'+obj.filterid+'" selected="">'+obj.filtername+'</option>');
               partnersfilter_area.selectpicker("refresh");
            });

            $.each(response.result_countries, function(i, obj) {
               countriesfilter_area.append('<option value="'+obj.filterid+'" selected="">'+obj.filtername+'</option>');
               countriesfilter_area.selectpicker("refresh");
            });

            $.each(response.result_sections, function(i, obj) {
               sectionsfilter_area.append('<option value="'+obj.sectionnumber+'" selected="">'+obj.sectionname+'</option>');
               sectionsfilter_area.selectpicker("refresh");
            });

        }, 'json');

        partnersfilter_area.selectpicker('refresh');
        countriesfilter_area.selectpicker('refresh');
        sectionsfilter_area.selectpicker('refresh');

    }

最后控制器fct

function filters_report(){

    $this->load->model('Api_model');

    $nps_partners_ids = $array_in = array();

    $partnersfilter = $this->input->post('partnersfilter');
    $countriesfilter = $this->input->post('countriesfilter');
    $sectionsfilter = $this->input->post('sectionsfilter');

    $query_sections = "SELECT r.sectionid as sectionnumber, r.section as sectionname FROM `tblresponses` r
        LEFT JOIN tblcontacts c ON c.id = r.contactid
        LEFT JOIN tblclients cl ON c.userid=cl.userid 
        LEFT JOIN tblcountries ct ON ct.country_id=cl.country 
    WHERE 1=1 ";


    $query_countries = "SELECT cl.country as filterid, ct.short_name as filtername FROM `tblresponses` r
        LEFT JOIN tblcontacts c ON c.id = r.contactid
        LEFT JOIN tblclients cl ON c.userid=cl.userid 
        LEFT JOIN tblcountries ct ON ct.country_id=cl.country 
   WHERE 1=1 ";


    $query_partners = "SELECT cl.userid as filterid, cl.company as filtername
    FROM `tblresponses` r
        LEFT JOIN tblcontacts c ON c.id = r.contactid
        LEFT JOIN tblclients cl ON c.userid=cl.userid 
        LEFT JOIN tblcountries ct ON ct.country_id=cl.country
    WHERE 1=1 ";

    if (is_array($countriesfilter) && !empty($countriesfilter)) {
        $query_partners .= " AND cl.country IN ('".implode('\',\'', $countriesfilter)."')";
    }

    if (is_array($partnersfilter) && !empty($partnersfilter)) {
        $query_countries .= " AND cl.userid IN ('".implode('\',\'', $partnersfilter)."')";
    }

    if (is_array($sectionsfilter) && !empty($sectionsfilter)) {
        $query_sections .= " AND r.sectionid IN ('".implode('\',\'', $sectionsfilter)."')";
    }

    $query_countries .= " GROUP BY cl.country ";
    $query_partners .= " GROUP BY cl.userid ";
    $query_sections .= " GROUP BY r.sectionid ";

    $result_partners = $this->db->query($query_partners)->result_array();
    $result_countries = $this->db->query($query_countries)->result_array();
    $result_sections =  $this->db->query($query_sections)->result_array();

    $data['result_countries'] = $result_countries;

    $data['result_partners'] = $result_partners;

    $data['result_sections'] = $result_sections;

    echo json_encode($data);

}

Thnx回复

0 个答案:

没有答案