el-upload 数组得新增删除
<el-upload
ref="upload"
:action="uploads.url + '?baseDir=' + uploads.baseDir"
:headers="uploads.headers"
:on-success="
handleUploadSuccess2.bind(null, { index: i, data: item })
"
:on-change="beforeUpload2"
:on-remove="
handleUploadRemove2.bind(null, { index: i, data: item })
"
:file-list="contentlist[i].fileList"
:limit="1"
accept=".jpg,.jpeg,.png"
>
<img
src="../../../assets/images/img2.png"
alt=""
class="exambs_cont5_div2_img"
/>
</el-upload>
handleUploadSuccess2(obj, res, file) {
if(res.code == 200){
this.contentlist[obj.index].fileList.push({
name: res.data.fileName,
url: res.data.filePath,
});
}else {
this.contentlist[obj.index].fileList = [];
this.$message({
message: res.msg,
type: "error",
duration: 2000,
});
console.log(this.contentlist);
}
},
handleUploadRemove2(obj, res, file) {
this.contentlist[obj.index].fileList=[];
},
完整代码
<template>
<div class="exambs">
<div class="exambs_return">
<img
src="../../../assets/images/left.png"
alt=""
@click="getreturn"
class="exambs_return_img"
/>
<img src="../../../assets/images/img1.png" alt="" />
<span>添加试题 </span>
</div>
<div class="exambs_cont1">
<el-form
:model="listForm"
:rules="listFormRules"
ref="listFormRef"
label-width="100px"
>
<el-form-item
label="题目类型"
prop="questionBankType"
class="exambs_cont1_item1"
>
<el-select
@input="$forceUpdate()"
v-model="listForm.questionBankType"
placeholder="请选择题目类型"
>
<el-option
v-for="item in options1"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
</el-form-item>
<el-form-item
label="难度等级"
prop="difficultyLevel"
class="exambs_cont1_item1"
>
<el-select
@input="$forceUpdate()"
v-model="listForm.difficultyLevel"
placeholder="请选择难度等级"
>
<el-option
v-for="item in options2"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select>
</el-form-item>
<el-form-item
label="归属题库"
prop="questionBankId"
class="exambs_cont1_item2"
>
<el-select
@input="$forceUpdate()"
v-model="listForm.questionBankId"
placeholder="请选择归属题库"
>
<el-option
v-for="item in options3"
:key="item.questionBankId"
:label="item.courseName"
:value="item.questionBankId"
>
</el-option>
</el-select>
</el-form-item>
<el-form-item label="题目内容" prop="subjectContent">
<el-input
@input="$forceUpdate()"
type="textarea"
v-model="listForm.subjectContent"
placeholder="请输入题目内容"
></el-input>
</el-form-item>
<el-form-item label="试题图片">
<el-upload
ref="upload"
:action="upload.url + '?baseDir=' + upload.baseDir"
:headers="upload.headers"
:on-success="handleUploadSuccess1"
:file-list="fileList"
:on-change="beforeUpload1"
:on-remove="handleUploadRemove1"
:limit="1"
accept=".jpg,.jpeg,.png"
>
<el-button size="small" type="primary">
<i class="el-icon-document-add"></i>
上传</el-button
>
<div slot="tip" class="el-upload__tip" style="margin-left: 40px">
(如果要更换试题图片,请删除已有的试题图片后在操作。)
</div>
</el-upload>
</el-form-item>
<el-form-item label="整体解析">
<el-input
@input="$forceUpdate()"
type="textarea"
v-model="listForm.remark"
placeholder="请输入整体解析"
></el-input>
</el-form-item>
</el-form>
</div>
<div class="exambs_cont2">
<div class="exambs_cont2_btn">
<el-button
type="primary"
icon="el-icon-document-add"
size="mini"
@click="handleadd"
>新增</el-button
>
</div>
<!-- 答题头部 -->
<div class="exambs_cont3">
<span class="exambs_cont3_sp1">是否答案</span>
<span class="exambs_cont3_sp2">选项图片</span>
<span class="exambs_cont3_sp3">答案内容</span>
<span class="exambs_cont3_sp4">操作</span>
</div>
<div class="exambs_cont4" v-show="contentlist.length == 0">暂无数据</div>
<div class="exambs_cont5" v-for="(item, i) in contentlist" :key="i">
<div class="exambs_cont5_div1">
<el-checkbox v-model="contentlist[i].value">答案</el-checkbox>
</div>
<!-- 上传图片数组 -->
<div class="exambs_cont5_div2">
<el-upload
ref="upload"
:action="uploads.url + '?baseDir=' + uploads.baseDir"
:headers="uploads.headers"
:on-success="
handleUploadSuccess2.bind(null, { index: i, data: item })
"
:on-change="beforeUpload2"
:on-remove="
handleUploadRemove2.bind(null, { index: i, data: item })
"
:file-list="contentlist[i].fileList"
:limit="1"
accept=".jpg,.jpeg,.png"
>
<img
src="../../../assets/images/img2.png"
alt=""
class="exambs_cont5_div2_img"
/>
</el-upload>
</div>
<div class="exambs_cont5_div3">
<el-input
@input="$forceUpdate()"
type="textarea"
v-model="contentlist[i].content"
placeholder="请输入答案内容"
></el-input>
</div>
<div class="exambs_cont5_div4">
<i class="el-icon-delete" @click="deletesb(i)"></i>
</div>
</div>
</div>
<div class="exambs_btn">
<el-button @click="getsure">保存</el-button>
</div>
</div>
</template>
<script>
import { questionsadd, questionBankAll } from "@/api/exam/test";
import { getToken } from "@/utils/auth";
export default {
data() {
return {
// 试题图片
upload: {
headers: { Authorization: "Bearer " + getToken() },
// 上传的地址
url: process.env.VUE_APP_BASE_API + "/common/upload/file",
baseDir: "test",
},
// 试题图片
uploads: {
headers: { Authorization: "Bearer " + getToken() },
// 上传的地址
url: process.env.VUE_APP_BASE_API + "/common/upload/file",
baseDir: "test",
},
fileList: [],
options1: [
{
label: "单选题",
value: 1,
},
{
label: "多选题",
value: 2,
},
{
label: "判断题",
value: 3,
},
],
options2: [
{
label: "初级",
value: 1,
},
{
label: "中级",
value: 2,
},
{
label: "高级",
value: 3,
},
],
options3: [],
listForm: {
questionBankType: "",
difficultyLevel: "",
questionBankId: "",
subjectContent: "",
remark: "",
imgUrl: "",
contentlist: [],
},
listFormRules: {
questionBankType: [
{ required: true, message: "请选择题目类型", trigger: "blur" },
],
difficultyLevel: [
{ required: true, message: "请选择难度等级", trigger: "blur" },
],
questionBankId: [
{ required: true, message: "请选择归属题库", trigger: "blur" },
],
subjectContent: [
{ required: true, message: "请输入题目内容", trigger: "blur" },
],
},
contentlist: [
{
value: false,
content: "",
fileList: [],
},
],
};
},
created() {
this.getquestionBankAll();
},
methods: {
//所有题库
getquestionBankAll() {
questionBankAll().then((res) => {
this.options3 = res.data;
});
},
//返回
getreturn() {
this.$router.push("/exam/test");
},
//新增
handleadd() {
this.contentlist.push({
value: false,
content: "",
fileList: [],
});
},
deletesb(i) {
this.contentlist.splice(i, 1);
},
handleUploadSuccess2(obj, res, file) {
if(res.code == 200){
this.contentlist[obj.index].fileList.push({
name: res.data.fileName,
url: res.data.filePath,
});
}else {
this.contentlist[obj.index].fileList = [];
this.$message({
message: res.msg,
type: "error",
duration: 2000,
});
console.log(this.contentlist);
}
},
handleUploadRemove2(obj, res, file) {
this.contentlist[obj.index].fileList=[];
},
handleUploadRemove1(file) {
this.listForm.imgName = "";
this.listForm.imgUrl = "";
},
// 上传成功回调
handleUploadSuccess1(file, fileList) {
console.log(file);
console.log(fileList);
if (file.code == 200) {
this.listForm.imgName = file.data.fileName;
this.listForm.imgUrl = file.data.filePath;
} else {
this.fileList = [];
this.$message({
message: file.msg,
type: "error",
duration: 2000,
});
}
},
beforeUpload1(file, fileList) {
console.log(file);
if (file.size / (1024 * 1024) > 500) {
// 限制文件大小
this.$message.warning(`当前限制文件大小不能大于500M`);
return false;
}
let suffix = this.getFileType1(file.name); //获取文件后缀名
let suffixArray = ["jpg", "png", "jpeg"]; //限制的文件类型,根据情况自己定义
if (suffixArray.indexOf(suffix) === -1) {
this.$message({
message: "文件格式错误,后缀名不属于.jpg,.jpeg,.png",
type: "error",
duration: 2000,
});
}
return suffixArray;
},
getFileType1(name) {
let startIndex = name.lastIndexOf(".");
if (startIndex !== -1) {
return name.slice(startIndex + 1).toLowerCase();
} else {
return "";
}
},
beforeUpload2(file, fileList) {
console.log(file);
if (file.size / (1024 * 1024) > 500) {
// 限制文件大小
this.$message.warning(`当前限制文件大小不能大于500M`);
return false;
}
let suffix = this.getFileType2(file.name); //获取文件后缀名
let suffixArray = ["jpg", "png", "jpeg"]; //限制的文件类型,根据情况自己定义
if (suffixArray.indexOf(suffix) === -1) {
this.$message({
message: "文件格式错误,后缀名不属于.jpg,.jpeg,.png",
type: "error",
duration: 2000,
});
}
return suffixArray;
},
getFileType2(name) {
let startIndex = name.lastIndexOf(".");
if (startIndex !== -1) {
return name.slice(startIndex + 1).toLowerCase();
} else {
return "";
}
},
//保存
getsure() {
this.listForm.contentlist = this.contentlist;
console.log(this.contentlist);
console.log(this.listForm);
this.$refs["listFormRef"].validate((valid) => {
if (valid) {
console.log(this.listForm);
questionsadd(this.listForm).then(response => {
this.$modal.msgSuccess("新增成功");
this.$router.push('/exam/test')
});
}
});
},
},
};
</script>
<style scoped lang="scss">
.exambs {
padding: 18px 33px;
}
.exambs_return {
margin-bottom: 18px;
.exambs_return_img {
margin-left: 5px;
margin-right: 26px;
cursor: pointer;
}
span {
font-size: 14px;
line-height: 18px;
color: #333333;
margin-left: 7px;
}
}
.exambs_cont1 {
background: #fff;
padding: 36px 30px;
border-radius: 8px;
::v-deep .el-form-item__label {
font-size: 14px;
line-height: 18px;
color: #666666;
font-weight: 400;
margin-right: 36px;
}
::v-deep .el-textarea {
width: 91.5%;
}
::v-deep .el-upload-list__item-name {
padding-left: 40px;
}
.exambs_cont1_item1 {
width: 32%;
display: inline-block;
::v-deep .el-form-item__label {
margin-top: 8px;
}
::v-deep .el-select {
width: 82%;
}
::v-deep .el-input--medium .el-input__inner {
border: 1px solid #c7c7cc;
}
}
.exambs_cont1_item2 {
width: 34.5%;
display: inline-block;
::v-deep .el-form-item__label {
margin-top: 8px;
}
::v-deep .el-select {
width: 74%;
}
::v-deep .el-input--medium .el-input__inner {
border: 1px solid #c7c7cc;
}
}
}
.exambs_cont2_btn {
margin: 20px 0px;
}
.exambs_btn {
margin-top: 40px;
}
.exambs_cont3 {
span {
display: inline-block;
font-size: 14px;
line-height: 40px;
color: #5d57d1;
height: 40px;
background: #ebeaff;
border-right: 1px solid #fff;
border-bottom: 1px solid #fff;
}
span:nth-of-type(4) {
border-right: 0px solid #fff;
}
.exambs_cont3_sp1 {
width: 10%;
text-align: center;
border-radius: 8px 0px 0px 0px;
}
.exambs_cont3_sp2 {
width: 20%;
padding-left: 35px;
}
.exambs_cont3_sp3 {
width: 60%;
padding-left: 35px;
}
.exambs_cont3_sp4 {
width: 10%;
text-align: center;
border-radius: 0px 8px 0px 0px;
}
}
.exambs_cont4 {
width: 100%;
background: #fff;
text-align: center;
height: 100px;
vertical-align: top;
border-bottom: 1px solid #ebeaff;
font-size: 20px;
line-height: 100px;
color: #999999;
}
.exambs_cont5 {
.exambs_cont5_div1 {
width: 10%;
display: inline-block;
background: #fff;
height: 100px;
vertical-align: top;
text-align: center;
border-right: 1px solid #ebeaff;
border-bottom: 1px solid #ebeaff;
.el-checkbox {
margin-top: 40px;
}
}
.exambs_cont5_div2 {
width: 20%;
display: inline-block;
background: #fff;
height: 100px;
vertical-align: top;
border-right: 1px solid #ebeaff;
border-bottom: 1px solid #ebeaff;
text-align: center;
.exambs_cont5_div2_img {
margin-top: 38px;
}
::v-deep .el-upload-list {
display: inline-block;
list-style: none;
margin-left: 12px;
width: 79%;
}
}
.exambs_cont5_div3 {
width: 60%;
display: inline-block;
background: #fff;
height: 100px;
vertical-align: top;
border-right: 1px solid #ebeaff;
border-bottom: 1px solid #ebeaff;
padding: 10px;
::v-deep .el-textarea__inner {
height: 80px;
resize: none;
}
}
.exambs_cont5_div4 {
width: 10%;
display: inline-block;
background: #fff;
height: 100px;
vertical-align: top;
text-align: center;
border-right: 0px solid #ebeaff;
border-bottom: 1px solid #ebeaff;
.el-icon-delete {
margin-top: 34px;
font-size: 25px;
color: #999999;
cursor: pointer;
}
.el-icon-delete:hover,
.el-icon-delete:focus {
color: #5d57d1;
}
}
}
</style>
效果图