[luci] How to call and pass arguments to lua function from a *template*

Nguyễn Hồng Quân ng.hong.quan at gmail.com
Thu Dec 12 09:16:35 CET 2013


Hi


2013/12/12 Le Tran Dat <trandatnh at gmail.com>

> Hi all,
>
> I wonder if I could call and pass arguments to a lua function which
> located in the firmware. What I am trying to archive is the same with
> clicking button delete in tblsection.htm template.
>
> Here is the onlick event of delete button in template tblsection.htm:
>
> onclick="<% luci.sys.call("env -i chilli_query logoff mac") %>
> this.form.cbi_state='del-section'; return true"
>

If you code like this, when the template is processed to generate web page
to browser, the code
luci.sys.call("env -i chilli_query logoff mac")
will be run at that time, and if it returns "AA:AA:BB:CC" for example, the
web page which your browser receives will be like this:

onclick="AA:AA:BB:CC
this.form.cbi_state='del-section'; return true"


>
> I observed I could run a lua command right before calling del-section.
> However, what I am trying to do is pass an argument into the lua
> command right before clicking delete button.
>
> Here is my situation, I have a table included:
> Mac Addr(DummyValue) - IP Addr(DummyValue) - Other Option(DummyValue) -
> <Delete>
>
> What I want to achieve is whenever I click the button <Delete> then I
> get the value of Mac Addr or IP Addr and pass it through a lua
> function to print it into the firmware(OpenWRT).
>
> Currently, I can be able to get the Mac Addr and IP Addr by using
> javascript within a template. However, I have no idea how to pass the
> Mac Addr to lua funtion.
>
> I hope I explained my case well so you can understand what I am talking
> about.
>

What you need to do is to create a new view, at a URL
/deladdress
for example

and your JS code, after collecting MAC Addr/IP Addr will send an Ajax
request to that URL,
/deladdress?mac=aa:bb:cc
for example
And you have to code the view for /deladdress so that it get the value of
"mac" variable, then build a Luci call
For example:
local mac = form['mac']
local command = "env -i chilli_query logoff " + mac
luci.sys.call(command)

Note:
The form['mac'] should be checked again. I don't remember how to get HTTP
GET/POST data in LuCI

-- 
***********************************************
* Nguyễn Hồng Quân                            *
* Y!M: ng_hquan_vn                            *
* Identi.ca: hongquan                         *
***********************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.subsignal.org/pipermail/luci/attachments/20131212/4339f258/attachment.html>


More information about the luci mailing list