1. 相比于
【Java Web】【2】SSM框架3个字符串,数组前后台传参显示(增加操作),编辑就是先在include的jsp页面给表格赋值,同样是需要遍历表格,然后赋值
2. 赋值操作:在include的jsp页面,通过js给表格setData
// 设置值
XC.setData = function(list){
var rows = [];
for(var i=0; i<list.length; i++){
var bean = list[i];
var row = {
xcap:"",//行程安排
jtgj:"",//交通工具
txrs:""//同行人数
};
row.id = this.getId();
row.xcap = bean[0];//行程安排
row.jtgj = bean[1];//交通工具
row.txrs = bean[2];//同行人数
rows.push(row);
}
$("#xcGrid").datagrid({data: rows});
}
3. 设置值之前,还需要讲传进来的json串转为数组,通过eval()就可以讲json串转为数组(我的json串为:[["1","2","3"],
["1","2","3"],
["1","2","3"]...])
<tr class="item" style="display: none;">
<td class="item-label">收入类型以及电价</td>
<td class="item-val" colspan="5">
<input
id="ptype_tag"
name="bean.ptype"
style="width:80%;"
class="easyui-textbox"
data-options="
required: true,
validType: ['length[1, 200]']
">
</td>
</tr>
<script type="text/javascript">
// 框架加载完毕
page.layoutComplete = function() {
loadGroupData();
}
// 加载公司信息
function loadGroupData() {
Toast.load("页面正在初始化...", false, function() {
Ajax({
url: "${base}/companyProduct_info.do",
success: function(rs){
Toast.close();
fillForm2("tableForm", parent.Bean.modRow);
indata(rs);
}
});
});
}
function indata(rs){
var list = eval($("#ptype_tag").textbox("getValue"));
XC.setData(list);
}
4. 传给后台并接受
// 保存
function save() {
var datas = XC.getFormatData();
var d = JSON.stringify(datas);
if(datas.length < 1){
Toast.error("请输入收入类型");
return;
}
// 校验表单
var form = $("#tableForm");
if (!form.form("validate")) return;
// 请求成功
var onSuccess = function(rs) {
if (rs.returnCode != 0) {
Toast.error(rs.returnMessage, 2000);
}else{
Toast.ok("数据保存成功", 1000, function() {
closeSelf();
});
}
}
// loading
Toast.load("提交中...", false, function() {
// 构造formdata
formData = new FormData(form.get(0));
Ajax({
type: "POST",
url: "${base}/companyProduct_mod.do",
data: {
"bean.pid":$("#pid").textbox("getValue"),
"bean.companyid":$("#companyid").textbox("getValue"),
"bean.companyname":$("#companyname").textbox("getValue"),
"bean.pname":$("#pname").textbox("getValue"),
"bean.ptype": d
},
success: onSuccess
});
});
}
5. 后台接收
/**
* 编辑
*/
public void mod() {
Result result = new Result();
try {
// 校验公司名称唯一性
List<CompanyProductBean> list = companyProductService.infoByField("pname", bean.getPname().trim());
if(0 != list.size()){
CompanyProductBean cb = list.get(0);
if (!cb.getPid().equals(bean.getPid())) {
result.setRe(ReturnCode.R__2, "产品已存在");
ResponseUtil.sendResult(result);
return;
}
}
companyProductService.modify(bean);
}catch (Exception e) {
e.printStackTrace();
result.fail();
log.error("[CompanyProductAction.mod] 编辑出错", e);
}
ResponseUtil.sendResult(result.toJSONString());
}