trfficcodes.vue 8.65 KB
<template>
  <div class="contentBox">
    <div class="content">
      <div>
        <span class="selectBox">
          <el-select
            v-model="curCateUnid"
            placeholder="请选择"
            @change="cateChange"
            :popper-append-to-body="false"
          >
            <el-option
              v-for="(item, index) in catesData"
              :key="index"
              :value="item.cate_unid"
              :label="item.name"
            ></el-option>
          </el-select>
        </span>
        <span class="addbox">
          <el-button type="primary" @click="addcode">添加code</el-button>
        </span>
      </div>
      <div class="mt10">
        <el-table
          height="574"
          :data="tableData"
          stripe
          border
          style="width: 100%"
        >
          <el-table-column
            type="index"
            align="center"
            label="#"
            width="60"
          ></el-table-column>
          <el-table-column align="center" prop="code_unid" label="code_unid">
          </el-table-column>
          <el-table-column prop="code" align="center" label="code">
          </el-table-column>
          <el-table-column align="center" prop="name" label="名称">
          </el-table-column>
          <el-table-column align="center" prop="note" label="备用名称">
          </el-table-column>
          <el-table-column
            align="center"
            prop="active"
            label="状态"
            :formatter="activeFormatter"
          >
          </el-table-column>
          <el-table-column align="center" prop="operation" label="操作">
            <template slot-scope="scope">
              <el-tooltip
                content="编辑"
                placement="bottom"
                effect="light"
                :visible-arrow="false"
              >
                <span
                  class="icon-fanxing-xiugai editIcon"
                  @click="editCode(scope.$index, scope.row)"
                ></span>
              </el-tooltip>
              <el-tooltip
                content="删除"
                placement="bottom"
                effect="light"
                :visible-arrow="false"
              >
                <span
                  class="el-icon-delete  delIcon del-btn"
                  @click="deleteCode(scope.$index, scope.row)"
                ></span>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <div style="margin-top: 28px;" hidden>
          <el-pagination
            style="float: right;"
            background
            prev-text="上一页"
            next-text="下一页"
            :page-sizes="[20, 30, 50, 100, 200]"
            layout="total, prev, pager, next,sizes, jumper"
            :current-page="page"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :total="total"
          >
          </el-pagination>
          <div style="clear: both;"></div>
        </div>
      </div>
    </div>
    <el-dialog title="设置CODE" :visible.sync="detailVisible" width="400px">
      <el-form
        ref="trfficform"
        :model="codeData"
        label-position="left"
        label-width="80px"
      >
        <el-form-item
          label="类型"
          prop="catename"
          :rules="[
            { required: true, message: '类型不能为空!', trigger: 'blur' }
          ]"
        >
          <el-input v-model="codeData.catename" disabled></el-input>
        </el-form-item>
        <el-form-item
          label="CODE"
          prop="code"
          :rules="[
            { required: true, message: 'code不能为空!', trigger: 'blur' }
          ]"
        >
          <el-input v-model="codeData.code" :disabled="editdis"></el-input>
        </el-form-item>
        <el-form-item
          label="名称"
          prop="name"
          :rules="[
            { required: true, message: '名称不能为空!', trigger: 'blur' }
          ]"
        >
          <el-input v-model="codeData.name"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="codeData.note"></el-input>
        </el-form-item>
        <el-form-item
          label="启用"
          prop="active"
          :rules="[
            { required: true, message: '名称不能为空!', trigger: 'blur' }
          ]"
        >
          <el-select v-model="codeData.active" placeholder="请选择">
            <el-option label="开启" :value="true"></el-option>
            <el-option label="关闭" :value="false"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="detailVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="saveCode"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      detailData: [],
      total: 0,
      page: 1,
      curCateUnid: "",
      pageSize: 20,
      tableData: [],
      catesData: [],
      selectDevs: "",
      detailVisible: false,
      codeStatus: 0,
      codeData: {},
      editdis: false
    };
  },
  components: {},
  created() {
    this.getCates();
  },
  methods: {
    cateChange(cateunid) {
      let offset = (this.page - 1) * this.pageSize;
      let obj = {
        limit: this.pageSize,
        offset: offset
      };
      this.$api.codes
        .codes(obj, cateunid)
        .then(res => {
          this.tableData = res.list_data;
        })
        .catch(err => {});
    },
    addcode() {
      this.codeStatus = 0;
      this.detailVisible = true;
      this.codeData.catename = this.getCateName(this.curCateUnid);
      this.editdis = false;
    },
    editCode(index, row) {
      this.codeStatus = 1;
      this.detailVisible = true;
      this.codeData = row;
      this.editdis = true;
      this.codeData.catename = this.getCateName(this.curCateUnid);
    },
    getCateName(cateunid) {
      let name = "";
      this.catesData.forEach(ele => {
        if (ele.cate_unid === cateunid) {
          name = ele.name;
        }
      });
      return name;
    },
    getCates() {
      this.$api.codes.cates().then(res => {
        this.catesData = res.list_data;
      });
    },
    activeFormatter(row) {
      let text = "开启";
      if (row.active) {
        text = "开启";
      } else {
        text = "关闭";
      }
      return text;
    },
    saveCode() {
      this.$refs["trfficform"].validate(valid => {
        if (valid) {
          if (this.codeStatus === 0) {
            this.saveAddCode();
          }
          if (this.codeStatus === 1) {
            this.saveEditCode();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    saveAddCode() {
      let data = {
        code_unid: this.codeData.code_unid,
        code: this.codeData.code,
        name: this.codeData.name,
        note: this.codeData.note,
        active: this.codeData.active
      };
      this.$api.codes.addTrafficCode(data, this.curCateUnid).then(res => {
        console.log(res);
        this.detailVisible = false;
        this.cateChange(this.curCateUnid);
      });
    },
    saveEditCode() {
      let data = {
        code_unid: this.codeData.code_unid,
        code: this.codeData.code,
        name: this.codeData.name,
        note: this.codeData.note,
        active: this.codeData.active
      };
      this.$api.codes
        .editTrafficCode(data, this.curCateUnid, this.codeData.code_unid)
        .then(res => {
          console.log(res);
          this.detailVisible = false;
          this.cateChange(this.curCateUnid);
        });
    },
    deleteCode(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$api.codes
            .deleteTrafficCode(this.curCateUnid, row.code_unid)
            .then(res => {
              console.log(res);
              this.cateChange(this.curCateUnid);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleSizeChange(val) {
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.page = val;
    }
  }
};
</script>
<style lang="scss" scoped>
.addbox {
  float: right;
  margin-right: 10px;
}
.code-btn {
  margin-bottom: 10px;
  float: right;
  margin-right: 10px;
}
.del-btn {
  padding-left: 20px;
}
</style>