ForgeComponents
en

Components

j-AutoComplete

jComponent 20 · jcomponent@20 component Example loaded with jComponent 20
  • works with <input tags only
  • singleton
  • NEW: works with contenteditable attribute
var opt = {};

opt.element = 'YOUR_ELEMENT';

opt.search = function(query, render) {
	// `query` contains a value from the input
	// `render` is a function
	// Here we have to find a datasource and render argument helps with rendering HTML

	AJAX('GET /cities/', { q: query }, render);

	// or e.g.
	// render([{ name: 'Item 1', type: 'Pages' }, { name: 'Item 2', type: 'Widgets' }]);
};

opt.callback = function(value, input) {
	console.log('---> selected value:', value);
	// input.val('');
};
// opt.callback {Function}
// OR:
// opt.path {String} a path to variable (for result)

// opf.offsetWidth {Number} "width" offset
// opt.offsetX {Number} "x" offset
// opt.offsetX {Number} "y" offset
// opt.autoselect {Boolean} selects first suggestion automatically (default: false)

SETTER('autocomplete/show', opt);

Use it with <ui-component name="autocomplete">. When the component is not loaded yet, jComponent downloads it on first use from the address in DEF.fallback (the Componentator CDN by default). Author: Peter Širka. Source: the Componentator components repository, MIT license.

example.html
<ui-component name="autocomplete" config="height:200"></ui-component>

<div style="margin: 80px auto 0;width:300px;">
	<input type="text" style="width:100%;padding:5px 10px;border:1px solid #E0E0E0" placeholder="Type something ..." />
</div>

<script>

	var opt = {};
	opt.element = $('input');
	opt.offsetY = 17;
	opt.offsetWidth = 21;

	opt.search = function(q, render) {
		console.log('---> autocomplete search query:', q);
		// e.g. we have to call e.g. AJAX or another datasource
		render([{ name: 'Item 1', type: 'pages' }, { name: 'Item 2', type: 'widgets' }, { name: 'Item 3' }, { name: 'Item 4' }, { name: 'Item 5' }, { name: 'Item 6' }, { name: 'Item 7' }, { name: 'Item 8' }, { name: 'Item 9' }, { name: 'Item 10' }]);
	};

	opt.callback = function(value, input) {
		alert('autocomplete selected --> ' + JSON.stringify(value));
		input.val('');
	};

	if (jComponent.is20)
		SETTER('autocomplete/show @important', opt);
	else
		SETTER(true, 'autocomplete/show', opt);

</script>