【问题标题】:Select entire Handsontable column with icon click?单击图标选择整个 Handsontable 列?
【发布时间】:2013-10-31 15:33:35
【问题描述】:

我希望表格中的每一列都有一个图标,单击该图标将选择整个列。我有这个用于第一个(非固定)列的按钮,但无法让每个图标工作。另外,知道为什么最后一列(2018)的宽度更大并且水平滚动似乎永远不会到达终点吗?提前致谢。

jQuery

container.handsontable("loadData", getData());

$("button#selectFirst").on('click', function () {
    //container.handsontable("selectCell", 0, 4)
    container.handsontable("selectCell", 0, 4, 5, 4)
});

http://jsfiddle.net/D4Kx3/5/

【问题讨论】:

  • 可以选择索引为http://jsfiddle.net/D4Kx3/6/的行
  • @AbrahamUribe 再次感谢。你是个天才,帮助很大。
  • @AbrahamUribe 是否可以将图像放在表格内(而不是标题)?单元格类型是数字、日期、复选框等,但这些图像可以附加吗? jsfiddle.net/D4Kx3/7
  • 这将是选择一行,而不是选择列 - 就像在 Excel 中一样。
  • 您必须更改css以防止所选边框上的位移并在selectCellhttp://jsfiddle.net/D4Kx3/9/上添加false

标签: javascript jquery html click handsontable


【解决方案1】:

你需要像这样为箭头添加一个自定义渲染器

var myRendererArrow = function (instance, td, row, col, prop, value, cellProperties) {
    Handsontable.cellTypes.checkbox.renderer.apply(this, arguments);
    $(td).empty().append("<span class='sm-moon delBlue icon-right-arrow-17' data-icon='&#xe795;'>O</span>");
    return td;
};      

在 afterRender 回调中你需要添加这段代码

afterRender:function(){
    $('input[type=checkbox]').uniform(); 
    $('.checkall').on('click', function () {
        $(this).closest('table').find('input.htCheckboxRendererInput').prop("checked",this.checked);
        $.uniform.update();//update UniformJS
    });
//select clicked column
$(".icon-down-arrow-17").on("click",function(){
    var col=$(this).closest("th").index();
    container.handsontable("selectCell", 0, col, 5, col);
}); 
//select row only change th for tr and the column on selectCell
$(".icon-right-arrow-17").on("click",function(){
    var row=$(this).closest("tr").index();
    container.handsontable("selectCell", row, 0, row, 13,false);//false prevent scroll
});                  
}    

如果值已更改,则仅更改背景颜色,您可以在 afterChange 中使用更改对象

$('#example1').handsontable('getInstance').addHook('afterChange', function(changes) {
    var ele=this;
    $.each(changes, function (index, element) {
        if(element[2]!=element[3]){    //if the original value is not equal to the actual 
               $(ele.getCell(element[0],ele.propToCol(element[1])))
                    .addClass('changeInput').data("change",true);
        }
    });
});    

http://jsfiddle.net/D4Kx3/10/

【讨论】:

  • 我注意到一个小错误。选中复选框时,它们会变成黄色,就像输入一样。复选框应该是例外。另外,在选中复选框时,您可以选择整行吗?谢谢。
  • 啊,还有一件事。选中的行应保持选中状态,在检查新行时,仅选中该行。在这方面,检查标题中的所有复选框应该检查所有复选框并选择所有行。
  • 只有在选中复选框时才应选择该行。如果取消选中该复选框,则该行仍处于选中状态。
  • http://jsfiddle.net/D4Kx3/18/ 选择您需要实现类似这样的所有行并更改 selectCell 以选择多行
  • 上面的例子似乎没有实现选择多于一行,或者根据复选框选择/取消选择行。我看到的唯一变化是“全选”复选框缺少样式。
猜你喜欢
  • 2015-11-19
  • 1970-01-01
  • 2016-07-08
  • 2016-05-12
  • 2015-08-31
  • 2015-11-03
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
相关资源
最近更新 更多