首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Handsontable-in-handsontable,我如何只从一列返回值?

使用Handsontable-in-handsontable,我如何只从一列返回值?
EN

Stack Overflow用户
提问于 2015-10-23 04:29:22
回答 1查看 239关注 0票数 1

我尝试在Handsontable网格中使用"Handsontable“编辑器。我希望用户能够从“下拉”类型菜单中进行选择,但要显示相关数据的多列(例如,用户正在选择一个ID号,而我希望能够显示相关的姓名和年龄)。我让它正常工作,但用户可以在弹出编辑器中单击/any/ cell,它将返回该单元中的任何内容。在" ID“单元格上,它工作得很好,但当我只需要ID列中的值时,我可以很容易地返回一个"Name”或"Age“。

关于如何从一列强制返回值/only/ be,您有什么想法吗?

http://jsfiddle.net/sq9xs5a7/

代码语言:javascript
复制
document.addEventListener("DOMContentLoaded", function() {

  function getCarData() {
    return [
      ["Nissan", 2013, "black", "black"],
      ["Nissan", 2014, "blue", "blue"],
      ["Chrysler", 2015, "yellow", "black"],
      ["Volvo", 2016, "white", "gray"]
    ];
  }

  var
    carData = getCarData(),
    container = document.getElementById('example1'),
    manufacturerData,
    colors,
    color,
    colorData = [],
    hot;

  manufacturerData = [
    {name: 'BMW', country: 'Germany', owner: 'Bayerische Motoren Werke AG'},
    {name: 'Chrysler', country: 'USA', owner: 'Chrysler Group LLC'},
    {name: 'Nissan', country: 'Japan', owner: 'Nissan Motor Company Ltd'},
    {name: 'Suzuki', country: 'Japan', owner: 'Suzuki Motor Corporation'},
    {name: 'Toyota', country: 'Japan', owner: 'Toyota Motor Corporation'},
    {name: 'Volvo', country: 'Sweden', owner: 'Zhejiang Geely Holding Group'}
  ];
  colors = ['yellow', 'red', 'orange', 'green', 'blue', 'gray', 'black', 'white'];

  while (color = colors.shift()) {
    colorData.push([
      [color]
    ]);
  }

  hot = new Handsontable(container, {
    data: carData,
    colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color'],
    columns: [
      {
        type: 'handsontable',
        handsontable: {
          colHeaders: ['Marque', 'Country', 'Parent company'],
          data: manufacturerData
        }
      },
      {type: 'numeric'},
      {
        type: 'handsontable',
        handsontable: {
          colHeaders: false,
          data: colorData
        }
      },
      {
        type: 'handsontable',
        handsontable: {
          colHeaders: false,
          data: colorData
        }
      }
    ]
  });

  function bindDumpButton() {
      if (typeof Handsontable === "undefined") {
        return;
      }

      Handsontable.Dom.addEvent(document.body, 'click', function (e) {

        var element = e.target || e.srcElement;

        if (element.nodeName == "BUTTON" && element.name == 'dump') {
          var name = element.getAttribute('data-dump');
          var instance = element.getAttribute('data-instance');
          var hot = window[instance];
          console.log('data of ' + name, hot.getData());
        }
      });
    }
  bindDumpButton();

});

这是我正在做的一个很好的例子(摘自HoT帮助)。在这里,它就像我拥有的那样工作。当您点击“汽车”字段时,您可以选择“德国”,而我实际上只需要第一列中的值。

有什么想法?

EN

回答 1

Stack Overflow用户

发布于 2017-09-30 06:42:44

您可以将getValue()方法添加到热中,如下所示...

代码语言:javascript
复制
document.addEventListener("DOMContentLoaded", function() {

  function getCarData() {
    return [
      ["Nissan", 2013, "black", "black"],
      ["Nissan", 2014, "blue", "blue"],
      ["Chrysler", 2015, "yellow", "black"],
      ["Volvo", 2016, "white", "gray"]
    ];
  }

  var
    carData = getCarData(),
    container = document.getElementById('example1'),
    manufacturerData,
    colors,
    color,
    colorData = [],
    hot;

  manufacturerData = [
    {name: 'BMW', country: 'Germany', owner: 'Bayerische Motoren Werke AG'},
    {name: 'Chrysler', country: 'USA', owner: 'Chrysler Group LLC'},
    {name: 'Nissan', country: 'Japan', owner: 'Nissan Motor Company Ltd'},
    {name: 'Suzuki', country: 'Japan', owner: 'Suzuki Motor Corporation'},
    {name: 'Toyota', country: 'Japan', owner: 'Toyota Motor Corporation'},
    {name: 'Volvo', country: 'Sweden', owner: 'Zhejiang Geely Holding Group'}
  ];
  colors = ['yellow', 'red', 'orange', 'green', 'blue', 'gray', 'black', 'white'];

  while (color = colors.shift()) {
    colorData.push([
      [color]
    ]);
  }

  hot = new Handsontable(container, {
    data: carData,
    colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color'],
    columns: [
      {
    type: 'handsontable',
    handsontable: {
      colHeaders: ['Marque', 'Country', 'Parent company'],
      data: manufacturerData,
      getValue: function() {
        var selection = this.getSelected();

        // Get always manufacture name of clicked row
        return this.getSourceDataAtRow(selection[0]).name;
      }
    }
      },
      {type: 'numeric'},
      {
    type: 'handsontable',
    handsontable: {
      colHeaders: false,
      data: colorData
    }
      },
      {
    type: 'handsontable',
    handsontable: {
      colHeaders: false,
      data: colorData
    }
      }
    ],

  });

});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33290150

复制
相关文章

相似问题

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