OpenWrt Forum Archive

Topic: Simple LuCI questions

The content of this topic has been archived on 4 May 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hello,

I'm trying to go through and figure out LuCI to add a custom page (or rather pages).

What I'd like to accomplish on one page would be to add a few images, make them clickable, and depending which image is clicked, reconfigure some UCI settings.

From what I understand, the developer HAS to choose to either use the CBI approach, or a template approach.  Is that correct?

The only way I can see how to do it would be to choose the template approach, add a onclick event to an html element, and have some LuCI code wrapped in Javascript to handle what I'd like done.

Is there an easier approach to this?

Also, this is one section of a page.  Further down the page, I'd like more configuration options, similar to what stuff that's on the existing LuCI configuration tab.  To add things like dropdown selections and such using the CBI approach is trivial.  To accomplish the same thing in html is a little more difficult.  Is there a way to embed CBI calls in an html template?  Is there a way to have a page that uses both an html template AND the CBI stuff? 

Thanks for any help.

Almost any cbi object has a .template property which you can set to a custom template file to override its rendering.

I wasn't aware of that.  This will make things much easier.

Thank you!

Hey jow,

I have another question that I figured would fit in better with this thread rather than starting a new one.  I'm having trouble setting up a callback in my controller that takes extra parameters that are strings.

In my controller, I have this entry:

    entry({"admin", "mypackage", "myfunction"}, call("action_myfunction", myparameter), nil).leaf = true

In my view, I have html buttons that are generated, and uniquely named.  When a user presses one, it calls a javascript function that looks as follows:

function buttonPressed(myparameter) {
    XHR.poll(3, '<%=luci.dispatcher.build_url("admin", "mypackage", "myfunction", myparameter)%>', null, function(x, y) {
        window.location.reload();
    });
    alert("Button was pressed" + myparameter);
};

And finally, the callback itself:

function action_myfunction(myparameter)
    luci.sys.exec("echo " .. myparameter .. " >> /root/log")
end

In execution, however, I'm getting the following error:

/usr/lib/lua/luci/dispatcher.lua:448: Failed to execute call dispatcher target for entry '/admin/mypackage/myfunction'.
The called action terminated with an exception:
/usr/lib/lua/luci/controller/admin/mypackage.lua:44: attempt to concatenate local 'myparameter' (a nil value)
stack traceback:
    [C]: in function 'assert'
    /usr/lib/lua/luci/dispatcher.lua:448: in function 'dispatch'
    /usr/lib/lua/luci/dispatcher.lua:195: in function </usr/lib/lua/luci/dispatcher.lua:194>

There are controllers that use callbacks with parameters that I can try to copy, but even if I get my package set up to do almost the same exact thing, I don't get my desired output.  The alert in the javascript code is fine, though, so I might be doing something wrong on the LuCI end.  Do you know what might be going on?

You're confusing JavaScript und Lua scopes.

XHR.poll(3, '<%=luci.dispatcher.build_url("admin", "mypackage", "myfunction", myparameter)%>', ...

should be

XHR.poll(3, '<%=luci.dispatcher.build_url("admin", "mypackage", "myfunction") %>/' + myparameter, ...

I'm also trying to put CBI on a template:
lua:

local h = require "luci.http"
luci.http.write("wifi.lua 2")
m = Map("wireless", translate("Wireless Interface"), "")
m.template = "admin_network/wireless_config"
s = m:section( TypedSection , "wifi-device", translate("Device Configuration"))
local channel = s:option( ListValue, "channel", translate("Channel"))
return m

admin_network/wireless_config (1 line of debbuging text):

wireless_config.htm 1

All I get in the browser is (strangely) the header , footer and the debugging text:
wifi.lua 2
wireless_config.htm 2

What more syntax do I need.

The discussion might have continued from here.