JQuery在链接选择后选择多个

Phi*_*ner 5 javascript jquery select chained

用户应在选择某些组后选择主机.我已经使用JQuery链接远程插件构建了一个链式选择,用于通过组选择主机.以下代码正在使用并正常工作:

$('#hosts').remoteChained({
    parents: "#hosts_group",
    url: "ajax/getHosts"
 });
Run Code Online (Sandbox Code Playgroud)
   <select id="hosts_group" name="hosts_group" class="form-control">
         <option value="">Bitte Gruppe selektieren</option>
         <option value="1>Some Groups</option>
   </select>

   <select id="hosts" name="hosts"></select>
Run Code Online (Sandbox Code Playgroud)

但最终结果应该为主机提供duallistbox,用户可以从任何组中选择主机.我尝试将多个标记添加到主机选择并通过以下代码段添加JQuery DuallistBox:

 $('#hosts').remoteChained({
    parents: "#hosts_group",
    url: "ajax/getHosts"
 }).DualListBox({json: false});
Run Code Online (Sandbox Code Playgroud)

duallist框显示正常,但在选择组时没有显示主机.

JSON数据如下所示:

[
    {'name': 'host1', 'id': '1'},
    {'name': 'host2', 'id': '2'}
]
Run Code Online (Sandbox Code Playgroud)

选择其他组时,json还包含不同的主机.链式选择插件通过以下请求请求数据:ajax/getHosts /?hosts_group = selectedId

只需使用带有正常多重选择的链式选择就可以正常工作.问题是在duallist框中显示json数据,每个选择都有所不同.

我试图构建一个JsFiddle示例,但它不起作用,因为外部库将不会被加载,我真的不明白我如何通过不同的选择手动提供json.

Jon*_*ers 0

好吧,我想我已经按照你想要的方式工作了。该问题源于以下事实:DualListBox 试图在remoteChained 完成填充选择框之前初始化自身。理想的解决方案是在 RemoteChained 完成时,初始化 DualListBox。不幸的是,remoteChained 似乎没有回调函数,这使得这有点棘手。这是我的(有点老套的)解决方案:

$(document).ready(function() {
    // setup remote chained
    $('#hosts').remoteChained({
        parents: "#hosts_group",
        url: "ajax/getHosts"
    });

    // set up dualList when change is triggered
    $('#hosts_group').on("change", function() {

        // if the option wasn't the first one
        if($(this).find("option:selected").val() != "") {
            setUpDualList();
        } else {
            // left as an exercise
            //putHostsSelectBoxBackIfItWasDestroyedByDualList();
        }
    });
});

function setUpDualList() {
    if(!hostsChanged()) {
        // because there's no callback, we have continually check if json is complete
        setTimeout(function() { setUpDualList(); }, 300);
    } else {
        // this actually does it.
        $("#hosts").DualListBox({json:false});
    }
}

function hostsChanged() {
    /*
    * this is VERY simplistic.  It will have to handle if #hosts was
    * 'changed' to have the same content, or if it was changed to 
    * something that has the same number of options, etc.  This ONLY
    * works for the very simple case you presented above.
    */
    return $("#hosts").find("option").length != 0;
}
Run Code Online (Sandbox Code Playgroud)

HTML 可以保持不变:

<select id="hosts_group" name="hosts_group" class="form-control">
    <option value="">Bitte Gruppe selektieren</option>
    <option value="1">Some Groups</option>
</select>

<select id="hosts" name="hosts"></select>
Run Code Online (Sandbox Code Playgroud)

但是,由于 DualListBox 具有破坏性,因此最好创建一个“hosts”选择和一个“hosts_destroyable”选择。可破坏的选择将简单地在ajax完成时复制“主机”,初始化DualList,并隐藏“主机”。当需要再次隐藏 DualList 时(因为用户更改了选择的组),则需要重新创建“hosts_destroyable”。

所以。上面是一个简短的、hacky 的解决方案,是的,这个可行,但它需要帮助。以下是一步一步的完整解决方案。

  • 初始化远程链接

  • 将 #hosts_group 更改为有效组时:

    1. $("#hosts").show();

    2. 监视#hosts 并确定remoteChained 何时成功完成json 请求。

    3. 请求完成后,将 #hosts 复制到临时 #hosts_temp

    4. $("#hosts_temp").DualListBox({json:false})

    5. $("#hosts").hide();

  • 将 #hosts_group 更改为无效组(例如“Bitte Gruppe selektieren”):

    1. 销毁 DualListBox(不确定创建了什么,如果存在的话以某种方式从 DOM 中删除)

    2. $("#hosts").show();