简体   繁体   English

与JSON数据源级联选择

[英]Cascading select with JSON data source

I'm trying to build a cascading selects based on data retrieved as JSON. 我正在尝试基于作为JSON检索的数据构建级联选择。

I'm trying to achieve the best performance and compatibility with all modern browser (IE11+). 我正在尝试与所有现代浏览器(IE11 +)取得最佳性能和兼容性。

The behaviour is: 该行为是:

  1. The user select a market 用户选择一个市场
  2. A new select with a list of relevant commodities to the selected market is created 创建一个新选择,其中包含与所选市场有关的商品列表
  3. The user select a commodity 用户选择商品
  4. A new select with a list of relevant price type to the selected commodity is created 创建具有与所选商品相关的价格类型列表的新选择

The JSON data is structured like this: JSON数据的结构如下:

[{
    "marketName": "def",
    "marketId": 124,
    "commodities": [
      {
        "commodityName": "Maize",
        "commodityId": 21,
        "priceTypes": [
          {
            "typeName": "Wholesale",
            "typeId": 16,
            "unitOfMeasures": [
              {
                "unitName": "MT",
                "unitId": 80
              }
            ]
          }
        ]
      }
   }]

I can also change the above structure if there is a more efficient way to represent it. 如果有一种更有效的表示方式,我也可以更改上述结构。

The js code I'm using is the following: 我正在使用的js代码如下:

var marketSelect=document.createElement('select');
marketSelect.addEventListener("change", function(){
    showCommodities(this.options[this.selectedIndex].value);
});

json.forEach(function(obj) {
   var opt = document.createElement("option");
   opt.value= obj.marketId;
   opt.innerHTML = obj.marketName;
   marketSelect.appendChild(opt);
});
document.getElementById('app').appendChild(marketSelect);

I'm stucked at point No.2 as I don't understand how to select only the commodities of the selected market 我被困在第二点,因为我不明白如何只选择所选市场的商品

here I've prepared a jsfiddle with full data example: https://jsfiddle.net/182dnzbL/1/ 在这里,我准备了一个带有完整数据示例的jsfiddle: https ://jsfiddle.net/182dnzbL/1/

I think I've solved this way: 我想我已经解决了这种方式:

var marketSelect=document.createElement('select');
marketSelect.addEventListener("change", function(){
    showCommodities(this.options[this.selectedIndex].value);
});

json.forEach(function(obj) {
   var opt = document.createElement("option");
   opt.value= obj.marketId;
   opt.innerHTML = obj.marketName;
   marketSelect.appendChild(opt);
});
document.getElementById('app').appendChild(marketSelect);


function showCommodities(marketId){
  var commoditySelect=document.createElement('select');
  commoditySelect.addEventListener("change", function(){
      //showPriceTypes...
  });
  var commodities = json.filter(e=>e.marketId==marketId)[0].commodities;
  commodities.forEach(function(obj) {
     var opt = document.createElement("option");
     opt.value= obj.commodityId;
     opt.innerHTML = obj.commodityName;
     commoditySelect.appendChild(opt);
  });
  document.getElementById('app').appendChild(commoditySelect);
}

so with this instruction I'm able to get only the data I need: var commodities = json.filter(e=>e.marketId==marketId)[0].commodities; 因此,通过此指令,我只能获取所需的数据:var goods = json.filter(e => e.marketId == marketId)[0] .commodities;

here is the fiddle: https://jsfiddle.net/182dnzbL/4/ 这是小提琴: https : //jsfiddle.net/182dnzbL/4/

 var json = [{ "marketName": "abc", "marketId": 123, "commodities": [{ "commodityName": "Maize", "commodityId": 21, "priceTypes": [{ "typeName": "Retail", "typeId": 15, "unitOfMeasures": [{ "unitName": "KG", "unitId": 73 }] }, { "typeName": "Wholesale", "typeId": 16, "unitOfMeasures": [{ "unitName": "MT", "unitId": 80 }] } ] }, { "commodityName": "Wheat", "commodityId": 22, "priceTypes": [{ "typeName": "Retail", "typeId": 15, "unitOfMeasures": [{ "unitName": "KG", "unitId": 73 }] }] }, { "commodityName": "Rice", "commodityId": 23, "priceTypes": [{ "typeName": "Retail", "typeId": 15, "unitOfMeasures": [{ "unitName": "KG", "unitId": 73 }] }, { "typeName": "Wholesale", "typeId": 16, "unitOfMeasures": [{ "unitName": "MT", "unitId": 80 }] } ] }, { "commodityName": "Milk", "commodityId": 24, "priceTypes": [{ "typeName": "Retail", "typeId": 15, "unitOfMeasures": [{ "unitName": "L", "unitId": 73 }] }, { "typeName": "Wholesale", "typeId": 16, "unitOfMeasures": [{ "unitName": "L", "unitId": 73 }, { "unitName": "10 L", "unitId": 74 } ] } ] } ] }, { "marketName": "def", "marketId": 124, "commodities": [{ "commodityName": "Maize", "commodityId": 21, "priceTypes": [{ "typeName": "Wholesale", "typeId": 16, "unitOfMeasures": [{ "unitName": "MT", "unitId": 80 }] }] }, { "commodityName": "Wheat", "commodityId": 22, "priceTypes": [{ "typeName": "Retail", "typeId": 15, "unitOfMeasures": [{ "unitName": "12.5 KG", "unitId": 73 }] }] }, { "commodityName": "Rice", "commodityId": 23, "priceTypes": [{ "typeName": "Retail", "typeId": 15, "unitOfMeasures": [{ "unitName": "KG", "unitId": 73 }] }, { "typeName": "Wholesale", "typeId": 16, "unitOfMeasures": [{ "unitName": "MT", "unitId": 80 }] } ] }, { "commodityName": "Oil", "commodityId": 26, "priceTypes": [{ "typeName": "Retail", "typeId": 15, "unitOfMeasures": [{ "unitName": "L", "unitId": 73 }] }, { "typeName": "Wholesale", "typeId": 16, "unitOfMeasures": [{ "unitName": "L", "unitId": 73 }, { "unitName": "15 L", "unitId": 77 } ] } ] } ] } ]; var marketSelect = document.createElement('select'); marketSelect.addEventListener("change", function() { showCommodities(this.options[this.selectedIndex].value); }); json.forEach(function(obj) { var opt = document.createElement("option"); opt.value = obj.marketId; opt.innerHTML = obj.marketName; marketSelect.appendChild(opt); }); document.getElementById('app').appendChild(marketSelect); function showCommodities(marketId) { var commoditySelect = document.createElement('select'); commoditySelect.addEventListener("change", function() { //showPriceTypes... }); var commodities = json.filter(e => e.marketId == marketId)[0].commodities; commodities.forEach(function(obj) { var opt = document.createElement("option"); opt.value = obj.commodityId; opt.innerHTML = obj.commodityName; commoditySelect.appendChild(opt); }); document.getElementById('app').appendChild(commoditySelect); } 
 <div id="app"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM