本文共 8562 字,大约阅读时间需要 28 分钟。
好的,现在我们来实现删除模板(model表删除记录)的功能,删除功能不难做,主要我们这次实现的是批量删除功能。
思路实现以后,我们上代码。
var id_array = []; //获取选中行 // 获取选中行 这里我们可以选择删除 这里的id_array是一个隐藏的值 device对应device_id user对应code survry对应model_id table.on('checkbox(test)', function(obj) { //监听复选框 if (obj.type == 'all') { if (obj.checked == true) { var data = table.cache.dataCheck; //批量操作的表格复选框 id_array = []; for (var l = 0; l < data.length; l++) { id_array.push(data[l].model_id); } console.log(id_array); } else { id_array = []; } } else { if (obj.checked == true) { id_array.push(obj.data.model_id); console.log(id_array); } else { var index = id_array.indexOf(obj.data.model_id); id_array.splice(index, 1); console.log(id_array); } } }); $('#btn-delete-all').click(function() { //删除全部通过一个获取选中行的值,来删除 layer.confirm('您确定要删除这些数据吗?', function(index) { //打开正在加载中弹出层 layer.msg('加载中', { icon: 16, shade: 0.01, time: '9999999' }); var url = "survey/del_model"; var data = { model_id: id_array //这里将当前的model_id传到后端 } $.post(url, data, function(data) { layer.close(layer.index); //关闭正在加载中弹出层 console.log(id_array); if (data.code == 1) { layer.msg(data.msg, { icon: 6 }); location.reload(); } else { layer.msg(data.msg, { icon: 5 }); } }, "json"); }); });
首先对数据表格进行监听,如果数据表格的当前行被选中,则往id_array数组中push一个id,有多少个,push多少个,而后我们将数组用ajax上传,键名为model_id.
//返回数据用responseBody //删除model @RequestMapping(value="Index/survey/del_model") @ResponseBody public MapdelModel(HttpServletRequest req) throws IOException { String[] arr = req.getParameterValues("model_id[]");//前端传来的modelId int code; String msg;// if(arr!=null)// return api.returnJson(3,arr[0]); for (int i = 0; i < arr.length; i++) { List is_answer=chooseService.findChooseByModelId(arr[i]); if (!is_answer.isEmpty()) { return api.returnJson(3,"抱歉,题目已经被作答,无法删除"); } continue; } //如果没有 那么当前选中模板的题目都没有被答过 作级联删除 删除模板表 删除题目表 删除选项表(根据model查出Qsn 再根据qsnId删除Option) int is_del=modelService.deleteModelByIds(arr);//删除模板表 for (int i = 0; i qsnList=qsnService.findQsnList(arr[i]);//得到题目 System.out.println(qsnList); for (int j = 0; j
这里进行说明一下,区别于tp5,我们使用httpservlet.request的getParameterValues()方法来获取前端传入的array数组
Service层:
modelService:package com.sl.example.service;import com.sl.example.pojo.Model;import java.util.List;public interface ModelService { public ListfindAllModel(); public int deleteModelById(String modelId); public int deleteModelByIds(String[] arr); public int InsertModel(Model model); public Model selectModelById(String modelId);}
modelServiceImpl:
package com.sl.example.service;import com.sl.example.dao.ModelMapper;import com.sl.example.pojo.Model;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;import java.util.List;@Service("modelService")@Transactionalpublic class ModelServiceImpl implements ModelService{ @Resource private ModelMapper modelMapper; @Override public ListfindAllModel() { return modelMapper.selectAllModel(); } @Override public int deleteModelById(String modelId) { return modelMapper.deleteByPrimaryKey(modelId); } @Override public int deleteModelByIds(String[] arr) { return modelMapper.deleteByIds(arr); } @Override public int InsertModel(Model model) { return modelMapper.insertSelective(model); } @Override public Model selectModelById(String modelId) { return modelMapper.selectByPrimaryKey(modelId); }}
将DAO层也贴出来:
package com.sl.example.dao;import com.sl.example.pojo.Model;import java.util.List;public interface ModelMapper { int deleteByPrimaryKey(String modelId); int deleteByIds(String[] list); int insert(Model record); int insertSelective(Model record); Model selectByPrimaryKey(String modelId); ListselectAllModel(); int updateByPrimaryKeySelective(Model record); int updateByPrimaryKey(Model record);}
对应的ModelMapper.xml:
model_id, name, time, create_name, rmk2, rmk3, rmk4, rmk5 delete from t_gr_qsn_model where model_id = #{modelId,jdbcType=VARCHAR} delete from t_gr_qsn_model where model_id in #{item} insert into t_gr_qsn_model (model_id, name, time, create_name, rmk2, rmk3, rmk4, rmk5) values (#{modelId,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{time,jdbcType=DATE}, #{createName,jdbcType=VARCHAR}, #{rmk2,jdbcType=VARCHAR}, #{rmk3,jdbcType=VARCHAR}, #{rmk4,jdbcType=VARCHAR}, #{rmk5,jdbcType=VARCHAR}) insert into t_gr_qsn_model model_id, name, time, create_name, rmk2, rmk3, rmk4, rmk5, #{modelId,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{time,jdbcType=DATE}, #{createName,jdbcType=VARCHAR}, #{rmk2,jdbcType=VARCHAR}, #{rmk3,jdbcType=VARCHAR}, #{rmk4,jdbcType=VARCHAR}, #{rmk5,jdbcType=VARCHAR}, update t_gr_qsn_model where model_id = #{modelId,jdbcType=VARCHAR} name = #{name,jdbcType=VARCHAR}, time = #{time,jdbcType=DATE}, create_name = #{createName,jdbcType=VARCHAR}, rmk2 = #{rmk2,jdbcType=VARCHAR}, rmk3 = #{rmk3,jdbcType=VARCHAR}, rmk4 = #{rmk4,jdbcType=VARCHAR}, rmk5 = #{rmk5,jdbcType=VARCHAR}, update t_gr_qsn_model set name = #{name,jdbcType=VARCHAR}, time = #{time,jdbcType=DATE}, create_name = #{createName,jdbcType=VARCHAR}, rmk2 = #{rmk2,jdbcType=VARCHAR}, rmk3 = #{rmk3,jdbcType=VARCHAR}, rmk4 = #{rmk4,jdbcType=VARCHAR}, rmk5 = #{rmk5,jdbcType=VARCHAR} where model_id = #{modelId,jdbcType=VARCHAR}
我们来试验一下。
成功啦,下一节我们讲对应某一套题目模板下的题目的增删。
转载地址:http://ppbnl.baihongyu.com/