Search code examples
handsontable

Pass additional parameters to handsontable autocomplete source function


I am implementing HandsOnTable autocomplete ajax as documented here: https://docs.handsontable.com/3.0.0/demo-autocomplete.html#strict-ajax

But I would like to pass additional parameter to the autocomplete source function, something like row.id below:

hot3 = new Handsontable(container3, {
    data: getCarData(),
    colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color'],
    columns: [
      {
        type: 'autocomplete',
        source: function (query, process, row.id) {
          $.ajax({
            //url: 'php/cars.php', // commented out because our website is hosted as a set of static pages
            url: 'scripts/json/autocomplete.json',
            dataType: 'json',
            data: {
              query: query
            },

From the documentation, source function only accepts two parameters (query and process), anyone knows how can I pass additional parameters?


Solution

  • You can't add an argument to source function. It does not "accept" parameters, it provides. This function is called internally by Handsontable with predefined arguments.

    But you can still access the data you are looking for. Check the context that source function is called with. It's a ColumnSettings object which contains (among others) column and row ids.

    Something like:

    columns: [
      {
        type: 'autocomplete',
        source: function (query, process) {
          var rowId = this.row;
          var columnId = this.col;
    
          $.ajax({
            ... // do whatever you want
          });
        }
      }
    ]