[英]Set 'selected' value for Select2 control with filter
I have a select2 control on a form, the control is populated by an Ajax call triggered by the user typing a value into the filter. 我在窗体上有一个select2控件,该控件由用户在过滤器中输入值触发的Ajax调用填充。 This works great for any new entry on the form. 这适用于表单上的任何新条目。 The problem I have is when an entry is edited, the same form is used however I cannot select the default value as the form does not have any options by default. 我的问题是编辑条目时,使用了相同的表单,但是由于表单默认情况下没有任何选项,因此我无法选择默认值。
Here's the form's markup: 这是表单的标记:
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-shipping-country"><?php echo $entry_country; ?></label>
<div class="col-sm-10">
<select name="country_id" id="input-shipping-country" class="form-control">
<option value=""><?php echo $text_select; ?></option>
<?php foreach ($countries as $country) { ?>
<?php if ($country['country_id'] == $shipping_country_id) { ?>
<option value="<?php echo $country['country_id']; ?>" selected="selected"><?php echo $country['name']; ?></option>
<?php } else { ?>
<option value="<?php echo $country['country_id']; ?>"><?php echo $country['name']; ?></option>
<?php } ?>
<?php } ?>
</select>
</div>
</div>
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-shipping-zone"><?php echo $entry_zone; ?></label>
<div class="col-sm-10">
<select name="zone_id" id="input-shipping-zone" class="form-control">
</select>
</div>
</div>
Below is the jQuery that I've written: 以下是我编写的jQuery:
$(document).ready(function () {
$('#input-shipping-country').change(function () {
var i = $('#input-shipping-country').val();
$('#hiddenPaymentCountryId').val(i);
});
$('#input-shipping-country').select2({
placeholder: "--- Please Select ---",
allowClear: true
});
var mySelect = $('#input-shipping-zone');
$.each( function (val, text) {
mySelect.append(
$('<option></option>').val(val).html(text)
);
});
$('#input-shipping-zone').select2({
placeholder: "Select a suburb",
allowClear: true,
ajax: {
url: '../index.php?route=extension/total/shipping/country' ,
dataType: 'json',
delay: 250,
data: function (params) {
return {
country_id : (($("#hiddenShippingCountryId").val() == "") ? ($('#input-shipping-country').val()) : $("#hiddenShippingCountryId").val()),
filter: params.term + '%',
page: params.page
};
},
processResults: function (data, page) {
var zones = [];
data['zone'].forEach(function (d) {
zones.push({
id: d.zone_id,
text: d.name
});
});
return {
results: zones
};
},
},
minimumInputLength: 2,
});
});
The only thing I wish to do is have the previously selected option displayed to the user and this can be changed if desired. 我唯一想做的就是将先前选择的选项显示给用户,并且可以根据需要更改。 This is implemented on OpenCart and uses a custom model to retrieve filtered options. 这是在OpenCart上实现的,并使用自定义模型来检索过滤的选项。
In my first attempt to implement this, I found it easier to use a hidden field and populate this when a selection was made on the $("#input-shipping-country")
so I've made another hidden field for the zone which is populated when the page loads: 在实现此目标的第一次尝试中,我发现在$("#input-shipping-country")
上进行选择时,更容易使用隐藏字段并填充此字段,因此我为该区域创建了另一个隐藏字段,页面加载时填充:
<input type="hidden" id="hiddenShippingCountryId" value="">
<input type="hidden" id="hiddenShippingZoneId" value="<?php echo $shipping_zone_id; ?>">
So in theory, I need to simply set the value of the select box once the control is initialised but the documentation isn't very clear here, should I have a default option in my HTML? 因此,从理论上讲,控件初始化后,我只需要简单地设置选择框的值,但是此处的文档不是很清楚,我的HTML中应该有默认选项吗?
在select
填充所有值之后,将默认值设置为
$('#input-shipping-country').val(1).change();//1 is value of select
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.