[英]Not able to pass ajax select options in Choices.js
I am using Choices.js ( https://github.com/jshjohnson/Choices ).我正在使用 Choices.js ( https://github.com/jshjohnson/Choices )。 I am also using Ajax to populate data into my select field dynamically from database.我还使用 Ajax 从数据库动态地将数据填充到我的 select 字段中。 The problem I am facing is when I use following code:我面临的问题是当我使用以下代码时:
const choices = new Choices('select',
{
loadingText: 'Loading...',
searchEnabled: false,
itemSelectText: '',
removeItemButton: true,
});
The dynamic select field stops working and data does not come from ajax script.动态 select 字段停止工作,数据并非来自 ajax 脚本。 Here the data comes dynamically from DB in the first select - course field.这里的数据动态来自第一个 select - course 字段中的 DB。 If I remove the above choices parameters, everything works fine.如果我删除上述选项参数,一切正常。 But As I add it, it breaks and remote ajax data does not populate.但是当我添加它时,它会中断并且不会填充远程 ajax 数据。
Any idea how to enable ajax data that comes from DB to show up in Choices.js知道如何使来自 DB 的 ajax 数据显示在 Choices.js 中
I see there is an option to add using setchoices: https://github.com/jshjohnson/Choices#setchoiceschoices-value-label-replacechoices我看到有一个使用 setchoices 添加的选项: https://github.com/jshjohnson/Choices#setchoiceschoices-value-label-replacechoices
I am not sure how to use those in my code.我不确定如何在我的代码中使用它们。 Any help will be appreciated!任何帮助将不胜感激!
Here's the html script这是 html 脚本
<div class="input-field">
<div class="input-select">
<select name="course_id" id="course_id" class="form-control input-lg dynamic" data-dependent="branch_id">
<option value="">Select Course</option>
@foreach($papers as $paper)
<option value="{{ $paper->course_id}}">{{ $paper->course_name }}</option>
@endforeach
</select>
</div>
</div>
<br>
<div class="input-field">
<div class="input-select">
<select name="branch_id" id="branch_id" class="form-control input-lg dynamic" data-dependent="sem_id">
<option value="">Select Branch</option>
</select>
</div>
</div>
<br>
<div class="input-field">
<div class="input-select">
<select name="sem_id" id="sem_id" class="form-control input-lg">
<option value="">Select Semester</option>
</select>
</div>
</div>
Here's the Ajax:这是 Ajax:
<script type="text/javascript">
$(document).ready(function(){
$('.dynamic').change(function(){
if($(this).val() != '')
{
var select = $(this).attr("id");
var value = $(this).val();
var dependent = $(this).data('dependent');
var _token = $('input[name="_token"]').val();
$.ajax({
url:"{{ route('dynamicdependent.fetch') }}",
method:"POST",
data:{select:select, value:value, _token:_token, dependent:dependent},
success:function(result)
{
$('#'+dependent).html(result);
}
})
}
});
$('#course').change(function(){
$('#branch').val('');
$('#sem').val('');
});
$('#branch').change(function(){
$('#sem').val('');
});
});
</script>
you can using settimeout你可以使用 settimeout
setTimeout(function () {
const choices = new Choices('[data-trigger]',
{
searchEnabled: false,
itemSelectText: '',
});
}, 500);
function loadCategoryProduct() {
$.ajax(
{
type: "POST",
url: "/Product/GetProductCategory",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: "true",
cache: "false",
success: function (data) {
var s = '<option placeholder="" value="0">Danh mục</option>';
for (var i = 0; i < data.length; i++) {
s += '<option value="' + data[i].ProductCategoriesID + '">' + data[i].ProductCategoriesName + '</option>';
}
$("#ddlProductCategory").html(s);
if (idCategoryProduct == "") {
$('#ddlProductCategory').val(0);
}
else {
$('#ddlProductCategory').val(idcountry);
}
setTimeout(function () {
const choices = new Choices('[data-trigger]',
{
searchEnabled: false,
itemSelectText: '',
});
}, 500);
},
Error: function (x, e) {
//alert("Some error");
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.