guns前後端分離商業版 -1-vue3實現表格編輯 幾個知識點說明 圖例 position-edit.vue position.vue

這裏僅僅是對錶格的編輯、添加進行說明

幾個知識點說明

guns前端文檔(開發者還未完成)

guns-v7使用第三方的框架EleAdminPro (vue3)

eleadmin使用Antd技術

圖例

position-edit.vue

<!-- 新增和編輯彈窗 -->
<template>
  <a-modal
    :width="800"
    :visible="visible"
    :confirm-loading="loading"
    :title="isUpdate ? '修改職位' : '新建職位'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible="updateVisible"
    @ok="save"
  >
    <a-form
      layout="horizontal"
      ref="form"
      :model="form"
      :rules="rules"
      :label-col="{ md: { span: 4 }, sm: { span: 24 } }"
      :wrapper-col="{ md: { span: 20 }, sm: { span: 24 } }"
    >
      <a-form-item label="職位名稱:" name="positionName">
        <a-input v-model:value="form.positionName" placeholder="請輸入職位名稱" allow-clear autocomplete="off" />
      </a-form-item>

      <a-form-item label="職位編碼:" name="positionCode">
        <a-input v-model:value="form.positionCode" placeholder="請輸入職位編碼" allow-clear autocomplete="off" />
      </a-form-item>

      <a-form-item label="職位排序:" name="positionSort">
        <a-input v-model:value="form.positionSort" placeholder="請輸入職位排序" allow-clear autocomplete="off" />
      </a-form-item>

      <a-form-item label="備註信息:" name="positionRemark">
        <a-input v-model:value="form.positionRemark" placeholder="請輸入備註信息" allow-clear autocomplete="off" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import { PositionApi } from '@/api/PositionApi';

