Luci-app-xxx, luci JavaScript, how to put a default value or MAC address in option box

I need to add my pre-defined data into the config app file, for example:

in the "First Option" box, how to fill a default data / hint data likes "12345678".

The source code is:

		m = new form.Map('example', _('Example Form'),
			_('Example Form Configuration.'));

		s = m.section(form.TypedSection, 'first', _('first section'));
		s.anonymous = true;

		o = s.option(form.Value, 'first_option', _('First Option'),
			_('Input for the first option'));
		o.write = '12345678';      <-----------not effective
		o.rmempty = false;
		o.editable = true;

pls help.

o.placeholder = '12345678';
1 Like

Thanks @jow , o.placeholder = '12345678'; is OK for a hint.
2 more questions:

  1. how to set it as an inputted value (default), if I no need to change the value, just use the default value. I have tried to use o.cfgvalue / o.value..., no lucky.

  2. how to use o.placeholder = getMAC(), I added require network.Device in front, but I got 404 error to call the API getMAC().
    Thanks.

NetworkError
HTTP error 404 while loading class file "/luci-static/resources/network/Device.js?v=git-24.073.29889-cd7e519"
  at compileClass (https://192.168.31.1/luci-static/resources/luci.js?v=git-24.073.29889-cd7e519:168:16)
  at async Promise.all (index 2)
o.value('blah');
o.default = 'blah';
  1. You need to 'require network' or so to import the functions.

Try o.default = '12345678' then. Also there is no singular getMAC() it's a method of the luci.network.Device class and on a given OpenWrt system there may be an arbitrary number of devices. Also the Device instance must be instantiated through a method returning a promise, so you either need to do that in the view's load() method or wrap the map building into a promise then() callback.

hi, @systemcrash @jow the o.default = '12345678' as expected, tks.

to get the MAC address string, 'require network' not work too. What the best way to get the device mac in luci js? use uci script?

An uci CLI cmd likes this:
/sbin/uci get network.wan.macaddr

Could I call the CLI to get the address, use which interface cmd?
Thanks.

This can get it, if it is saved. ( It's more complex if it recently changed in the GUI ).

	load: function() {
		return Promise.all([
			...,
			L.resolveDefault(uci.load('network')) // ensure network config is loaded
		]);
	},

...
	render: function (data) {
		...

		var getMAC = function() { //static function to use
			var mac = uci.get('network', 'wan', 'macaddr');
			return mac;
		};
		...

Thanks, let me try on it.

not work, I always got "null", or

my code:

'require uci';
'require network';

var getMAC = function() { //static function to use
	var mac = uci.get('network', 'wan', 'macaddr');
	return mac;
};

return view.extend({

	load: function() {
		return Promise.all([
			L.resolveDefault(uci.load('network')) // ensure network config is loaded
		]);
	},

	render: function() {
		var m, s, o;

		m = new form.Map('example', _('Example Form'),
			_('Example Form Configuration.'));

		s = m.section(form.TypedSection, 'first', _('first section'));
		s.anonymous = true;

		o = s.option(form.Value, 'first_option', _('First Option'),
			_('Input for the first option'));
			
		o.value(getMAC);
		
		o.editable = true;
		
		......

Well it needs to be o.value(getMAC()) since you want to pass the return value of calling getMAC() as argument to the o.value(…) call.

1 Like

o.value(getMAC()); still not able to capture the mac address:

Did you configure ACL access to network config?

I was wrong: no MAC address in the network/wan/macaddr, and no need
L.resolveDefault in the front. Finally, I got the content (IP),

the code:

'use strict';
'require view';
'require form';
'require uci';
'require network';

var getIP = function() { //static function to use

	var lanIP = uci.get('network', 'lan', 'ipaddr');
	
	return lanIP;
};

return view.extend({

	load: function() {
		return Promise.all([
			uci.load('network') // ensure network config is loaded
		]);
	},

	render: function() {
		var m, s, o;

		m = new form.Map('example', _('Example Form'),
			_('Example Form Configuration.'));

		s = m.section(form.TypedSection, 'first', _('first section'));
		s.anonymous = true;

		o = s.option(form.Value, 'first_option', _('First Option'),
			_('Input for the first option'));
			
		o.value(getIP());

		o.editable = true;
......

Now, I have to find another way to get the device MAC address, openwrt no macaddr contents in the network config file by default.

this CLI cmd got the macaddr I wanted:

cat /sys/class/ieee80211/phy0/macaddress

How to embedded the cmd into the js code? thanks.