java+vue实现添加单选题、多选题到题库功能

本文为大家分享了java+vue实现添加选择题到题库功能的具体代码,供大家参考,具体内容如下

做个备份

数据库表:

后台接口

@DeleteMapping("deleteQuestion")
 @ApiOperation(value = "删除问题")
 public ServerResponse deleteQuestion(Integer id){
 sysQuestionMapper.deleteByPrimaryKey(id);
 sysQuestionAnswerMapper.deleteByQUestionId(id);
 return ServerResponse.createBySuccess("删除成功");
 }

 @GetMapping("getQuestionList")
 @ApiOperation(value = "获得问题列表")
 public ServerResponse getQuestionList(){
 List<SysQuestion> list = sysQuestionMapper.selectAllQuestion();
 return ServerResponse.createBySuccess(list);
 }

 @GetMapping("getQuestionAnswerList")
 @ApiOperation(value = "获得问题选项列表")
 public ServerResponse getQuestionAnswerList(Integer question_id){
 List<SysQuestionAnswer> list = sysQuestionAnswerMapper.selectByQuestionId(question_id);
 return ServerResponse.createBySuccess(list);
 }

 @PostMapping("addQuestion")
 @ApiOperation(value = "添加问题")
 public ServerResponse addQuestion(String question,String[] answerList,Integer[] answer){
 Integer type = 1;
 if (answer.length != 1) {
 type = 2;
 }
 String stringAnswer = "";
 List<Integer> list = Arrays.asList(answer);
 SysQuestion sysQuestion = new SysQuestion();
 sysQuestion.setQuestionName(question);
 sysQuestion.setCreateTime(new Date());
 sysQuestion.setType(type);
 sysQuestionMapper.insert(sysQuestion);
 Integer question_id = sysQuestionMapper.selectLastQuestionId();
 for (int i=0;i<answerList.length;i++){
 SysQuestionAnswer sysQuestionAnswer = new SysQuestionAnswer();
 sysQuestionAnswer.setAnswer(answerList[i]);
 sysQuestionAnswer.setQuestionId(question_id);
 sysQuestionAnswerMapper.insert(sysQuestionAnswer);
 Integer answer_id = sysQuestionAnswerMapper.selectLastAnswerId();
 if (list.contains(i)) {
 stringAnswer = stringAnswer + "," + answer_id;
 }
 }
 System.out.println(stringAnswer);
 stringAnswer = stringAnswer.substring(1,stringAnswer.length());
 System.out.println(stringAnswer);
 SysQuestion sysQuestion1 = sysQuestionMapper.selectByPrimaryKey(question_id);
 sysQuestion1.setAnswerId(stringAnswer);
 sysQuestionMapper.updateByPrimaryKey(sysQuestion1);
 return ServerResponse.createBySuccess("创建成功");
 }

 @PostMapping("updateQuestion")
 @ApiOperation(value = "更新问题")
 public ServerResponse updateQuestion(Integer question_id,String question,String[] answerList,Integer[] answer){
 Integer type = 1;
 if (answer.length != 1) {
 type = 2;
 }
 String stringAnswer = "";
 List<Integer> list = Arrays.asList(answer);
 sysQuestionAnswerMapper.deleteByQUestionId(question_id);
 for (int i=0;i<answerList.length;i++){
 SysQuestionAnswer sysQuestionAnswer = new SysQuestionAnswer();
 sysQuestionAnswer.setAnswer(answerList[i]);
 sysQuestionAnswer.setQuestionId(question_id);
 sysQuestionAnswerMapper.insert(sysQuestionAnswer);
 Integer answer_id = sysQuestionAnswerMapper.selectLastAnswerId();
 if (list.contains(i)) {
 stringAnswer = stringAnswer + "," + answer_id;
 }
 }
 stringAnswer = stringAnswer.substring(1,stringAnswer.length());
 SysQuestion sysQuestion1 = sysQuestionMapper.selectByPrimaryKey(question_id);
 sysQuestion1.setAnswerId(stringAnswer);
 sysQuestion1.setQuestionName(question);
 sysQuestion1.setType(type);
 sysQuestion1.setUpdateTime(new Date());
 sysQuestionMapper.updateByPrimaryKey(sysQuestion1);
 return ServerResponse.createBySuccess("更新成功");
}

代码中涉及的sql语句

<select id="selectLastQuestionId" resultType="int">
  select max(id) from sys_question
 </select>

 <select id="selectAllQuestion" resultMap="BaseResultMap">
  select * from sys_question order by create_time desc
</select>
<select id="selectByQuestionId" resultMap="BaseResultMap">
  select * from sys_question_answer
  where question_id=#{question_id}
 </select>

 <select id="selectLastAnswerId" resultType="int">
  select max(id) from sys_question_answer
 </select>

 <delete id="deleteByQUestionId">
  delete from sys_question_answer where question_id=#{question_id}
</delete>

vue页面

<!-- -->
<style lang="scss">
 tr {
 & > td.el-table__expanded-cell {
 font-size: 20px;
 }
 }
 .el-textarea.is-disabled .el-textarea__inner{
 color: #17181a !important;
 }
</style>
<style lang="scss" scoped>
 .shop-container {
 padding: 10px;
 }

 @import url("//unpkg.com/element-ui@2.4.0/lib/theme-chalk/index.css");
 .demo-table-expand {
 font-size: 0;
 }

 .demo-table-expand label {
 width: 90px;
 color: #67C23A;
 }

 .demo-table-expand .el-form-item {
 margin-right: 0;
 margin-bottom: 0;
 width: 100%;
 }

 .el-dialog {
 width: 50% !important;
 }
 .el-form-item {
 float: none!important;
 }
</style>
<template>
 <div class="product-container" v-loading.fullscreen.lock=fullscreenLoading>
 <div style="margin-top:10px;width: 100%">
 <div style="width: 20%;display:inline;float:right">
 <el-button @click="flag = 0, dialogFormVisible = true, text = '添加'" type="primary" round>添加</el-button>
 </div>
 </div>

 <el-dialog v-dialogDrag :title="text" :visible.sync="dialogFormVisible" :modal-append-to-body='false'>
 <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
 <el-form-item prop="question" label="问题" :rules="{
 required: true, message: '问题不能为空', trigger: 'blur'
 }">
 <el-input v-model="dynamicValidateForm.question"></el-input>
 </el-form-item>
 <el-form-item v-for="(domain, index) in dynamicValidateForm.domains" :label="'选项' + (index + 1)" :key="domain.key" :prop="'domains.' + index + '.value'" :rules="{
 required: true, message: '选项不能为空', trigger: 'blur'
 }">
 <el-input v-model="domain.value"></el-input><el-button @click.prevent="removeDomain(domain)">删除</el-button>
 </el-form-item>
 <el-form-item label="答案">
 <el-select v-model="value" multiple placeholder="请选择">
 <el-option
 v-for="(domain, index) in dynamicValidateForm.domains"
 :key="domain.key"
 :label="'选项' + (index + 1)"
 :value="index">
 </el-option>
 </el-select>
 </el-form-item>

 <el-form-item>
 <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
 <el-button @click="addDomain">新增选项</el-button>
 <el-button @click="resetForm('dynamicValidateForm')">清空</el-button>
 </el-form-item>
 </el-form>
 </el-dialog>

 <el-table max-height="600" highlight-current-row header-align="center" align="center"
 :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)" stripe style=" width: 100%"
 :default-sort="{prop: 'id',order: 'descending'}">
 <el-table-column label="问题" align="center" min-width="180px">
 <template slot-scope="scope">
 <el-input type="textarea" :disabled="true" style="font-size: 16px"
 :rows="2"
 placeholder="请输入内容"
 v-model="scope.row.questionName">
 </el-input>
 </template>
 </el-table-column>
 <el-table-column label="创建时间" prop="createTime" align="center" min-width="120px">
 </el-table-column>
 <el-table-column label="操作" align="center" min-width="250px" fixed="right">
 <template slot-scope="scope">
 <el-button @click="updateQuestion(scope.row.id,scope.row.questionName,scope.row.answerId)" size="mini" type="primary">更新
 </el-button>
 <el-button @click="deleteQuestion(scope.row.id)" size="mini" type="danger">删除
 </el-button>
 </template>
 </el-table-column>
 </el-table>
 <div align="center">
 <el-pagination
 @size-change="handleSizeChange"
 @current-change="handleCurrentChange"
 :current-page="currentPage"
 :page-sizes="[10, 20, 50, 100]"
 :page-size="pagesize"
 layout="total, sizes, prev, pager, next, jumper"
 :total="tableData.length">
 </el-pagination>
 </div>
 </div>
