gpt4 book ai didi

javascript - 搜索在动态 Bootstrap 多选中不起作用

转载 作者:行者123 更新时间:2023-11-30 16:17:32 25 4
gpt4 key购买 nike

我正在使用 http://davidstutz.github.io/bootstrap-multiselect/用于在我的网页中创建 Bootstrap 多选控件。

下面的静态数据 fiddle 链接是我想使用动态数据实现的:

https://jsfiddle.net/DROCKS/mrmLrsad/4/

//please refer the fiddle

如果选择框的值是静态的,搜索选项可以正常工作,但如果它们是动态创建的,则会创建多选。但是,搜索过滤器在这种情况下不起作用。然而,我的代码类似于下面的 fiddle ,唯一的区别是搜索过滤器部分在我的实际代码中不起作用,但在这个 fiddle 中起作用。

fiddle 动态数据链接。 https://jsfiddle.net/DROCKS/mrmLrsad/5/

//please refer the fiddle

这是我的代码。HTML代码

<select id="lstFieldList" ></select>

jQuery 代码 [更新]:

function uncheckFields(field_id)
{
$('#' + field_id).multiselect('deselectAll', true);
}

//json_obj is the value from the fiddle.

createMultiSelectBox(json_obj);

//this function creates the multiselect
function createMultiSelectBox(json_obj)
{
var element_String = "";
var default_ele_set = 0;
var def_element_first = "1"; //if the first Y flag should be considered
var def_element_last = "n"; //if the last Y flag should be considered
var def_element = def_element_first; //change the value here to toggle between default selection
var tmp = [];

for (var key in json_obj)
{
var val = json_obj[key];
//alert("Key: " + key);

var chk_box_ctr = 0;
var element;

for(var child_key in val)
{
//alert("key: " + child_key + "\nvalue: " + val[child_key]);

var default_value = child_key.split(",")[7];

//alert("default_value: " + default_value);

if(!chk_box_ctr)
{
g_max_PageSize = child_key.split(",")[8];
//alert("g_max_PageSize: " + g_max_PageSize);
}

if(def_element == def_element_first)
{
if(default_value == "Y" && !default_ele_set)
{
//element_String += '<option value="' + child_key + '" selected="selected">' + val[child_key] + '</option>';
element = {"label": val[child_key],"value":child_key};
}
else
{
//element_String += '<option value="' + child_key + '">' + val[child_key] + '</option>';
element = {"label": val[child_key],"value":child_key};
}
}
else
{
if(default_value == "Y")
{
//element_String += '<option value="' + child_key + '" selected="selected">' + val[child_key] + '</option>';
element = {"label": val[child_key],"value":child_key};
}
else
{
//element_String += '<option value="' + child_key + '">' + val[child_key] + '</option>';

element = {"label": val[child_key],"value":child_key};
}
}
tmp.push(element);
}

if(def_element == def_element_last)
{
uncheckFields('lstFieldList');
}
}

//$('#lstFieldList').append(element_String);

//$('#lstFieldList').multiselect('rebuild');

$("#lstFieldList").multiselect('dataprovider', tmp);

$('#lstFieldList').multiselect({
maxHeight: 200,
buttonWidth:"100%",
enableFiltering:true,
enableCaseInsensitiveFiltering:true,
/*maxHeight: 200,
enableCaseInsensitiveFiltering: true,
//enableFiltering: true,
onChange: function(option, checked, select) {
alert('Changed option ' + $(option).val() + '.' + "\nchecked: " + checked + "\nselect: " + select);

if(checked)
{
//uncheckFields('lstFieldList');
}
else
{
uncheckFields('lstFieldList');
}
}*/
});

var elem = $('#lstFieldList').next();
elem.attr('class',(elem.attr('class') + ' open'));
}

可能是什么问题?因为两个文件中存在相同的代码。

如有任何帮助,我们将不胜感激。

提前致谢。

最佳答案

创建数组var tmp = [];

在循环查找选项时创建这样的 json

element = {"label": this.Name,"value":this.id};

并将每个元素推送到tmp

tmp.push(element);

并将这个tmp数组传递给多选

$("#lstFieldList").multiselect('dataprovider', tmp);

并将多选添加到下拉列表中

 $("#lstFieldList").multiselect({
maxHeight: 260,
buttonWidth:"100%",
enableFiltering:true,
enableCaseInsensitiveFiltering:true,
});

关于javascript - 搜索在动态 Bootstrap 多选中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35265104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com