如何获取 header 单元格的当前值? (包括嵌套 headers)
How to get current value of header cell? (incl. nested headers)
我有一个 handsontable,其中一些 header 是复选框,其中一些是 select 框。
如何访问 header 以检查 header 单元格内的 select/checkbox 的值?
JSFiddle(嵌套 headers - 与我的项目相同):http://jsfiddle.net/Lmh50uwu/
我想访问 mybox
和 myselect
的值
$("#mybox")
或$("#myselect")
不起作用。
我希望能够在 DOM 加载后随时检查 myselect
和 mybox
的值。
document.addEventListener("DOMContentLoaded", function() {
var example1 = document.getElementById('example1');
var hot = new Handsontable(example1, {
data: Handsontable.helper.createSpreadsheetData(5,10),
colHeaders: true,
rowHeaders: true,
nestedHeaders: [
['<input type="checkbox" id="mybox">', {label: 'B', colspan: 8}, 'C'],
['<select id="myselect" name="menu" class="selectpicker"><option value="LVW">LVW</option><option value="SIM">SIM</option></select>', {label: 'E', colspan: 4}, {label: 'F', colspan: 4}, 'G'],
['H', {label: 'I', colspan: 2}, {label: 'J', colspan: 2}, {label: 'K', colspan: 2}, {label: 'L', colspan: 2}, 'M'],
['N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W']
]
});
});
$("#mybox").remove();
console.log("test")
var elem = document.getElementById("myselect");
// console.log(elem.value); // elem is null here
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://docs.handsontable.com/pro/1.0.0/bower_components/handsontable-pro/dist/handsontable.full.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://docs.handsontable.com/pro/1.0.0/bower_components/handsontable-pro/dist/handsontable.full.min.css">
<div>
<div id="example1" class="hot handsontable htRowHeaders htColumnHeaders"></div>
</div>
编辑:更新:http://jsfiddle.net/Lmh50uwu/2/
编辑:将片段添加到我的 post,但无法测试,因为我的公司阻止访问它。
事实证明,行和列 headers 中的自定义 HTML 元素需要通过 class 名称而不是 ID 来标识。 这是因为行和列 headers 在 DOM 树中重复并且 id 属性必须是唯一的。
(见此 link:http://bollwyvl.github.io/jquery-handsontable/demo/renderers_html.html)
解法:
使用 <input type="checkbox" class="mybox">
和 getter $(".mybox")[1].checked
而不是
将<input type="checkbox" id="mybox">
与getter一起使用$("#mybox").checked
我有一个 handsontable,其中一些 header 是复选框,其中一些是 select 框。
如何访问 header 以检查 header 单元格内的 select/checkbox 的值?
JSFiddle(嵌套 headers - 与我的项目相同):http://jsfiddle.net/Lmh50uwu/
我想访问 mybox
和 myselect
$("#mybox")
或$("#myselect")
不起作用。
我希望能够在 DOM 加载后随时检查 myselect
和 mybox
的值。
document.addEventListener("DOMContentLoaded", function() {
var example1 = document.getElementById('example1');
var hot = new Handsontable(example1, {
data: Handsontable.helper.createSpreadsheetData(5,10),
colHeaders: true,
rowHeaders: true,
nestedHeaders: [
['<input type="checkbox" id="mybox">', {label: 'B', colspan: 8}, 'C'],
['<select id="myselect" name="menu" class="selectpicker"><option value="LVW">LVW</option><option value="SIM">SIM</option></select>', {label: 'E', colspan: 4}, {label: 'F', colspan: 4}, 'G'],
['H', {label: 'I', colspan: 2}, {label: 'J', colspan: 2}, {label: 'K', colspan: 2}, {label: 'L', colspan: 2}, 'M'],
['N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W']
]
});
});
$("#mybox").remove();
console.log("test")
var elem = document.getElementById("myselect");
// console.log(elem.value); // elem is null here
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://docs.handsontable.com/pro/1.0.0/bower_components/handsontable-pro/dist/handsontable.full.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://docs.handsontable.com/pro/1.0.0/bower_components/handsontable-pro/dist/handsontable.full.min.css">
<div>
<div id="example1" class="hot handsontable htRowHeaders htColumnHeaders"></div>
</div>
编辑:更新:http://jsfiddle.net/Lmh50uwu/2/
编辑:将片段添加到我的 post,但无法测试,因为我的公司阻止访问它。
事实证明,行和列 headers 中的自定义 HTML 元素需要通过 class 名称而不是 ID 来标识。 这是因为行和列 headers 在 DOM 树中重复并且 id 属性必须是唯一的。
(见此 link:http://bollwyvl.github.io/jquery-handsontable/demo/renderers_html.html)
解法:
使用 <input type="checkbox" class="mybox">
和 getter $(".mybox")[1].checked
而不是
将<input type="checkbox" id="mybox">
与getter一起使用$("#mybox").checked