</template>
<script>
 import img_404 from '@/assets/404_images/image404.png'
 import { mapState, mapGetters } from 'vuex'
 import { getQuestionList, getQuestionAnswerList, addQuestion, updateQuestion, deleteQuestion } from '@/api/question'
 export default {
 data() {
 return {
 text: '',
 question_id: '',
 flag: 0,
 value: [],
 dynamicValidateForm: {
 domains: [{
 value: ''
 }],
 question: ''
 },
 templateSelection: '',
 total: null,
 dialogFormVisible: false,
 fullscreenLoading: false,
 img_404,
 tableData: [],
 currentPage: 1,
 pagesize: 10
 }
 },

 watch: {},

 components: {},

 computed: {
 ...mapState({
 userInfo: state => state.user.userInfo
 }),
 ...mapGetters([
 'orderListData'
 ])
 },

 methods: {
 deleteQuestion(id) {
 new Promise((resolve, reject) => {
 deleteQuestion(id).then(res => {
 this.$message.info('删除成功')
 this.initData()
 resolve(res)
 }).catch(err => {
 reject(err)
 })
 })
 },
 updateQuestion(id, question, answerId) {
 this.value = []
 this.question_id = id
 this.flag = 1
 this.text = '修改'
 this.dynamicValidateForm.question = question
 const answer = answerId.split(',').map(Number)
 new Promise((resolve, reject) => {
 getQuestionAnswerList(id).then(res => {
 console.log(res)
 this.dynamicValidateForm.domains = []
 for (let i = 0; i < res.data.data.length; i++) {
 if (answer.indexOf(res.data.data[i].id) !== -1) {
 this.value.push(i)
 }
 this.dynamicValidateForm.domains.push({
 value: res.data.data[i].answer
 })
 }
 resolve(res)
 }).catch(err => {
 reject(err)
 })
 })
 this.dialogFormVisible = true
 },
 submitForm(formName) {
 console.log(this.value)
 if (this.value.length === 0) {
 this.$message.warning('答案不能为空')
 return
 }
 this.$refs[formName].validate((valid) => {
 if (valid) {
 const answerList = []
 for (let i = 0; i < this.dynamicValidateForm.domains.length; i++) {
 answerList.push(this.dynamicValidateForm.domains[i].value)
 }
 if (this.flag === 0) {
 const FromData = {
 question: this.dynamicValidateForm.question,
 answerList: answerList,
 answer: this.value
 }
 new Promise((resolve, reject) => {
 this.fullscreenLoading = false
 addQuestion(FromData).then(res => {
 this.$message.success('添加成功')
 this.initData()
 resolve(res)
 }).catch(err => {
 reject(err)
 })
 })
 } else {
 const FromData = {
 question: this.dynamicValidateForm.question,
 answerList: answerList,
 answer: this.value,
 question_id: this.question_id
 }
 new Promise((resolve, reject) => {
 this.fullscreenLoading = false
 updateQuestion(FromData).then(res => {
 this.$message.success('修改成功')
 resolve(res)
 }).catch(err => {
 reject(err)
 })
 })
 }
 } else {
 console.log('error submit!!')
 return false
 }
 })
 },
 resetForm(formName) {
 this.$refs[formName].resetFields()
 },
 removeDomain(item) {
 this.value = []
 const index = this.dynamicValidateForm.domains.indexOf(item)
 if (index !== -1) {
 this.dynamicValidateForm.domains.splice(index, 1)
 }
 },
 addDomain() {
 this.dynamicValidateForm.domains.push({
 value: '',
 key: Date.now()
 })
 },
 submit(id) {
 this.newForm.opinion = ''
 this.newForm.id = id
 this.dialogFormVisible = true
 },
 timestampToTime(timestamp) {
 var date = new Date(timestamp)
 const Y = date.getFullYear() + '-'
 const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
 const D = date.getDate() + ' '
 const h = date.getHours() + ':'
 const m = date.getMinutes() + ':'
 const s = date.getSeconds()
 return Y + M + D + h + m + s
 },
 handleSizeChange: function(size) {
 this.pagesize = size
 },
 handleCurrentChange: function(currentPage) {
 this.currentPage = currentPage
 },
 async initData() {
 this.fullscreenLoading = true
 new Promise((resolve, reject) => {
 this.fullscreenLoading = false
 getQuestionList().then(res => {
 this.setData(res)
 resolve(res)
 }).catch(err => {
 reject(err)
 })
 })
 },
 setData(res) {
 console.log(res)
 this.tableData = []
 this.tableData = res.data.data
 for (var i = 0; i < this.tableData.length; i++) {
 this.tableData[i].createTime = this.timestampToTime(this.tableData[i].createTime)
 }
 }
 },

 mounted: function() {
 this.initData()
 }
 }

