首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Jexcel根据选择的另一个下拉列表更改下拉列表的值

使用Jexcel根据选择的另一个下拉列表更改下拉列表的值
EN

Stack Overflow用户
提问于 2020-05-07 21:24:26
回答 1查看 399关注 0票数 1

我正在使用Jexcel电子表格对一项调查中的一些数据进行分类。

在一个专栏中,我有一个汽车制造商的下拉列表作为选项(丰田、本田……)。

在另一篇专栏文章中,我有另一个关于车辆型号的下拉列表。

如何仅从第一个下拉列表中选择的制造商中过滤模型?

数据结构类似于:

代码语言:javascript
复制
var manufacturers = [
  { id: 1, name: 'Toyota' },
  { id: 2, name: 'Honda' }
];

var models = [
  { id: 8, manufacturer_id: 1, name: 'Corolla' },
  { id: 9, manufacturer_id: 2, name: 'Civic' }
];
EN

回答 1

Stack Overflow用户

发布于 2020-05-30 00:08:13

  1. 设置列源。
  2. 将“
  3. ”列的filter属性指向一个函数,该函数将用于过滤内容源,该函数使用id、manufacturer_id和source.filter()

来过滤源

Here's a Working Pen

代码语言:javascript
复制
var manufacturers = [
  { id: 1, name: "Toyota" },
  { id: 2, name: "Honda" }
];

var models = [
  { id: 8, manufacturer_id: 1, name: "Corolla" },
  { id: 9, manufacturer_id: 2, name: "Civic" },
  { id: 10, manufacturer_id: 2, name: "Accord" }
];

var options = {
  minDimensions: [2, 5],
  columns: [
    {
      type: "dropdown",
      title: "Manufacturer",
      width: "150",
      source: manufacturers
    },
    {
      type: "dropdown",
      title: "Model",
      width: "150",
      source: models,
      // this is where the magic happens :)
      filter: dropdownFilter
    }
  ],
};


var mySpreadsheet = $("#spreadsheet").jexcel(options);


function dropdownFilter(instance, cell, c, r, source) {
  //get the manufacturer_id from the previus column
  var manufacturer_id = instance.jexcel.getValueFromCoords(c - 1, r);
  
  if (manufacturer_id !== "") {
    // if a manufacturer is selected filter source using its id
    return source.filter(function (item) {
      if (item.manufacturer_id == manufacturer_id) return true;
    })
  } else {
    //if no manufacturer is selected return an empty array
    return [];
    // or uncomment the following line to return the full source
    // return source
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61658909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档