vue基于elementui的增删改查

 

 import { quillEditor } from "vue-quill-editor"; //调用编辑器
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";

是添加和修改的时候引入的富文本

<template>
  <div class="wrap">
    <!-- 顶层输入框搜索和新增 -->
    <el-form :inline="true" ref="user" :model="user">
      <el-form-item label="搜索内容">
        <el-input
          placeholder="请输入搜索内容"
          v-model="user.name"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <div class="center">
      <el-table v-loading="loading" :data="tableData" style="width: 100%">
        <el-table-column
          prop="articleTime"
          label="日期"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="articleType"
          label="类型"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="articleName" label="标题" width="180">
        </el-table-column>
        <el-table-column prop="articleContent" label="内容"> </el-table-column>
        <el-table-column width="150" label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >查看</el-button
            >
            <el-button type="text" size="small" @click="handleUpdate(scope.row)"
              >修改</el-button
            >
            <el-button type="text" size="small" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 添加弹出框 -->
    <el-dialog title="新增" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="articleName">
          <el-input v-model="form.articleName" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="类型">
          <el-radio-group v-model="form.articleType">
            <el-radio label="新闻动态"></el-radio>
            <el-radio label="学院动态"></el-radio>
          </el-radio-group>
        </el-form-item>

        <el-upload  style="margin-left: 80px;margin-bottom: 30px;"
          class="upload-demo"
          action="http://staticfile.xlcwx.com/api/modalupload/image"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :file-list="fileList"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件,且不超过500kb
          </div>
        </el-upload>

        <el-form-item label="内容" prop="articleContent" style="height: 320px">
          <!-- <el-input
            v-model="form.articleContent"
            type="textarea"
            placeholder="请输入内容"
          />
          <tinymce placeholder="内容" :height="400" v-model="form.articleContent"></tinymce> -->
          <quill-editor
            v-model="form.articleContent"
            ref="myQuillEditor"
            :options="editorOption"
            @blur="onEditorBlur($event)"
            @focus="onEditorFocus($event)"
            @change="onEditorChange($event)"
            style="height: 260px"
          >
          </quill-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 修改弹出框 -->
    <el-dialog title="修改" :visible.sync="opens" width="1000px" append-to-body>
      <el-form ref="forms" :model="forms" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="articleName">
          <el-input v-model="forms.articleName" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="类型">
          <el-radio-group v-model="forms.articleType">
            <el-radio label="新闻动态"></el-radio>
            <el-radio label="学院动态"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-upload style="margin-left: 80px;margin-bottom: 30px;"
          class="upload-demo"
          action="http://staticfile.xlcwx.com/api/modalupload/image"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :file-list="fileList"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件,且不超过500kb
          </div>
        </el-upload>
        <el-form-item label="内容" prop="articleContent" style="height: 320px">
          <quill-editor
            v-model="forms.articleContent"
            ref="myQuillEditor"
            :options="editorOption"
            @blur="onEditorBlur($event)"
            @focus="onEditorFocus($event)"
            @change="onEditorChange($event)"
            style="height: 260px"
          >
          </quill-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForms">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看弹出框 -->
    <el-dialog
      title="查看"
      :visible.sync="openss"
      width="1000px"
      append-to-body
    >
      <el-form ref="formss" :model="formss" :rules="rules" label-width="80px">
        <div style="display: flex;justify-content: space-between;align-items: center;">
          <div style="width: 80%;height: 80%;">
            <el-form-item label="时间" prop="articleTime " style="width: 80%;">
              <el-input v-model="formss.articleTime" placeholder="请输入时间"  style="width: 100%;"/>
            </el-form-item>
            <el-form-item label="类型" prop="articleType " style="width: 80%;">
              <el-input v-model="formss.articleType" placeholder="请输入类型"  style="width: 100%;"/>
            </el-form-item>
            <el-form-item label="标题" prop="articleName" style="width: 80%;">
              <el-input v-model="formss.articleName" placeholder="请输入标题" style="width: 100%;" />
            </el-form-item>
          </div>
          <div style="width: 200px;height: 200px;margin-top: 15px;">
            <img style="width: 80%;height: 80%;" :src="formss.articlePicture" alt="" />
          </div>
        </div>
        <el-form-item label="内容" prop="articleContent" style="height: 320px">
          <quill-editor
            v-model="formss.articleContent"
            ref="myQuillEditor"
            :options="editorOption"
            @blur="onEditorBlur($event)"
            @focus="onEditorFocus($event)"
            @change="onEditorChange($event)"
            style="height: 260px"
          >
          </quill-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import tinymce from "@/components/Tinymce/index.vue";
