番外-Easyui批量添加
2020-12-27 本文已影响0人
通灵路耳
1、JS与HTML页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>叶片管理</title>
<link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.3.3/themes/default/easyui.css"></link>
<link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.3.3/themes/icon.css"></link>
<script type="text/javascript" src="/static/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript" src="/static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="/static/js/date.js"></script>
<script type="text/javascript">
var url;
function closeProductDialog() {
$("#dlg").dialog("close");
resetValue();
}
/**
* 弃用
*/
function saveProduct() {
$("#dg1").form("submit", {
url: url,
onSubmit: function () {
return $(this).form("validate");
},
success: function (result) {
var result = eval("(" + result + ")");
if (result.success == 1) {
$.messager.alert("系统提示", "保存成功!");
resetValue();
$("#dlg").dialog("close");
$("#dg").datagrid("reload");
} else {
$.messager.alert("系统提示", result.errorInfo);
}
}
});
}
<!----------------------------打开新增框-------------------------------------->
function openProductAddDialog() {
$("#dlg").dialog("open").dialog("setTitle", "添加叶片信息");
url = "/bas/ye/save";
}
<!----------------------------打开修改框除-------------------------------------->
function openProductModifyDialog() {
var selectedRows = $("#dg").datagrid("getSelections");
if (selectedRows.length != 1) {
$.messager.alert("系统提示", "请选择一条要修改的数据!");
return;
}
//修改后保存
var row = selectedRows[0];
$("#dlg").dialog("open").dialog("setTitle", "修改物品信息");
$("#fm").form("load", row);
$('#product_id').textbox('textbox').attr('readonly',true);
url = "/bas/product/update";
$("#dg").datagrid("reload");
}
<!----------------------------删除-------------------------------------->
function deleteProduct() {
var selectedRows = $("#dg").datagrid("getSelections");
if (selectedRows.length != 1) {
$.messager.alert("系统提示", "请选择一条要删除的数据!");
return;
}
var guid = selectedRows[0].product_guid;
$.messager.confirm("系统提示", "您确定要删除这条数据吗?", function (r) {
if (r) {
$.post("/bas/product/delete?product_guid="+guid, function (result) {//删除
if (result.success) {
$.messager.alert("系统提示", "数据已成功删除!");
$("#dg").datagrid("reload");
} else {
$("#dg").datagrid("reload")
}
}, "json");
}
});
}
<!----------------------------双击单行数据打开修改框-------------------------------------->
$(document).ready(function () {
$("#dg").datagrid({
onDblClickRow: function (index, row) {
$("#dlg").dialog("open").dialog("setTitle", "修改物资信息");
$("#fm").form("load", row);
$('#product_id').textbox('textbox').attr('readonly',true);
url = "/bas/product/update";
}
});
});
<!----------------------------查询物资信息-------------------------------------->
function searchProduct() {
var a = $("#s_product_id").val();
var opts = $("#dg").datagrid("options");
opts.url = "/bas/ye/all?yeGroupId=" + a;
$('#dg').datagrid('reload');
}
function resetValue() {
$("#yeGroupId").textbox('setValue', "");
$("#productId").textbox('setValue', "");
$("#theOrder").textbox('setValue', "");
$("#quantity").textbox('setValue', "");
}
//<!--------------------------------------批量添加------------------------------------------>
//编辑的行
var editIndex = undefined;
function endEditing() {
if (editIndex == undefined){return true}
$('#dg1').datagrid('endEdit', editIndex);
editIndex = undefined;
return true;
}
function onClickCell(index, field){
if (editIndex != index) {
if (endEditing()) {
$('#dg1').datagrid('selectRow', index)
.datagrid('beginEdit', index);
var ed = $('#dg1').datagrid('getEditor', { index: index, field: field });
if (ed) {
($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
}
editIndex = index;
} else {
setTimeout(function () {
$('#dg1').datagrid('selectRow', editIndex);
}, 0);
}
}
}
function onEndEdit(index, row){
var ed = $(this).datagrid('getEditor', {
index: index,
field: 'productId'
});
}
//批量添加按钮
function append(){
var index = $('#dg1').datagrid('getRowIndex', $('#dg').datagrid('getSelected'));
if (index == -1)
index = 0;
$("#dg1").datagrid("insertRow", {
index: index+1,
row: {oper: "<a href='javascript:append()'>+<a> <a href='javascript:removeit()'>-<a>",status:'P'}
});
}
//删除
function removeit(){
if (editIndex == undefined){return}
$('#dg1').datagrid('selectRow', editIndex);
$('#dg1').datagrid('cancelEdit', editIndex)
.datagrid('deleteRow', editIndex);
editIndex = undefined;
}
//批量添加数据,Ajax提交
function accept(){
if (endEditing()){
var $dg = $('#dg1');
var rows = $dg.datagrid('getChanges');
if (rows.length) {
var inserted = $dg.datagrid('getChanges', "inserted");
var deleted = $dg.datagrid('getChanges', "deleted");
var updated = $dg.datagrid('getChanges', "updated");
var effectRow = new Object();
if (inserted.length) {
effectRow["inserted"] = JSON.stringify(inserted);
}
if (deleted.length) {
effectRow["deleted"] = JSON.stringify(deleted);
}
if (updated.length) {
effectRow["updated"] = JSON.stringify(updated);
}
}
}
$.ajax({
url: '/bas/ye/save',
type: "post",
async: true,
dataType: 'text',
data: { 'sper':effectRow["inserted"]},
success: function (data) {
if(data.message=='ok'){
alert(data.message);
}else{
alert(data.message);
return;
}
//重新加载表格数据
$('#dg').datagrid('reload');
}
});
}
//其他设置
function reject(){
$('#dg').datagrid('rejectChanges');
editIndex = undefined;
}
function getChanges(){
var rows = $('#dg').datagrid('getChanges');
alert(rows.length+' rows are changed!');
}
function contains(arr, obj) {
var i = arr.length;
while (i--) {
if (arr[i] === obj) {
return true;
}
}
return false;
}
//<!--------------------------------------批量添加到此打住------------------------------------------>
</script>
</head>
<body style="margin: 1px">
<table id="dg" class="easyui-datagrid" rownumbers="false"
fitColumns="true" pagination="true" singleSelect="true"
fit="true" toolbar="#tb" firstload="false">
<thead>
<th field="yeGroupGuid" width="100" align="center" hidden="hidden"></th>
<th field="yeGroupId" width="100" align="center">叶片组型号</th>
<th field="productId" width="100" align="center">叶片型号</th>
<th field="theOrder" width="150" align="center">序号</th>
<th field="quantity" width="50" align="center" >数量</th>
</thead>
</table>
<div id="tb">
<fieldset style="border-color: #E7F0FF">
<div>
<a href="javascript:openProductAddDialog()" class="easyui-linkbutton" iconCls="icon-add"
plain="true">添加</a>
<a href="javascript:openProductModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit"
plain="true">修改</a>
<a href="javascript:deleteProduct()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
</div>
<div>
叶片组编号: <input type="text" id="s_product_id" size="20"
onkeydown="if(event.keyCode==13) searchProduct()"/>
<a href="javascript:searchProduct()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
</div>
</fieldset>
</div>
<div id="dlg" class="easyui-dialog" style="width: 850px;height: 400px;padding: 10px 20px"
closed="true" buttons="#dlg-buttons" data-options="onClose:function(){resetValue()}">
<!-----------------------------批量添加------------------------------->
<table id="dg1" class="easyui-datagrid" title="批量添加" style="width:800px;height:auto"
data-options="
iconCls: 'icon-edit',
singleSelect: true,
toolbar: '#tb1',
url: 'datagrid_data1.json',
method: 'get',
onClickCell: onClickCell,
onEndEdit: onEndEdit
">
<thead>
<tr>
<th data-options="field:'oper',width:80">操作</th>
<th data-options="field:'itemid',width:80">Item ID</th>
<!--<th data-options="field:'productid',width:100,
formatter:function(value,row){
return row.productname;
},
editor:{
type:'combobox',
options:{
valueField:'productid',
textField:'productname',
method:'get',
url:'products.json',
}
}">Product</th>-->
<th data-options="field:'theOrder',width:80,align:'right',editor:'numberbox'">序号</th>
<th data-options="field:'quantity',width:80,align:'right',editor:'numberbox'">数量</th>
<th data-options="field:'productId',width:250,editor:'textbox'">叶片型号</th>
<th data-options="field:'status',width:250,align:'center',editor:{type:'checkbox',options:{on:'P',off:''}}">状态为P-才能添加</th>
</tr>
</thead>
</table>
<div id="tb1" style="height:auto">
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append()">Append</a>
<a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit()">Remove</a>
</div>
</div>
<div id="dlg-buttons">
<a href="javascript:accept()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
<a href="javascript:closeProductDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>
</body>
</html>
2、controller层
package com.mf.controller.bas;
import com.mf.entity.bas.BasYeGroup;
import com.mf.service.bas_service.BasYeService;
import com.mf.util.GetIp;
import com.mf.util.ResponseResult;
import com.mf.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@RestController
@RequestMapping("/bas/ye")
public class BasYeController {
@Autowired
private BasYeService basYeService;
@RequestMapping ("/save")
public ResponseResult<Void> yeAdd(HttpServletResponse response, HttpServletRequest request, String sper) throws Exception{
String IP = GetIp.getip(request, response);
//1、sper字符串接收数据
System.out.println("数据"+sper);
//2、首先把字符串转成 JSONArray 对象
JSONArray backBodyJson = JSONArray.fromObject(sper);
//3、遍历 jsonarray 数组,把每一个对象转成 json 对象
BasYeGroup basYeGroup = new BasYeGroup();
if (backBodyJson.size() > 0) {
for (int i = 0; i < backBodyJson.size(); i++) {
//4、开始拆分
JSONObject job = backBodyJson.getJSONObject(i);
//5、拿到叶片型号
String productId=(String) job.get("productId");
basYeGroup.setProductId(productId);
//3-1、如果叶片型号为空,忽略不添加
if(productId.equals("")){
continue;
}
//6、拿到序号
Integer theOrder=Integer.parseInt((String) job.get("theOrder"));
basYeGroup.setTheOrder(theOrder);
//7、拿到数量
Integer quantity=Integer.parseInt((String) job.get("quantity"));
basYeGroup.setQuantity(quantity);
//8、开始添加
basYeService.addYe(basYeGroup,IP);
System.out.println("所有数据:"+basYeGroup); // 得到 每个对象中的属性值
}
}
return new ResponseResult<Void>(1,"添加成功!");
}
}
3、Service
package com.mf.service.bas_service;
import com.mf.entity.bas.BasProduct;
import com.mf.entity.bas.BasYeGroup;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
public interface BasYeService {
/**
* 添加叶片
* @param basYeGroup
* @param ip
*/
void addYe(BasYeGroup basYeGroup, String ip);
}
4、serviceImpl实现类
package com.mf.service.bas_impl;
import com.mf.entity.SysUserLog;
import com.mf.entity.bas.BasYeGroup;
import com.mf.repository.bas.BasYeRepository;
import com.mf.service.SysLogService;
import com.mf.service.bas_service.BasYeService;
import com.mf.util.StringUtil;
import com.mf.util.UtilId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
@Service
public class BasYeServiceImpl implements BasYeService {
@Autowired
BasYeRepository basYeRepository;
@Autowired
SysLogService sysLogService;
/**
* 叶片添加
* @param basYeGroup
* @param ip
*/
@Override
public void addYe(BasYeGroup basYeGroup, String ip) {
basYeGroup.setYeGroupGuid(UtilId.uuids());
basYeRepository.save(basYeGroup);
sysLogService.save(new SysUserLog(SysUserLog.ADD_ACTION,"添加物资信息:"+basYeGroup.getYeGroupId(),ip));
}
}
5、dao层
package com.mf.repository.bas;
import com.mf.entity.bas.BasYeGroup;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface BasYeRepository extends JpaRepository<BasYeGroup,String>, JpaSpecificationExecutor<BasYeGroup> {
}
6、实体类
package com.mf.entity.bas;
import lombok.Data;
import org.springframework.data.domain.Sort;
import javax.persistence.*;
import java.io.Serializable;
/**
* 叶片
*/
@Data
@Entity//实体
@Table(name = "bas_ye_group")
@NamedQuery(name = "BasYeGroup.findAll",query = "select b from BasYeGroup b")
public class BasYeGroup implements Serializable {
@Id
@Column(name = "ye_group_guid")
public String yeGroupGuid;
@Column(name = "ye_group_id")
public String yeGroupId;
@Column(name = "product_id")
public String productId;
@Column(name = "the_order")
public Integer theOrder;
@Column(name = "quantity")
public Integer quantity;
public BasYeGroup(){}
public String getYeGroupGuid() {
return yeGroupGuid;
}
public void setYeGroupGuid(String yeGroupGuid) {
this.yeGroupGuid = yeGroupGuid;
}
public String getYeGroupId() {
return yeGroupId;
}
public void setYeGroupId(String yeGroupId) {
this.yeGroupId = yeGroupId;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public Integer getTheOrder() {
return theOrder;
}
public void setTheOrder(Integer theOrder) {
this.theOrder = theOrder;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
@Override
public String toString() {
return "BasYeGroup{" +
"yeGroupGuid='" + yeGroupGuid + '\'' +
", yeGroupId='" + yeGroupId + '\'' +
", productId='" + productId + '\'' +
", theOrder=" + theOrder +
", quantity=" + quantity +
'}';
}
}