Hi,
I load in Spring MVC a form with the values from the DB using a URL.
It's OK
But I would like to preload the value in the option.
This is the code
<script type="text/javascript">
$(document).ready(function() {
$("#findByAjaxCriteria").select2({
width: '100%',
minimumInputLength: 1,
placeholder: '<spring:message code="select2.mainBrand" />',
ajax: {
url: "${pageContext.request.contextPath}${requestScope.action_findByAjaxCriteria}",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term,
};
},
processResults: function (data,params) {
return {
results: $.map(data, function(obj) {
return { id:
obj.id, text : obj.brand};
}),
pagination: {
}
};
},
cache: true
}
})
});
</script>
this the form:
<form:form modelAttribute="mainModel" action="${pageContext.request.contextPath}${requestScope.action_save}" cssClass="form-horizontal form-label-left" method="POST">
<form:hidden path="id" />
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="brand"><spring:message code="mainBrand.brand"/> <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<form:select path="
brand.id" id="findByAjaxCriteria" class="form-control col-md-7 col-xs-12" >
<form:options items="${brand}" itemLabel="brand" itemValue="id"/>
</form:select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="model"><spring:message code="mainModel.model"/> <span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<form:input path="model" required="required" class="form-control col-md-7 col-xs-12"/>
<form:errors path="model"/>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
<a href="${pageContext.request.contextPath}${requestScope.action_cancel}" class="btn btn-primary">
<spring:message code="common.cancel"/>
</a>
<button type="submit" class="btn btn-success"><spring:message code="common.submit"/></button>
</div>
</div>
Thanks.