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

Le Tran Dat trandatnh at gmail.com
Thu Dec 12 10:42:31 CET 2013


Hi Nguyễn Hồng Quân,

Thanks for your suggestion, It's a good solution. I have thought about
this solution before but it's quite complicated one for small issue.

However, I managed to use another trick that is passing the section id
into lua using a button.

Here is my .lua file and my trick.
m = Map("chillistatus", "CoovaChilli Users")

-- User's Configuration

clients = m:section(TypedSection, "online", translate("Online"),
translate("This list gives an overview over currently online users and
their status."))
clients.reset = false
clients.submit = false
clients.template = "cbi/tblsection"
clients.anonymous = true

mac = clients:option(DummyValue, "mac", translate("MAC Address"))
ip = clients:option(DummyValue, "ip", translate("IP Address"))

kick = clients:option(Button, "_kick", translate("Kick"))
kick.inputstyle = "reset"

function kick.write(self, section)
      -- I will have the section id and I can use it like an index.
      -- Whenever I click a button on a row (tblsection template) then
I will get the index of that row.
end

Whenever I click *kick* button on a row (I am using tblsection
template) then I will get the index of that row. I can use it like an
index to find the mac address in the same row.

Many thanks for your help!
Dat

On Thu, Dec 12, 2013 at 4:16 PM, Nguyễn Hồng Quân
<ng.hong.quan at gmail.com> wrote:
> 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                         *
> ***********************************************
>
> _______________________________________________
> luci mailing list
> luci at lists.subsignal.org
> https://lists.subsignal.org/mailman/listinfo/luci
>


More information about the luci mailing list