如何在 handsontable 中动态添加复选框列
How to dynamically prepend a checkbox column in handsontable
我正在使用 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
},
....
但显然必须动态创建列数组,第一列复选框和所有其余文本(默认设置)如何修改数组以使其看起来像这样?
要获取第一列中的复选框,请执行以下操作:
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
配置的一部分。
可以这样尝试:
$(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?
我正在使用 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
},
....
但显然必须动态创建列数组,第一列复选框和所有其余文本(默认设置)如何修改数组以使其看起来像这样?
要获取第一列中的复选框,请执行以下操作:
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
配置的一部分。
可以这样尝试:
$(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?