【问题标题】:JQuery select multiple after chained selectJQuery在链式选择后选择多个
【发布时间】:2015-09-09 07:13:03
【问题描述】:

用户应在选择某些组后选择主机。我已经使用JQuery chained remote Plugin 构建了一个链式选择,用于通过组选择主机。以下代码正在使用并且工作正常:

$('#hosts').remoteChained({
    parents: "#hosts_group",
    url: "ajax/getHosts"
 });
   <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>

但最终结果应该为主机提供一个双重列表框,用户可以在其中从任何组中选择主机。我尝试将多个标签添加到主机选择并通过以下 sn-p 添加JQuery DuallistBox

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

双列表框显示正常,但选择组时没有显示主机。

JSON 数据如下所示:

[
    {'name': 'host1', 'id': '1'},
    {'name': 'host2', 'id': '2'}
]

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

只需将链式选择与普通的多选一起使用即可。 问题是在双列表框中显示 json 数据,每个选择都不同。

我尝试构建一个 JsFiddle 示例,但它不起作用,因为不会加载外部库,而且我不太明白如何通过不同的选择手动提供 json。

【问题讨论】:

  • 不确定,但 URL"/ajax/getHosts" 中似乎缺少初始反斜杠
  • 那部分是正确的,将 ajax/getHosts 添加到当前 URL 时,我得到了 json 数据。只需正常选择,数据即可正常显示。它只是不显示双列表框中的数据。
  • 分享您从服务器获得的 JSON 响应。如果您将代码小提琴jsfiddle.net 与代码放在一起,将会非常有帮助。
  • 添加了 json 响应并更详细地解释了链式选择是如何工作的。

标签: javascript jquery select chained


【解决方案1】:

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

$(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;
}

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>

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

所以。以上是简短的,hacky,yes-this-works-but-it-needs-help 解决方案。下面是一步一步的完整解决方案。

  • 初始化 remoteChained

  • 将#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();

【讨论】:

    猜你喜欢
    • 2014-09-30
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    • 2011-02-18
    • 2010-09-21
    • 2018-12-13
    • 1970-01-01
    • 2013-06-09
    相关资源
    最近更新 更多