【问题标题】:How to dynamically prepend a checkbox column in handsontable如何在handsontable中动态添加复选框列
【发布时间】:2015-02-28 11:56:17
【问题描述】:

我正在使用handsontable 和一个jsfiddle http://jsfiddle.net/kc11/cb920ear/1/。我正在尝试在数据之前动态添加一个复选框列。其中有以下js结构,我假设是一个多维数组:

columns: [
  {
    data: "car"
    //1nd column is simple text, no special options here
  },
  {
    data: "year",
    type: 'numeric'
  },
  {
    data: "available",
    type: "checkbox"
  }
]

如果我理解正确以获取第一列中的复选框,我需要使这个对象数组看起来像:

columns: [
  {
   "checkboxes"
   type: "checkbox" 
  },
  {
   data: "car"
    //1nd column is simple text, no special options here
  },
....

但显然列数组必须动态创建,第一列复选框和所有剩余文本(这是默认值)我怎样才能修改数组看起来像这样?

【问题讨论】:

    标签: javascript jquery arrays checkbox handsontable


    【解决方案1】:

    可以这样尝试:

      $(document).ready(function () {
            
            function getCarData() {
                return [
                    {car: "Mercedes A 160", year: 2006, available: true, comesInBlack: 'yes'},
                    {car: "Citroen C4 Coupe", year: 2008, available: false, comesInBlack: 'yes'},
                    {car: "Audi A4 Avant", year: 2011, available: true, comesInBlack: 'no'},
                    {car: "Opel Astra", year: 2004, available: false, comesInBlack: 'yes'},
                    {car: "BMW 320i Coupe", year: 2011, available: false, comesInBlack: 'no'}
                ];
            }
            
            var keys = $.map(getCarData()[0], function(element,key) { return key; }); 
            keys.unshift("check");
            
            console.log(keys);
       	 var column=[
           	      {
             	        data: "car"
             	        //1nd column is simple text, no special options here
             	      },
             	      {
             	        data: "year",
             	        type: 'numeric'
             	      },
             	      {
             	        data: "available",
             	        type: "checkbox"
             	      }
             	    ];
       	 column.unshift({
       		   data:"available",
       		   type: "checkbox" 
       		  });
            var example1 = document.getElementById('example1');
            var hot1 = new Handsontable(example1,{
                data: getCarData(),
                startRows: 7,
                startCols: 4,
                colHeaders: keys,
                
                columnSorting: true,
                columns: column
            });
            
            function bindDumpButton() {
                
                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();
            
        });
    body {background: white; margin: 20px;}
    h2 {margin: 20px 0;}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    
    <script src="http://handsontable.com/dist/handsontable.full.js"></script>
    <script src="http://handsontable.com/lib/numeral.de-de.js"></script>
    <link rel="stylesheet" media="screen" href="http://handsontable.com/dist/handsontable.full.css">
    <link rel="stylesheet" media="screen" href="http://handsontable.com/demo/css/samples.css?20140331">
    <link rel="stylesheet" media="screen" href="http://handsontable.com/demo/css/samples.css?20140331">
    <link rel="stylesheet" media="screen" href="http://handsontable.com/demo/css/samples.css?20140331">
    <link rel="stylesheet" media="screen" href="http://handsontable.com/demo/css/samples.css?20140331">
    
    <h2>Checkbox cell type</h2>
    
    <p>If you have cells that contains only 2 possible values, you can use <code>checkbox</code> type.
      Data in such cells will be rendered as checkbox
      and can be easily changed by checking/unchecking the checkbox. </p>
    
    <p>Checking and unchecking can be performed using mouse or by pressing <kbd>SPACE</kbd>.
      You can change the state of multiple cells at once.
      Simply select cells you want to change and press <kbd>SPACE</kbd></p>
    
    <div class="handsontable" id="example1"></div>
    
    <p>
      <button name="dump" data-dump="#example1" data-instance="hot1" title="Prints current data source to Firebug/Chrome Dev Tools">
        Dump
        data to console
      </button>
    </p>

    另请参阅:How to create dynamic columns for Handsontable?

    【讨论】:

      【解决方案2】:

      要在第一列中获取复选框,请执行以下操作:

      columns: [
          {
              data: "available",  // mention appropriate data here
              type: "checkbox"    // checkboxes in the 1st column!
          },
          {
              data: "car"         // 2nd column
          },
          {
              data: "year",       // 3rd column
              type: 'numeric'
          },
          {
              data: "available",  // 4rd column
              type: "checkbox"
          }
      ]
      

      jsFiddle 演示: http://jsfiddle.net/cb920ear/2/

      并且,仅供参考:这是一个对象数组和Handsontable 的部分配置。

      【讨论】:

      • 我意识到我没有正确解释我的想法。我需要动态创建此列,请参阅编辑。
      猜你喜欢
      • 2013-04-30
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多