代码之家  ›  专栏  ›  技术社区  ›  Steven Kanberg

JavaScript:按其他下拉列表的值筛选下拉列表

  •  1
  • Steven Kanberg  · 技术社区  · 8 年前

    我需要一些专家的帮助。我使用的是Electron,下面显示了三个文件,首先从SQLite数据库(data.js)中提取数据,然后将数据传递到一组数组中,并在下拉列表中访问数据(Model.js和View.js)。我的问题是,我需要根据第一个下拉列表中选择的国家筛选第二个下拉列表(显示省份名称)。按照目前的情况,无论选择哪个国家,您都可以获得完整的国家列表和省份列表。

    我尝试了几个不同的选项,包括使用onchange事件侦听器(它不能正常工作……很可能是p.I.c.n.I.c.),以及尝试将元素值向后传递到SQL查询中(它也不能工作,因为值在移动过程中丢失了)。有人能建议一下正确执行此操作的正确方法吗?提前感谢您的帮助!

    function getCountries(done) {
    var sqlite3 = require('sqlite3').verbose();
    var file = 'db/locations.sqlite3';
    var db = new sqlite3.Database(file);
    var larray = [];
    
    db.all('SELECT * FROM Country', function(err, rows) {
        //  This code only gets called when the database returns with a response.
        rows.forEach(function(row) {
            larray.push({
                cname: row.CountryName,
                ccode: row.CountryCode
            });
        })
        return done(larray);
    });
    db.close();
    }
    
    function getProvinces(done) {
    var sqlite3 = require('sqlite3').verbose();
    var file = 'db/locations.sqlite3';
    var db = new sqlite3.Database(file);
    var stmt = 'SELECT Country.CountryId, Country.CountryName, Province.ProvinceName, Province.ProvinceCode FROM Province INNER JOIN Country ON Province.CountryId = Country.CountryId'
    var larray = [];
    
    db.all(stmt, function(err, rows) {
            //  This code only gets called when the database returns with a response.
            rows.forEach(function(row) {
            larray.push({
                cname: row.CountryName,
                pname: row.ProvinceName,
                pcode: row.ProvinceCode
            });
        })
        return done(larray);
    });
    db.close();
    

    型号.js:

    function Countries(done) {
      //Pull location values from data
    return getCountries(done);
    }
    
    function Provinces(done) {
    //Pull location values from data
    return getProvinces(done);
    }
    

    视图.js:

    function viewCountries() {
    
    var viewCou =  Countries(function(results) {
        // Code only gets triggered when Countries() calls return done(...); 
        var container = document.getElementById('country-select');
        var fragment = document.createDocumentFragment();
    
        results.forEach(function(loc, index) { 
            var opt = document.createElement('option');
            opt.textContent = loc.cname;
            opt.value = loc.ccode;
            fragment.appendChild(opt);
        });
        container.appendChild(fragment);
    })
    }
    
    function viewProvinces() {
    var viewPro = Provinces(function(results) {
    // Code only gets triggered when Provinces() calls return done(...); 
    var container = document.getElementById('province-select');
    var fragment = document.createDocumentFragment();
    
        results.forEach(function(loc, index) {
            var opt = document.createElement('option');
            opt.textContent = loc.pname;
            opt.value = loc.pcode;
            fragment.appendChild(opt);
            });
        container.appendChild(fragment);
    });
    }
    

    HTML格式:

    <p class="col-lg-2 col-md-4 col-sm-8 codes">COUNTRY</p>
    <p id="country-value" class="col-lg-2 col-md-2 col-sm-4 fieldval">No value entered</p>
    <select required id="country-select" class="col-lg-3 col-md-3 col-sm-8 formselect">
    <option selected> ----Select---- </option>
    </select>          
    <label class="col-lg-4 col-md-3 col-sm-8 descr">Country code of sample</label>
    <p class="col-lg-2 col-md-4 col-sm-8 codes">PROVINCE</p>
    <p id="province-value" class="col-lg-2 col-md-2 col-sm-4 fieldval">No value entered</p>
    <select required id="province-select" class="col-lg-3 col-md-3 col-sm-8 formselect">
    <option selected> ----Select---- </option>                
    </select>    
    <label class="col-lg-4 col-md-3 col-sm-8 descr">Provence code of sample</label>
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Community CDub    5 年前

    你走对了路。

    1. 首先,您应该继续使用 onchange 事件当你改变一个国家时,它会倾听。当这种情况发生时,意味着是时候改变这个省了 select 选择 以避免以前使用中的垃圾。

    <select id="country-select" onchange="onCountryChanged()"></select>

    function onCountryChanged() {
        resetProvincesSelect();
        viewProvinces($("#country-select").val());
    }
    
    function resetProvincesSelect() {
        var container = document.getElementById('province-select');
        $(container).empty();
    }
    
    1. viewProvinces 函数接受一个参数、国家代码或 ccode .filter() 这个 results ccode公司 ).
    function viewProvinces(ccode) {
        var viewPro = Provinces(function(results) {
            // Code only gets triggered when Provinces() calls return done(...); 
            var container = document.getElementById('province-select');
            var fragment = document.createDocumentFragment();
            results.filter(function(el) {
                return el.ccode === ccode;
            }).forEach(function(loc, index) {
                var opt = document.createElement('option');
                opt.textContent = loc.pname;
                opt.value = loc.pcode;
                fragment.appendChild(opt);
            });
            container.appendChild(fragment);
        });
    }
    
    1. 你需要包括 ccode公司 getProvinces()
    {
        ccode: row.CountryCode,
        cname: row.CountryName,
        pname: row.ProvinceName,
        pcode: row.ProvinceCode
    }
    
    1. 您尚未提供调用main函数的位置。所以,一般情况下都是这样的

    viewCountries();

    您可以选择预装所有省份以及国家。因此,当您选择国家时,不需要获取省份。这是一种特定的设计选择。你也可以 Provinces getProvinces 接收 ccode公司 ,将此步骤从视图中抽象出来。

    我希望它能帮助你。