export default {
  name: 'PositionEdit',
  emits: ['done', 'update:visible'],
  props: {
    // 彈窗是否打開
    visible: Boolean,
    // 修改回顯的數據
    data: Object
  },
  data() {
    return {
      // 表單數據
      form: Object.assign({}, this.data),
      // 表單驗證規則
      rules: {
        positionName: [{ required: true, message: '請輸入職位名稱', type: 'string', trigger: 'blur' }],
        positionCode: [{ required: true, message: '請輸入職位編碼', type: 'string', trigger: 'blur' }],
        positionSort: [{ required: true, message: '請輸入數字順序', type: 'string', trigger: 'blur' }]
      },
      // 提交狀態
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 角色列表
      roleList: [],
      // 組織機構樹列表
      orgList: [],
      // 職位列表
      positionList: []
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
      if (this.$refs.form) {
        this.$refs.form.clearValidate();
      }
    }
  },
  methods: {
    /**
     * 保存和編輯職務
     *
     * @author fengshuonan
     * @date 2021/4/8 13:33
     */
    async save() {
      // 校驗表單
      await this.$refs.form.validate();

      // 修改加載框爲正在加載
      this.loading = true;

      let result = null;

      // 執行編輯或修改
      if (this.isUpdate) {
        result = PositionApi.edit(this.form);
      } else {
        result = PositionApi.add(this.form);
      }
      result
        .then(result => {
          // 移除加載框
          this.loading = false;

          // 提示添加成功
          this.$message.success(result.message);

          // 如果是新增,則form表單置空
          if (!this.isUpdate) {
            this.form = {};
          }

          // 關閉彈框,通過控制visible的值,傳遞給父組件
          this.updateVisible(false);

          // 觸發父組件done事件
          this.$emit('done');
        })
        .catch(() => {
          this.loading = false;
        });
    },

    /**
     * 更新編輯界面的彈框是否顯示
     *
     * @param value true-顯示,false-隱藏
     * @author fengshuonan
     * @date 2021/4/7 11:00
     */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
};
</script>

<style scoped></style>

position.vue

調用參數傳遞


<template>
  <div class="ele-body">
    <!-- 搜索表單 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="職位名稱:">
              <a-input v-model:value.trim="where.positionName" placeholder="請輸入職位名稱" allow-clear />
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查詢</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="positionId"
          :datasource="url"
          :columns="columns"
          :where="where"
          v-model:selection="selection"
          :scroll="{ x: 'max-content' }"
        >
          <!-- table上邊工具欄 -->
          <template #toolbar>
            <a-space>
              <a-button type="primary" @click="openEdit()">
                <template #icon>
                  <plus-outlined />
                </template>
                <span>新建</span>
              </a-button>
              <a-button type="danger" @click="removeBatch()">
                <template #icon>
                  <delete-outlined />
                </template>
                <span>刪除</span>
              </a-button>
            </a-space>
          </template>

          <!-- table列表狀態欄 -->
          <!-- 1是激活,2是禁用 -->
          <template #state="{ record }">
            <a-switch :checked="record.statusFlag === 1" @change="checked => editState(checked, record)" />
          </template>

          <!-- table操作欄按鈕 -->
          <template #action="{ record }">
            <a-space>
              <a @click="openEdit(record)">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm title="確定要刪除此職務嗎?" @confirm="remove(record)">
                <a class="ele-text-danger">刪除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
  </div>

  <!-- 編輯彈窗 -->
  <position-edit v-model:visible="showEdit" :data="current" @done="reload" />
</template>

<script>
import { createVNode } from 'vue';
// 刪除,感嘆號,加號圖標
import { DeleteOutlined, ExclamationCircleOutlined, PlusOutlined } from '@ant-design/icons-vue';
import PositionEdit from './position-edit';
import { PositionApi } from '@/api/PositionApi';

export default {
  name: 'Position',
  components: {
    DeleteOutlined,
    PlusOutlined,
    PositionEdit
  },
  data() {
    return {
      // 表格數據接口
      url: '/hrPosition/page',
      // 表格列配置
      columns: [
        {
          key: 'index',
          width: 48,
          customRender: ({ index }) => this.$refs.table.tableIndex + index
        },
        {
          title: '職位名稱',
          dataIndex: 'positionName',
          width: 160,
          sorter: true
        },
        {
          title: '職位編碼',
          dataIndex: 'positionCode',
          width: 160,
          sorter: true
        },
        {
          title: '備註',
          dataIndex: 'positionRemark',
          width: 160,
          sorter: true
        },
        {
          title: '創建時間',
          dataIndex: 'createTime',
          width: 160,
          sorter: true
        },
        {
          title: '狀態',
          dataIndex: 'status',
          sorter: true,
          align: 'center',
          width: 160,
          slots: { customRender: 'state' }
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center',
          slots: { customRender: 'action' }
        }
      ],
      // 表格搜索條件
      where: {},
      // 表格選中數據
      selection: [],
      // 當前編輯數據
      current: null,
      // 是否顯示編輯彈窗
      showEdit: false
    };
  },
  methods: {
    /**
     * 搜索按鈕
     *
     * @author fengshuonan
     * @date 2021/4/2 17:03
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author fengshuonan
     * @date 2021/4/2 17:03
     */
    reset() {
      this.where = {};
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 刪除單個
     *
     * @author fengshuonan
     * @date 2021/4/2 17:03
     */
    async remove(row) {
      const result = await PositionApi.del({ positionId: row.positionId });
      this.$message.success(result.message);
      this.reload();
    },

    /**
     * 批量刪除
     *
     * @author fengshuonan
     * @date 2021/4/2 17:03
     */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('請至少選擇一條數據');
        return;
      }
      this.$confirm({
        title: '提示',
        content: '確定要刪除選中的職位嗎?',
        icon: createVNode(ExclamationCircleOutlined),
        maskClosable: true,
        onOk: async () => {
          let params = this.selection.map(d => d.positionId);
          const result = await PositionApi.batchDel({ positionIds: params });
          this.$message.success(result.message);
          this.reload();
        }
      });
    },

    /**
     * 打開編輯彈窗
     *
     * @author fengshuonan
     * @date 2021/4/2 17:03
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },

    /**
     * 修改職位狀態
     *
     * @author fengshuonan
     * @date 2021/4/2 17:04
     */
    async editState(checked, row) {
      const positionId = row.positionId;
      // 職位狀態:1-啓用,2-禁用
      const statusFlag = checked ? 1 : 2;
      const result = await PositionApi.updateStatus({ positionId, statusFlag });
      this.$message.success(result.message);
      row.statusFlag = statusFlag;
    }
  }
};
</script>

<style scoped>
/*搜索框與表格之間的間隙*/
.block-interval {
  margin-bottom: 10px;
}

/*重寫頂部搜索表單的樣式*/
.ant-form-item {
  margin-bottom: 0;
}
</style>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章