通过struts2操作类(使用jtable)从数据库填充下拉列表
Populating dropdown from database via Struts 2 action class (using jtable)
我在我的项目中使用jtable http://jtable.org/。jTable是一个jQuery插件,用于创建基于AJAX的CRUD表,而无需编码HTML或Javascript。
http://i62.tinypic.com/3461eo3.jpg以上格式的jtable代码为
HTML代码:<div id="StudentTableContainer"></div>
JavaScript代码:
<script type="text/javascript">
$(document).ready(function () {
$('#StudentTableContainer').jtable({
title: 'Student List',
paging: true,
pageSize: 10,
sorting: true,
defaultSorting: 'Name ASC',
actions: {
listAction: '/Demo/StudentList',
deleteAction: '/Demo/DeleteStudent',
updateAction: '/Demo/UpdateStudent',
createAction: '/Demo/CreateStudent'
},
fields: {
StudentId: {
key: true,
create: false,
edit: false,
list: false
},
Name: {
title: 'Name',
width: '30%'
},
EmailAddress: {
title: 'Email address',
list: false
},
Password: {
title: 'User Password',
type: 'password',
list: false
},
Gender: {
title: 'Gender',
options: { 'M': 'Male', 'F': 'Female' },
list: false
},
ContinentalId: {
title: 'Continental',
options: '/Demo/GetContinentalOptions',
list: false
},
CountryId: {
title: 'Country',
dependsOn: 'ContinentalId', //Countries depends on continentals. Thus, jTable builds cascade dropdowns!
options: function (data) {
if (data.source == 'list') {
//Return url of all countries for optimization.
//This method is called for each row on the table and jTable caches options based on this url.
return '/Demo/GetCountryOptions?continentalId=0';
}
//This code runs when user opens edit/create form or changes continental combobox on an edit/create form.
//data.source == 'edit' || data.source == 'create'
return '/Demo/GetCountryOptions?continentalId=' + data.dependedValues.ContinentalId;
},
list: false
},
CityId: {
title: 'City',
width: '30%',
dependsOn: 'CountryId', //Cities depends on countries. Thus, jTable builds cascade dropdowns!
options: function (data) {
if (data.source == 'list') {
//Return url of all cities for optimization.
//This method is called for each row on the table and jTable caches options based on this url.
return '/Demo/GetCityOptions?countryId=0';
}
//This code runs when user opens edit/create form or changes country combobox on an edit/create form.
//data.source == 'edit' || data.source == 'create'
return '/Demo/GetCityOptions?countryId=' + data.dependedValues.CountryId;
}
},
BirthDate: {
title: 'Birth date',
type: 'date',
displayFormat: 'yy-mm-dd',
list: false
},
Education: {
title: 'Education',
list: false,
type: 'radiobutton',
options: [
{ Value: '1', DisplayText: 'Primary school' },
{ Value: '2', DisplayText: 'High school' },
{ Value: '3', DisplayText: 'University' }
]
},
About: {
title: 'About this person',
type: 'textarea',
list: false
},
IsActive: {
title: 'Status',
width: '15%',
type: 'checkbox',
values: { 'false': 'Passive', 'true': 'Active' },
defaultValue: 'true'
},
RecordDate: {
title: 'Record date',
width: '25%',
type: 'date',
displayFormat: 'yy-mm-dd',
create: false,
edit: false,
sorting: false //This column is not sortable!
}
}
});
//Load student list from server
$('#StudentTableContainer').jtable('load');
});
</script>
我想在我的项目中有一个下拉菜单,应该通过struts2动作类从数据库中获取值。与上面的演示代码一样,可以通过struts2操作类(使用URL模式/Demo/GetContinentalOptions
由于jtable只理解json,所以请指导我在Struts2 Action类和Struts.xml中应该写什么
注意:在示例代码中,您甚至可以硬编码下拉值您可以使用以下操作填充json字段。您还需要一个约定插件来使用注释。要使用json result,你需要一个json插件。
@Action(value="GetContinentalOptions", results=@Result(type="json", params = {"root", "map"}))
public class ContinentalOptionsAction extends ActionSupport {
Map<String, String> map=new HashMap<>();
public Map<String, String> getMap() {
return map;
}
@Override
public String execute() throws Exception {
map.put("1", "Asia");
map.put("2", "America");
map.put("3", "Europe");
map.put("4", "Africa");
return SUCCESS;
}
}
在options
函数中
var options = [];
$.ajax({ //get from server
url: '<s:url action="GetContinentalOptions"/>',
dataType: 'json',
success: function (data) {
options = data;
}
});
return options;
编辑:如果没有约定插件,您应该在struts.xml
中编写操作配置
<action name="GetContinentalOptions" class="com.action.ContinentalOptionsAction">
<result type="json">
<param name="root" value="map"/>
</result>
</action>
相关文章:
- PHP下拉列表由要使用javascript的数据库填充
- 使用javascript服务填充数据库中的下拉值
- 从数据库预填充模式(非引导)
- 从数据库填充JQueryUI
- 从另一个选择框并使用数据库连接填充选择框
- 在选择下拉选项时,从mysql数据库填充HTML表单字段
- 从数据库填充的下拉菜单中更新输入字段
- 使用 JavaScript 从数据库填充网格视图
- 离子收集重复 - 未命中由websql数据库填充
- 如何根据另一个组合框的选定项目从数据库填充组合框中的项目
- 优化WebSQL本地数据库填充
- 从数据库填充谷歌地图信息窗口
- 如何使用Codeigniter从数据库填充下拉列表
- 输入值并与数据库填充的值进行比较后的报警框
- 从SQL数据库填充Facebook提要对话框
- 通过struts2操作类(使用jtable)从数据库填充下拉列表
- 在初始加载时根据下拉列表中的值设置文本框(由数据库填充)
- 使用SqlWhereBuilder ASP.. NET服务器控件从数据库填充值
- 使用 php 从数据库填充初始化 jquery 数组时,Jquery 标签编辑器不起作用
- 用json和php从数据库填充listview