import { queryAllArticle } from "@/api/index.js";
import { quillEditor } from "vue-quill-editor"; //调用编辑器
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import {
  queryByName,
  insertArticle,
  updateArticle,
  deleteById,
} from "@/api/backstage.js";
export default {
  components: { quillEditor },
  //  components: { tinymce },
  data() {
    return {
      fileList: [
        {
          name: "默认照片.jpeg",
          url: "https://staticfile.xlcwx.com/userfiles/202008/0551ae24486e41a9893155a5529813168e20200831163249556.jpg",
        },
      ],
      editorOption: {},
      loading: true,
      open: false,
      opens: false,
      openss: false,
      user: {
        name: "",
      },
      form: {
        articleName: "",
        articleType: "",
        articleContent: "",
        id: "",
        articlePicture: "",
      },
      forms: {
        articleName: "",
        articleType: "",
        articleContent: "",
        id: "",
        articlePicture: "",
      },
      formss: {
        articleName: "",
        articleType: "",
        articleContent: "",
        id: "",
        articleTime: "",
        articlePicture: "",
      },
      tableData: [],
      rules: {
        articleName: [
          { required: true, message: "标题不能为空", trigger: "blur" },
        ],
        articleType: [
          { required: true, message: "类型不能为空", trigger: "blur" },
        ],
        articleContent: [
          { required: true, message: "内容不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.a();
  },
  computed: {
    editor() {
      return this.$refs.myQuillEditor.quill;
    },
  },

  methods: {
    onEditorReady(editor) {
      // 准备编辑器
      console.log(editor);
    },
    onEditorBlur() {}, // 失去焦点事件
    onEditorFocus() {}, // 获得焦点事件
    onEditorChange() {}, // 内容改变事件
    // 页面渲染方法
    a() {
      // 页面渲染接口
      queryAllArticle().then((res) => {
        console.log(res.data.rows);
        this.tableData = res.data.rows;
      });
    },
    // 重置
    reset() {
      this.form = {
        id: "",
        articleName: undefined,
        articleType: undefined,
        articleContent: undefined,
        articlePicture: undefined,
      };
    },
    // 搜索方法
    getList() {
      // 加载
      this.loading = true;
      // 搜索接口 根据标题搜索
      queryByName(this.user.name).then((res) => {
        console.log(res.data);
        this.tableData = res.data;
        // 取消加载
        this.loading = false;
      });
    },
    // 搜索按钮
    handleQuery() {
      // 执行搜索方法
      this.getList();
    },
    // 新增
    handleAdd() {
      this.reset();
      // 打开新增弹窗
      this.open = true;
    },
    // 取消按钮
    cancel() {
      // 关闭添加弹窗
      this.open = false;
      // 关闭修改弹窗
      this.opens = false;
      // 关闭查看弹窗
      this.openss = false;
      this.reset();
      this.a();
    },
    // 确定
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // 新增
          insertArticle(
            // 新增标题
            this.form.articleName,
            // 新增内容
            this.form.articleContent,
            // 新增图片
            this.form.articlePicture,
            // 新增时间
            this.form.articleTime,
            // 新增类型
            this.form.articleType
          ).then((res) => {
            console.log(res);
            this.open = false;
            this.a();
          });
        }
      });
    },
    //修改
    handleUpdate(row) {
      console.log(row);
      // 打开弹窗
      this.opens = true;
      // 渲染标题
      this.forms.articleName = row.articleName;
      // 渲染类型
      this.forms.articleType = row.articleType;
      // 渲染内容
      this.forms.articleContent = row.articleContent;
      // 渲染id
      this.forms.id = row.id;
      // 渲染图片
      this.forms.articlePicture = row.articlePicture;
    },
    //修改确定
    submitForms() {
      updateArticle(
        // 传入id
        this.forms.id,
        // 传入标题
        this.forms.articleName,
        // 传入内容
        this.forms.articleContent,
        // 传入图片
        this.forms.articlePicture,
        // 传入时间
        this.forms.articleTime,
        // 传入类型
        this.forms.articleType
      ).then((res) => {
        console.log(res);
        this.forms = {
          // 修改内容
          articleContent: res.data.articleContent,
          // 修改标题
          articleName: res.data.articleName,
          // 修改图片
          articlePicture: res.data.articlePicture,
          // 修改时间
          articleTime: res.data.articleTime,
          // 修改类型
          articleType: res.data.articleType,
          // 修改id
          id: res.data.id,
        };
        // 关闭弹窗重新渲染页面
        this.opens = false;
        this.a();
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm("是否确认删除数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 删除接口传入当前id
          deleteById(row.id).then((res) => {
            if (res.status == 200) {
              this.getList();
              this.a();
            }
          });
        })
        .catch(() => {});
    },
    // 查看
    handleClick(row) {
      console.log(row);
      // 打开弹窗
      this.openss = true;
      // 渲染标题
      this.formss.articleName = row.articleName;
      // 渲染类型
      this.formss.articleType = row.articleType;
      // 渲染内容
      this.formss.articleContent = row.articleContent;
      // 渲染id
      this.formss.id = row.id;
      // 渲染时间
      this.formss.articleTime = row.articleTime;
      // 渲染图片
      this.formss.articlePicture = row.articlePicture;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
      console.log(file.url);
      this.form.articlePicture = file.url;
      this.forms.articlePicture = file.url;
      this.formss.articlePicture = file.url;
    },
  },
};
</script>
<style lang="less" scoped>
.wrap {
  width: 90%;
  margin: auto;
  height: 100%;
  position: relative;
  .el-form {
    margin-top: 200px;
  }
  .center {
    width: 100%;
    height: auto;
  }
}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值