CBI/view: more than one button per line in NamedSection

I'm trying to display more than one button per line in the CBI and based on the status/firewall I've created my own view file.

I'm struggling with passing the Lua variables into the view file tho. Especially I don't know how to manage the token.

Here are the links to the model/cbi, view template and the controller.

Like I said I'd like to pass down some Lua variables into the template and don't know how. I can duplicate the code like I've done for testing, but I'd like to avoid that. Also, even with the code duplication, how do I grab the token?

Any help would be appreciated.

@jow, @feckert since you seem to be most active with luci lately, maybe you could help me out?
Thanks!

The token is a global variable injected into each template scope, you do not have to pass it in.

My problem must be somewhere else then.
This is my template:

<div class="cbi-value"><label class="cbi-value-title">Service Control</label>
	<div class="cbi-value-field">
		<form method="post" name="simple-adblock-action" action="<%=url("admin/services/simple-adblock-action")%>">
			<%-
				local style
				if tmpfsStatus == "Stopped" then
					style = "cbi-button cbi-button-apply important"
				else
					style = "cbi-button cbi-button-apply disabled"
				end
			-%>
			<input type="submit" class="<%=style%>" name="start" value="<%:Start%>" />
			&nbsp;
			<%-
				local style
				if tmpfsStatus == "Stopped" then
					style = "cbi-button cbi-button-apply disabled"
				else
					style = "cbi-button cbi-button-apply important"
				end
			-%>
			<input type="submit" class="<%=style%>" name="dl" value="<%:Force Re-Download%>" />
			&nbsp;
			<%-
				local style
				if tmpfsStatus == "Stopped" then
					style = "cbi-button cbi-button-reset disabled"
				else
					style = "cbi-button cbi-button-reset important"
				end
			-%>
			<input type="submit" class="<%=style%>" name="stop" value="<%:Stop%>" />
			&nbsp;
			&nbsp;
			&nbsp;
			&nbsp;
			&nbsp;
			&nbsp;
			<%-
				local style
				if enabledFlag ~= "1" then
					style = "cbi-button cbi-button-apply important"
				else
					style = "cbi-button cbi-button-apply disabled"
				end
			-%>
			<input type="submit" class="<%=style%>" name="enable" value="<%:Enable%>" />
			&nbsp;
			<%-
				local style
				if enabledFlag ~= "1" then
					style = "cbi-button cbi-button-reset disabled"
				else
					style = "cbi-button cbi-button-reset important"
				end
			-%>
			<input type="submit" class="<%=style%>" name="disable" value="<%:Disable%>" />
			&nbsp;
		</form>
	</div>
</div>

And looks like the action_simple_adblock function in controller is not even being called:

module("luci.controller.simple-adblock", package.seeall)
function index()
	if not nixio.fs.access("/etc/config/simple-adblock") then
		return
	end
	entry({"admin", "services", "simple-adblock"}, cbi("simple-adblock"), _("Simple AdBlock"))
	entry({"admin", "services", "simple-adblock-action"}, post("action_simple_adblock")).leaf = true
end

function action_simple_adblock()
	luci.sys.exec("logger luci test")
	local packageName = "simple-adblock"
	if luci.http.formvalue("start") then
		luci.sys.init.start(packageName)
	elseif luci.http.formvalue("stop") then
		luci.sys.init.stop(packageName)
	elseif luci.http.formvalue("enable") then
		luci.sys.init.enable(packageName)
	elseif luci.http.formvalue("disable") then
		luci.sys.init.disable(packageName)
	elseif luci.http.formvalue("dl") then
		luci.util.exec("/etc/init.d/simple-adblock dl")
	end
	luci.http.redirect(luci.dispatcher.build_url("admin", "services", packageName))
end

I'm lost.

The problem is that you're nesting a <form> within another <form> - this most likely won't work.

Is there another approach I can take with buttons? I just need a bunch of buttons horizontally.

Make them dummy buttons and invoke your action via XHR:

<input type="button" value="<%:Stop%>" onclick="XHR.post('<%=url("admin/services/simple-adblock-action")%>', { token: '<%= token %>', stop: true }, function() { location.reload() })">

Thank you so much! Had to do (new XHR()). but otherwise it works!

I've tried to google, but failed -- how can I delay the re-rendering of the page? Some operations take time and I want to be able to either wait a few seconds or wait until whatever's called with XHR.post has completed.

look in the LuCI repo for "XHR.poll" events.

1 Like

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