zxl
2025-03-25 6ae0fcef149ddbe614746023a58a3885b3ac4bde
src/components/VisibilityToolbar/index.vue
@@ -1,5 +1,5 @@
<template>
  <div class="top-right-btn" :style="style">
  <div class="top-right-btn">
    <el-row>
      <el-tooltip v-if="search" class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
        <el-button size="small" circle icon="el-icon-search" @click="toggleSearch"></el-button>
@@ -12,31 +12,38 @@
      </el-tooltip>
    </el-row>
    <el-drawer :visible.sync="table" title="信息显隐筛选" size="30%" append-to-body>
    <el-drawer
      :visible.sync="table"
      title="信息显隐筛选"
      size="30%"
      append-to-body
      :wrapperCLosable="false"
      :with-header="true">
      <div slot="header">
        <span>信息显隐筛选</span>
      </div>
      <div slot="default">
        <el-table :data="pagedColumns" style="width: 100%; height: 80%">
      <div slot="default" style="margin-left: 20px;margin-top: 20px">
        <el-table :row-key="row => { return row.id }" :data="getPage" style="width: 95%; height: 80%;;margin-bottom: 40px;" >
          <el-table-column prop="index" label="序号" width="80"></el-table-column>
          <el-table-column prop="label" label="数据名称"></el-table-column>
          <el-table-column prop="visible" label="是否显示">
          <el-table-column prop="visible" label="是否显示" >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.visible"
                class="ml-2"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="switchChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="date" label="排序">
          <el-table-column prop="sort" label="排序">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.serialNumber" :min="0" style="width: 120px" @change="sortChange(scope.row, $event)"></el-input-number>
              <el-input-number v-model="scope.row.sort" :min="0" style="width: 120px"
                               @change="sortChange(scope.row, $event)"></el-input-number>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          v-if="total > pageSize"
          :page-size="pageSize"
@@ -46,7 +53,8 @@
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
      <div slot="footer">
      <div style="position: absolute; bottom: 0; left: 0; right: 0; padding: 20px; text-align: right;">
        <el-button type="primary" @click="save" v-if="isAdmin()">保 存</el-button>
        <el-button @click="table = false">取 消</el-button>
        <el-button type="primary" @click="resetSort">重 置</el-button>
      </div>
@@ -55,6 +63,9 @@
</template>
<script>
import {saveHiddenList} from "@/api/projectEngineering/projectInfo";
import CircularJSON from 'circular-json'
export default {
  data() {
@@ -62,6 +73,8 @@
      table: false,
      currentPage: 1,
      columnRef: null,
      cpList: [],
      saveList:[],
    };
  },
  props: {
@@ -72,6 +85,10 @@
    columns: {
      type: Array,
      default: () => [],
    },
    hiddenTotal:{
      type: Number,
      default: 0,
    },
    search: {
      type: Boolean,
@@ -92,15 +109,17 @@
      return 10;
    },
    total() {
      return this.columns.length;
      return this.hiddenTotal;
    },
    pagedColumns() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.columns.slice(start, end);
    },
    getPage(){
      return this.columns;
    }
  },
  methods: {
    isAdmin(){
      return this.$auth.hasRole("admin")
    },
    toggleSearch() {
      this.$emit('update:showSearch', !this.showSearch);
    },
@@ -111,25 +130,113 @@
      this.table = true;
    },
    switchChange(row) {
      if (row.visible === true){
        row.display = "1"
      }else {
        row.display = "0"
      }
      if(this.saveList.length === 0){
        this.saveList.push(row)
      }else {
        let have = true;
        let index = this.saveList.findIndex(item => item.id === row.id);
        if (index !== -1){
            this.saveList[index] = row;
            have =false;
        }
        if (have){
          this.saveList.push(row)
        }
      }
      console
      this.$emit('update:columns', row);
    },
    handlePageChange(page) {
      this.currentPage = page;
      this.$emit('update:page', page);
      // this.currentPage = page;
      // this.queryParams.currentPage = page;
      // getHiddenList(this.queryParams).then(res =>{
      //   //转换
      //   res.data.forEach((item,index)=>{
      //     item.index = index + 1;
      //     item.key = index;
      //     item.serialNumber = index + 1;
      //     item.label = item.name;
      //     if(item.display === '0'){
      //       item.visible = false
      //     }else {
      //       item.visible = true;
      //     }
      //     if(item.columns ==='usedStatus' ){
      //       if(projectPhase !== '6') {
      //         item.visible = false
      //       }  else {
      //         item.visible = true
      //       }
      //     }
      //   });
      //   this.columns = res.data;
      // })
    },
    save(){
      //保存逻辑
      if (this.saveList.length !== 0){
        saveHiddenList(CircularJSON.stringify(this.saveList)).then(res =>{
          this.$modal.msgSuccess("保存成功");
          //刷新页面
          this.$emit('update:resetSort');
        });
      }
    },
    sortChange(row, val) {
      this.$emit('update:sort', { key: row.key, serialNumber: val });
      if (row.visible === true){
        row.display = "1"
      }else {
        row.display = "0"
      }
      if(this.saveList.length === 0){
        this.saveList.push(row)
      }else {
        let have = true;
        let index = this.saveList.findIndex(item => item.id === row.id);
        if (index !== -1){
          this.saveList[index] = row;
          have =false;
        }
        if (have){
          this.saveList.push(row)
        }
      }
      // // 创建一个新的columns数组,以避免直接修改原始数组
      // this.cpList = this.columns.map(item => ({ ...item })); // 使用解构来创建对象的浅拷贝
      //
      // // 更新指定项的serialNumber
      // this.cpList.forEach(item => {
      //   if (item.key ===  row.key) {
      //     item.serialNumber =  row.serialNumber;
      //   }
      // });
      //
      // // 根据serialNumber对newColumns进行排序
      // this.cpList = this.cpList.sort((a, b) => a.serialNumber - b.serialNumber);
      // // 触发事件通知父组件columns已更新(如果父组件需要知道这个变化)
      // this.$emit('update:sort', this.cpList);
      this.$emit('update:sort', row);
    },
    resetSort() {
      //重置保存集合
      this.currentPage = 1;
      this.saveList = [];
      this.$emit('update:resetSort');
    },
  },
  mounted() {
    this.columns.forEach((item, index) => {
      if (item.visible) {
        // 注意:这里假设columnRef应该是一个checkbox组件的引用,
        // 但在Vue 2中,我们可能需要另一种方式来管理checked状态,
        // 因为Vue 2没有ref的自动实例绑定,且这里的columnRef逻辑似乎不完整或错误。
        // 可能需要移除或重写这部分逻辑。
      }
    });
  },
@@ -157,4 +264,14 @@
.show-btn {
  margin-left: 12px;
}
.drawer-footer {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20px;
  background: #fff;
  border-top: 1px solid #e8e8e8;
  text-align: right;
}
</style>