</script>
<style lang="scss" scoped>

</style>

实现效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • vue实现添加与删除图书功能

    先放大图,当我们点击删除的时候,图书名单就会被我们删掉.当我们重新添加回来或者添加新书的时候,我们只需要在添加新书这里添加即可. 当我点击删除的时候,只需要的就是除却删除的那一个书籍之后剩下的图书. 我们先来看看splice的用法. splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目. 注释:该方法会改变原始数组 故在删除书籍中我们会用到的方法是 删掉idx位置的那一种书,返回剩下的新的书籍数组. 当我们点击添加新书的时候,如果新书的id值比之前的书籍列表的值大,就添加在书籍

  • vue实现学生录入系统之添加删除功能

    一.案例代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>vue指令综合案例</title> <style> #app{ margin: 50px auto; width: 620px; } fieldset{ border: 2px solid plum; margin-bottom: 20px; } fieldset inp

  • vue与bootstrap实现简单用户信息添加删除功能

    本文实例为大家分享了vue与bootstrap实现用户信息添加删除操作的具体代码,供大家参考,具体内容如下 小记: 1.v-model=""    用于input表单双向数据绑定  逻辑层跟渲染层双向绑定 2.v-on:click='add()'     click方法绑定 3.v-for='(item,index) in myData'   遍历数组  {{index}}      {{item.name}}      {{item.age}}   适用于vue版本2.0 4.v-f

  • java+vue实现添加单选题、多选题到题库功能

    本文为大家分享了java+vue实现添加选择题到题库功能的具体代码,供大家参考,具体内容如下 做个备份 数据库表: 后台接口 @DeleteMapping("deleteQuestion") @ApiOperation(value = "删除问题") public ServerResponse deleteQuestion(Integer id){ sysQuestionMapper.deleteByPrimaryKey(id); sysQuestionAnswer

  • Java基于JDBC实现事务,银行转账及货物进出库功能示例

    本文实例讲述了Java基于JDBC实现事务,银行转账及货物进出库功能.分享给大家供大家参考,具体如下: 1. 转账业务 转账必须执行2个sql语句(update更新)都成功的情况下,提交事务,如果有一个失败,则2个都回滚事务 2. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性. ① 原子性(atomicity).一个事务是一个不可分割的工作单位,事务中包括的诸操作要么都做,要么都不做. ② 一致性(consistency).事务必须是使数据库从一个一致性状

  • Java实现Excel表单控件的添加与删除

    目录 介绍 Java示例1添加表单控件 Java示例2删除表单控件 介绍 通过表单控件,用户可以快速地将数据填写到模板文档中,轻松引用单元格数据并与其进行交互.本文通过Java代码示例介绍如何在Excel表格中添加表单控件,包括文本框.单选按钮.复选框.组合框.微调按钮等:以及如何删除Excel中的指定表单控件. 程序运行环境:Java.IDEA.jdk1.8.0.无需安装Microsoft Excel 使用工具:Free Spire.XLS for Java (免费版) jar获取及导入:官网

  • Vue form 表单提交+ajax异步请求+分页效果

    废话不多说了,直接给大家贴代码了,具体代码如下所示: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <meta charset="UTF-

  • VUE实现表单元素双向绑定(总结)

    本文介绍了VUE实现表单元素双向绑定(总结) ,分享给大家,具体如下: checkbox最基本用法: <input type="checkbox" v-model="inputdata" checked/> <input type="checkbox" v-model="inputdata"/> <input type="checkbox" v-model="inpu

  • 学习vue.js表单控件绑定操作

    本文实例为大家分享了vue.js表单控件绑定的具体代码,供大家参考,具体内容如下 html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>表单控件绑定</title> </head> <body> <!-- v-model在表单控件元素上实现数据的双向绑定 -->

  • 基于HTML5+js+Java实现单文件文件上传到服务器功能

    上传单文件到服务器       应公司要求,在HTML5页面上实现上传文件到服务器,对于一个还没毕业的实习生菜鸟来说,这可不得了-----不会,网上各种百度,找各种博客还是没解决,最后还是请教了公司的大神,人家给卸了一个例子,然后根据人家写的终于把这个上传文件搞定. 好了,开始上代码. HTML5代码: <form name="upform" action="" method="POST"> <input type ="

  • Vue.js表单标签中的单选按钮、复选按钮和下拉列表的取值问题

    Vue.js可以很方便的实现数据双向绑定,所以在处理表单,人机交互方面具有很大的优势.下面给大家介绍Vue.js表单标签中的单选按钮.复选按钮和下拉列表的取值问题. 摘要: 表单标签取值问题中,单选按钮.复选按钮和下拉列表都比较特殊.这里总结一下vue.js中关于单选按钮.复选按钮和下拉列表不同情况的取值特殊性问题. 表单标签取值问题中,单选按钮.复选按钮和下拉列表都比较特殊.这里总结一下vue.js中关于单选按钮.复选按钮和下拉列表不同情况的取值特殊性问题. 一.单选按钮 单选按钮:单选按钮用

  • vue axios 表单提交上传图片的实例

    项目中用的element 的框架,然后在项目有一个添加数据需求是图片可上传,也可不上传, 然后问题就是element 中的上传控件在没有图片的时候是不会触发提交的,但接口写的是有file的  multipart/form-data    接收模式 所有只能自己另个模仿一个表单上传 <input class="file" name="file" type="file" accept="image/png,image/gif,imag

  • Vue.js 表单控件操作小结

    概念说明 v-model指令:在表单控件元素上创建双向数据绑定.v-model 会根据控件类型自动选取正确的方法来更新元素. 输入框 实例中演示了 input 和 textarea 元素中使用 v-model 实现双向数据绑定: HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 测试实例 - 菜鸟教程(runoob.com)</tit

随机推荐