Custom Widget - Call JS Function

Hello

I want to pass an array from a widget (HTML code) to a JS function, process it and return a value to the widget.

I created the JS function in an external file and stored it under conf/html. The script looks like this:

function SceneFunction(SceneArray, Scene) {
const index = SceneArray.findIndex(x => x.value === Scene);
return index;
}

This function should be called from the widget and the index should be returned to the widget.
How can I import/link this function in my widget?
And how do i call the function in the widget?

Thanks in advance
Carlo

I’m not an expert and someone will come along and tell me if I’m wrong, but I’m am almost certain you can’t do this. Widgets support Expressions (see Creating Personal Widgets | openHAB). It does not support all of JavaScript (by design).

Thanks for your quick response.
I’m still working with the “old” habpanel.
I was of the opinion that this should work. But it’s possible that I’m wrong.

I’ve done some tests now.
I can import an external script with oc-lazy-load=“[‘/static/myangular.js’]” and can call a function from this script. That works fine now.
The only thing I haven’t managed to do yet is pass a string to the function.
My function call looks like this:

input type=“button” style=“cursor:pointer” value=“Click Here” ng-click =“getArray()” />

getArray() is the function. But when I want to pass a string with it (getArray(‘TestString’), nothing happend.

The script looks like this:

(function() {

  'use strict';
  angular
  .module('app', [])
  .controller('ngclickCtrl', ['$scope', function($scope) {
	  
	$scope.myarray = '';
	$scope.getArray = function (myarray) {
	$scope.getIndex = $scope.myarray;

   }}]); 
})();

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.