Vue+Element UI弹窗实现表格编辑

  1. 点击编辑按钮弹出Dialog
<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-dialog title="编辑用户"
               :visible.sync="dialogFormVisible"
               width="500px"
               top="200px">
        <el-form :model="form">
            <el-form-item label="序号"
                          :label-width="formLabelWidth">
                <el-input v-model="form.id"
                          auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="账号"
                          :label-width="formLabelWidth">
                <el-input v-model="form.account"
                          auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="姓名"
                          :label-width="formLabelWidth">
                <el-input v-model="form.username"
                          auto-complete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer"
             class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary"
             @click="handleSet(scope.$index, scope.row)">确 定</el-button>
        </div>
    </el-dialog>
</template>

  1. js如下
<script>
export default {
  data () {
    return {
      dialogFormVisible: false,//弹出层默认为关闭
      //弹出层中的表单
      form: {
        account: "",
        username: "",
        id: ""
      },
      formLabelWidth: '50px'
    }
  },
  methods: {
    handleEdit (index, row) {
      this.dialogFormVisible = true //开启弹出层
      //对弹出层中的表单赋值 
      this.form.account = row.account
      this.form.username = row.username
      this.form.id = row.id
    },
    handleSet (index, row) {
      var params = {
        account: this.form.account,
        username: this.form.username,
        id: row.id
      }
      editUser('/user/editUser', params).then(
        res => {
          if (res.code === 200) {
            this.$message("修改成功!")
          }
          else {
            this.$message(res.data)
          }
        }
      )
    }
}
</script>

  1. 时间格式化
    3.1 方法一
 <el-table-column label="修改时间"
                  prop="alter_time"
                  :formatter="dataFormat">
</el-table-column>

<script>
import moment from 'moment'
export default{
    data(){
        return {},
        methods:{
            dataFormat(row,cloumn){
                var date=row[cloumn.property]
                if(date==undefined){return ''};
                return moment(date).format("yyyy-MM-DD HH:mm:ss")
            }
        }
    }
}
</script>

 3.2 方法二
<el-form :model="form">
          <div class="form-header">
            <el-form-item v-if="form.steps != null">
              <div>
                <div style="flex: 1">实验名称:{{form.experimentName}}</div>
              </div>
              <div style="display: flex">
                <div style="flex: 1">实验开始时间:{{form.steps.startTime}}</div>
                <div style="flex: 1">实验结束时间:{{form.steps.endTime}}</div>
              </div>
              <div style="display: flex">
                <div style="flex: 1">实验得分:{{form.steps.score }}</div>
                <div style="flex: 1">实验用时:{{form.steps.timeUsed | getDuration}}</div>
              </div>
              </el-form-item>
            </div>
        </el-form>
// 过滤器
filters: {
    getDuration: function (second) {
      var days = Math.floor(second / 86400);
      var hours = Math.floor((second % 86400) / 3600);
      var minutes = Math.floor(((second % 86400) % 3600) / 60);
      var seconds = Math.floor(((second % 86400) % 3600) % 60);
      var duration = days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
      return duration;
    }
  },
Logo

前往低代码交流专区

更多推荐