我有一些模式与表单,我创建对象存储到数据库中。 在这些表格中,我有一个像这样的选择字段:
list1 = list2
用Controller从Controller传递用户。使用这种方法,我必须更新所有页面以刷新select字段内的值,否则我只能更新我显示新创建对象的表,但我不能在select中使用新字段。 问题是性能和外观刷新页面,而且我不能使用这些指令来显示我的消息
<div class="form-group" id=existingUser>
<label>Username</label> <select class="form-control select2"
style="width: 100%;" th:field="*{user}">
<option th:each="user: ${users}" th:value="${user.username}"
th:text="${user.username}"></option>
</select>
</div>
调用模态时有没有办法调用ajax?或者我可以将数据从javascript传递给HTML(如果我在单击添加按钮时检索值)。 有时候我也会检查select字段是否为空,在这种情况下会在modals中显示一条消息而不是form.Thanks
更新。我想这个代码:
首先只有成功管理:
location.reload();
//reload only the tag with id carsTable, so only the table
//$('#carsTable').load(document.URL + ' #carsTable');
$('#addCarModal').modal("hide");
notifyMessage("Your car has been created!", 'success');
function notifyMessage(textMessage, typeMessage){
$.bootstrapGrowl(textMessage, {
type: typeMessage, // (null, 'info', 'error', 'success')
});
}
在我的html页面中:
function freeUserController($scope) {
$http.get("https://localhost:8080/users/")
.success(function(data) {
$scope.users = data;
});
}
答案 0 :(得分:0)
我假设您在服务器上呈现HTML。可能没有办法让它重新渲染该元素。但是,有不同的方法可以做到这一点:
一,你可以开始使用像Angular.js这样的客户端MVC /渲染。这样,您可以在添加新字段时自动刷新选择字段。
二,您可以在不使用MVC系统的情况下将新选项放入选择字段。这需要从视图中解耦数据,所以我不推荐它。但是,您可以让提交按钮执行ajax调用以确保服务器正确响应,并且仅在服务器响应到达时添加新选项。您的代码看起来像这样:
$.ajax(url).done(function(){
$('#my-select').append('<option>').html('your data')
}).fail(function(){
// show error message...
});