ElementUI实现表格增删改查

本文详细介绍了如何利用ElementUI库在Vue.js项目中实现表格数据的增加、删除、修改和查询功能,通过JavaScript进行交互逻辑处理,打造高效易用的前端界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



 <template>
  <div style="padding: 20px">
    <!-- <el-header>Header</el-header> -->

    <div>
      <main class="h-full pb-16 overflow-y-auto">
        <div class="container grid px-6 mx-auto">
          <div style="position: relative">
            <a
              class="
                flex
                items-center
                justify-between
                p-4
                mb-8
                text-sm
                font-semibold
                text-purple-100
                bg-purple-600
                rounded-lg
                shadow-md
                focus:outline-none focus:shadow-outline-purple
              "
              href=""
            >
              <div class="flex items-center">
                <svg
                  class="w-5 h-5 mr-2"
                  fill="currentColor"
                  viewBox="0 0 20 20"
                >
                  <path
                    d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"
                  ></path>
                </svg>

                <span>画布列表</span>
              </div>
            </a>
            <span id="newcanvas" @click="newcanvasBtn"
              >新建画布 &RightArrow;</span
            >
          </div>

          <div style="display: flex; margin-bottom: 20px">
            <p class="surveyfile">
              <span>画布名称:</span
              ><el-input
                placeholder="请输入内容"
                v-model="pageRequestVo.name"
              ></el-input>
            </p>
            <p class="surveyfile">
              <span style="width: 100px">创建时间:</span>
              <el-date-picker
                v-model="datetime"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </p>
            <el-button
              type="primary"
              round
              style="margin-left: 40px"
              @click="searchbtn"
              >搜索</el-button
            >
          </div>
          <div class="w-full overflow-hidden rounded-lg shadow-xs">
            <div class="w-full overflow-x-auto">
              <el-table :data="tableData" style="width: 100%; cursor: pointer">
                <el-table-column label="序号" width="80">
                  <template slot-scope="scope">
                    <span style="margin-left: 10px">{{
                      scope.row.number
                    }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="画布名称">
                  <template slot-scope="scope">
                    <div
                      @click="detailed(scope.row.id)"
                      slot="reference"
                      class="name-wrapper"
                    >
                      <el-tag size="medium">{{ scope.row.name }}</el-tag>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="画布描述">
                  <template slot-scope="scope">
                    <span style="margin-left: 10px">{{
                      scope.row.description
                    }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="创建日期">
                  <template slot-scope="scope">
                    <i class="el-icon-time"></i>
                    <span style="margin-left: 10px">{{
                      scope.row.createTime
                    }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="更新时间">
                  <template slot-scope="scope">
                    <i class="el-icon-time"></i>

                    <span style="margin-left: 10px">{{
                      scope.row.modifiedTime
                    }}</span>
                  </template>
                </el-table-column>

                <el-table-column label="操作" width="300">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      @click="handleEdit(scope.$index, scope.row)"
                      >修改</el-button
                    >

                    <el-button
                      size="mini"
                      type="danger"
                      @click="handleDelete(scope.$index, scope.row)"
                      >删除</el-button
                    >

                    <el-button
                      size="mini"
                      @click="historyBtn(scope.$index, scope.row)"
                      >历史记录</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </div>

            <div class="block" style="margin-top: 30px">
              <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageRequestVo.page"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pageRequestVo.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totalElements"
              >
              </el-pagination>
            </div>

            <el-dialog
              title="修改信息"
              :visible.sync="dialogVisible"
              width="40%"
              center
            >
              <div class="mask-style">
                <p>
                  画布名称:<el-input
                    v-model="editList.name"
                    placeholder="请输入画布名称"
                    style="width: 80%; margin-bottom: 30px"
                  ></el-input>
                </p>

                <p>
                  画布描述:<el-input
                    v-model="editList.description"
                    placeholder="请输入画布描述"
                    style="width: 80%"
                  ></el-input>
                </p>
              </div>

              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>

                <el-button type="primary" @click="editBtn()">确 定</el-button>
              </span>
            </el-dialog>
          </div>
        </div>
      </main>
    </div>
    <el-dialog title="新建画布" :visible.sync="dialogVisible1" width="30%">
      <el-input
        v-model="cnavasname"
        placeholder="请输入画布名称"
        style="margin-top: 30px"
      ></el-input>
      <el-input
        v-model="describe"
        placeholder="请输入画布描述"
        style="margin-top: 30px"
      ></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="submitBtn">确 定</el-button>
      </span>
    </el-dialog>
    <!-- <el-footer>Footer</el-footer> -->
  </div>
</template>



  <script>
import {
  getCanvas,
  deleteCanvas,
  editCanvas,
  getAddCanvas,
  getCanvasId,
} from "@/apis/operate.js";

import dayjs from "dayjs";

export default {
  data() {
    return {
      datetime: null,
      cnavasname: "",
      describe: "",
      dialogVisible1: false,
      totalElements: null,
      tableData: [],
      editList: {
        name: "",
        description: "",
        id: "",
      },
      pageRequestVo: {
        createTimeEnd: null,
        createTimeStart: null,
        name: null,
        page: 1,
        size: 10,
      },
      dialogVisible: false,
    };
  },

  methods: {
    //搜索
    searchbtn() {
      console.log(this.datetime);

      if (this.datetime == null) {
        this.pageRequestVo.createTimeStart = null;
        this.pageRequestVo.createTimeEnd = null;
      } else {
        if (this.datetime.length > 0) {
          this.pageRequestVo.createTimeStart = this.datetime[0];
          this.pageRequestVo.createTimeEnd = this.datetime[1];
          if (this.pageRequestVo.createTimeStart != null) {
            this.pageRequestVo.createTimeStart =
              new Date(this.pageRequestVo.createTimeStart).getTime() / 1000;
          }
          if (this.pageRequestVo.createTimeEnd != null) {
            this.pageRequestVo.createTimeEnd =
              new Date(this.pageRequestVo.createTimeEnd).getTime() / 1000;
          }
        }
      }
      this.fn();
    },
    //历史
    historyBtn(index, row) {
      console.log(index, row);
      this.$router.push({
        path: "/ballList/history-list",
        query: { id: row.id },
      });
    },

    //详情跳转
    detailed(id) {
      console.log(id);
      this.$router.push({ path: "/HelloWorld", query: { id: id } });
    },

    //编辑
    handleEdit(index, row) {
      console.log(index, row);
      this.dialogVisible = true;
      this.editList.description = row.description;
      this.editList.name = row.name;
      this.editList.id = row.id;

      getCanvasId(row.id).then((res) => {
        console.log(res);
        this.editList.graph = res.data.graph;
        // console.log(this.editList.graph );
      });
    },

    //编辑
    editBtn() {
      console.log(this.editList);
      console.log(this.editList.id);
      editCanvas(this.editList.id, this.editList).then((res) => {
        console.log(res);
        this.tableData.forEach((item) => {
          console.log(item);
          if (item.id == this.editList.id) {
            item.name = this.editList.name;
            item.description = this.editList.description;
            this.dialogVisible = false;
            this.$message({
              message: "修改成功",
              type: "success",
            });
          }
        });
      });
    },

    //删除
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })

        .then(() => {
          deleteCanvas(row.id).then((res) => {
            console.log(res);
            if (res.status == 200) {
              this.tableData.forEach((item, i) => {
                if (item.id == row.id) {
                  this.tableData.splice(i, 1);
                  this.fn();
                }
              });
            }
          });

          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })

        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //数据接口
    newcanvasBtn() {
      // this.$router.push("/home");
      // sessionStorage.clear()

      this.dialogVisible1 = true;
    },
    submitBtn() {
      let graph;
      if (typeof sessionStorage.graph == "undefined") {
        graph = {
          all: false,
          edgeSize: 0,
          edges: [],
          empty: false,
          vertexSize: 0,
          vertices: [],
        };
      } else {
        graph = JSON.parse(sessionStorage.graph);
        graph.vertices = [];
        graph.edges = [];
        graph.edgeSize = graph.edges.length;
        graph.vertexSize = graph.vertices.length;
        sessionStorage.graph = JSON.stringify(graph);
      }
      let data = {
        graph: JSON.stringify(graph),
        name: this.cnavasname,
        description: this.describe,
      };
      getAddCanvas(data).then((res) => {
        console.log(res);
        this.tableData.push(res.data);
        this.dialogVisible1 = false;
        this.detailed(res.data.id);
      });
    },
    fn() {
      getCanvas(this.pageRequestVo).then((res) => {
        console.log(res.data);
        this.totalElements = res.data.totalElements;
        //更改时间

        res.data.content.forEach((item) => {
          item.modifiedTime = dayjs
            .unix(item.modifiedTime)
            .format("YYYY-MM-DD HH:mm");
        });

        //创建日期
        res.data.content.forEach((item) => {
          item.createTime = dayjs
            .unix(item.createTime)
            .format("YYYY-MM-DD HH:mm");
        });
        let number = 1;

        res.data.content.map((item) => {
          return (item.number = number++);
        });

        this.tableData = [...res.data.content];
      });
    },

    //翻页

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);

      this.pageRequestVo.size = val;

      this.fn();
    },

    handleCurrentChange(val) {
      this.pageRequestVo.page = val;

      console.log(`当前页: ${val}`);

      this.fn();
    },
  },

  created() {
    this.fn();
  },
};
</script>

<style>
.block {
  display: flex;

  justify-content: flex-end;

  margin-right: 30px;
}

.el-pagination.is-background .el-pager li:not(.disabled).active {
  background: #7e3af2;
}

.mask-style {
  width: 70%;

  margin: 10px auto;
}
#newcanvas {
  position: absolute;
  right: 10px;
  top: 15px;
  color: #fff;
}
.surveyfile {
  display: flex;
  align-items: center;
  margin-left: 20px;
}
.surveyfile span {
  display: inline-block;
  width: 150px;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值