简体   繁体   中英

How to Onload() set focus to jQuery Handsontable

What is the proper method to set the focus onload to first field in jQuery Handsontable ?

Here is the example link http://jsfiddle.net/4kvMq/4/

$(function () {
  $(window).load(function () {
    $('#example10grid table tbody tr td:first-child').focus();
  });
});

Even have tried to set focus manually but no luck!

$('#btnGo').click(function() {
    $(".dataTable table tbody tr td:first-child").focus();
});

Many, many thanks!

Thanks for your request. Now there is a public method to do that.

Use it on a button:

$('#btnGo').click(function(event) {
  setTimeout(function() {
      //timeout is needed because Handsontable normally deselects 
      //current cell when you click outside the table
      $("#example10grid").handsontable("selectCell", 0, 0);
  }, 10);
});

Or DOM ready:

$(function() {
  setTimeout(function() { 
      //timeout is needed because Handsontable normally deselects 
      //current cell when you click outside the table
      $("#example10grid").handsontable("selectCell", 0, 0);
  }, 10);
});

Live example: http://jsfiddle.net/warpech/4kvMq/35/

You can't do this. The problem is the way handsontable handles focus events. It binds a click handler to the hmtl element and detects every millisecond if your cursor is on the table. Therefore, you can't change the focus with a click() or focus() on a specific element.

Furthermore, since you can't change the cursor position with JavaScript for security reasons (and jQuery can't trigger click or focus events natively), you can't change the focus on a cell easily. It would only be possible to modify handsontable itself to change the selected table cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM