doulu4233 2015-04-08 09:03
浏览 285
已采纳

如何使用golang的模板实现级联下拉菜单

Scenario:

I have a cascade scenario, where values in second dropdown depends upon first. I have three templates "layout", "input" and "inner".

Attempt:

I'm making ajax call on change of first dropdown in "input" template and stuck with handling the response returned. Currently I found a way to fix it by replacing html of second dropdown. But, I think this is not the better way to handle. I want something in line of rendering templates where I do not need to amend html.

please help in acheiving the task in better way or point to some wiki. Only Standard Library

Thanks,

Layout.html: http://play.golang.org/p/LikKy6rf3-

Input.html: http://play.golang.org/p/wM7EpPdXuM

Inner.html: http://play.golang.org/p/xFpInKZfFT

Main.go: http://play.golang.org/p/cxtJU-Lhi1

  • 写回答

1条回答 默认 最新

  • dongzou9047 2015-04-08 11:00
    关注

    On the higher level you have 2 options:

    • Either send all the values for the dropdown lists (e.g. as a tree) and change the values on the 2nd and 3rd level when the dropdown of a higher level changes (suitable for small lists, unsuitable for large dataset)
    • Or the one you chose: when selection changes, you make an AJAX call (triggered from onchange) and you populate the list from the results.

    Elaborating #2: populating list from the AJAX call result

    You also have 2 options to do this:

    • Either the AJAX call returns HTML call which you can simply use to replace the inner HTML of the HTML <select> tag.

    • Or the AJAX call may only return the data (e.g. encoded using JSON), and a Javascript code can build the content of the list.

    AJAX returning HTML

    The AJAX call may return the complete HTML code needed to be replaced as the inner HTML of the <select>. To achieve this at server side, you can create/separate the HTML template responsible only to generate the HTML code of this, for example:

    {{define "innerList"}}
        {{range .}}
            <option value="{{.Key}}">{{.Text}}</option>
        {{end}}
    {{end}}
    

    You can execute only this template like this:

    // tmpl is the collection of your templates
    values := ... // Load/construct the values
    tmpl.ExecuteTemplate(w, "innerList", values)
    

    Here values is a slice of the following structure:

    type Pair struct {
        Key string
        Text string
    }
    

    Building <select> content with Javascript

    The AJAX call may return a JSON datastructure, an array/list of value;text pairs from which you add the <option> child tags yourself.

    To add an <option> to a <select> tag:

    var x = document.getElementById("mySelect");
    var option = document.createElement("option");
    option.value = "1234";
    option.text = "Kiwi";
    x.add(option);
    

    So basically what you need to do is remove current children of the <select>, iterate over the list received as the response and add a new <option> tag constructed from each.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!