wwl
2024-12-25 42f17b759cc69f44a8d200934117daaaa703d87d
src/components/Packagese/index.vue
@@ -50,6 +50,7 @@
          >
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        ref="elTable"
@@ -76,6 +77,7 @@
        <el-table-column label="内容" align="center" prop="pacRemark" />
        <el-table-column label="备注" align="center" prop="price" />
      </el-table>
      <div class="pag">
        <div class="pag1">
          <pagination
@@ -87,6 +89,7 @@
          />
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="handleOk">确 定</el-button>
@@ -94,30 +97,24 @@
    </el-dialog>
  </div>
</template>
<script>
import { getTjHyBgList } from "@/api/hosp/project";
export default {
  name: "Packagese",
  // props: {
  //   pacCode: {
  //         type: String,
  //     }
  // },
  props: {
    selectedId: {
      type: String,
      default: "",  // 父组件传递的选中ID
    },
  },
  data() {
    return {
      open: false,
      openone: false,
      // 弹出层标题
      title: "",
      dataList: [],
      // 遮罩层
      loading: false,
      form: {
        desc: "",
      },
      queryParams: {
        pid: "",
        pname: "",
@@ -126,73 +123,106 @@
        page: 1,
        pageSize: 10,
      },
      total: 0,
      list: [],
      fList: {},
    };
  },
  watch: {
    // 'row'(val, newVla) {
    //     console.log(val,newVla)
    //     this.fList = val
    //     console.log(this.fList);
    // }
  },
  mounted() {
    // this.getList()
    // 监听父组件传递的 selectedId
    selectedId(newVal) {
      // 如果 selectedId 为空,清空所有选中的项
      if (!newVal) {
        this.clearSelection();
      } else {
        this.handleSelectionEcho(newVal);
      }
    },
  },
  created() {
    // this.getList()
  },
  methods: {
    // 获取数据
    getAllList() {
      this.loading = true;
      this.openone = true;
      getTjHyBgList(this.queryParams).then((res) => {
        this.dataList = res.data.list;
        this.total = res.data.total;
        this.loading = false;
        // 在获取数据后回显选中项
        this.handleSelectionEcho(this.selectedId);
      });
    },
    // getList() {
    //   this.loading = true;
    //   this.openone = false;
    //   Packagestlist(this.queryParams).then((res) => {
    //     this.dataList = res.rows;
    //     this.loading = false;
    //   });
    // },
    handleQuery() {
      this.getAllList();
    // 根据 selectedId 查找对应的项并回显选择
    handleSelectionEcho(selectedId) {
      if (this.dataList.length > 0 && selectedId) {
        const selectedItem = this.dataList.find(
          (item) => item.pacCode === selectedId
        );
        if (selectedItem) {
          this.$nextTick(() => {
            this.$refs.elTable.clearSelection();  // 清除之前的选择
            this.$refs.elTable.toggleRowSelection(selectedItem, true); // 选中对应项
            this.list = [selectedItem];  // 设置选中的项
          });
        }
      }
    },
    // 清空所有选中的项
    clearSelection() {
      this.$nextTick(() => {
        this.$refs.elTable.clearSelection();  // 清空所有选中的项
        this.list = [];  // 清空选中的列表
      });
    },
    // 处理选择变化
    handleSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.elTable.clearSelection(); // 清空所有选择
        this.$refs.elTable.toggleRowSelection(selection.pop()); // 设置选择项
        // 如果选择了多行,保留最后一行
        this.$refs.elTable.clearSelection();  // 清空所有选择
        this.$refs.elTable.toggleRowSelection(selection[selection.length - 1]);  // 重新选择最后一项
      }
      this.list = selection;
      console.log(this.list);
      // 获取最后选择的行
      this.list = [selection[selection.length - 1]];
    },
    // 确定选择
    handleOk() {
      this.open = false;
      if (this.list.length === 1) {
      this.queryParams = {
        pid: "",
        pname: "",
        pym: "",
        checktype: "deptdict",
        page: 1,
        pageSize: 10,
      };
      if (this.list.length > 0) {
        const pac = [
         {
          pacCode: this.list[0].pacCode,
          pacName: this.list[0].pacName
         }
      ];
          {
            pacCode: this.list[this.list.length - 1].pacCode,
            pacName: this.list[this.list.length - 1].pacName,
          },
        ];
        this.$emit("add", pac);
      } else {
        this.$message.warning("请选择一个项目");
      }
    },
    // 触发查询
    handleQuery() {
      this.getAllList();
    },
  },
};
</script>
<style scoped>
.pag {
  width: 100%;
@@ -201,7 +231,6 @@
}
.pag1 {
  width: 30%;
  width: 60%;
}
</style>