qx
2025-04-09 93a6f70b8eb631c6c1faab8f9e8a734669e49e6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="编码" prop="pid">
          <el-input
            v-model="queryParams.pid"
            placeholder="请输入编码"
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 120px"
          />
        </el-form-item>
        <el-form-item label="名称" prop="pname">
          <el-input
            v-model="queryParams.pname"
            placeholder="请输入名称"
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 130px"
          />
        </el-form-item>
        <el-form-item label="内容" prop="pym">
          <el-input
            v-model="queryParams.pym"
            placeholder="请输入内容"
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 130px"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
      
      <el-table
        :data="dataList"
        ref="elTable"
        v-loading="loading"
        @selection-change="handleSelectionChange"
        border
        height="320px"
      >
        <el-table-column type="selection" width="40" align="center" />
        <el-table-column label="序号" prop="xh" align="center" />
        <el-table-column label="编码" align="center" prop="pacCode" />
        <el-table-column
          label="名称"
          align="center"
          prop="pacName"
          width="100px"
        />
        <el-table-column
          label="拼音码"
          align="center"
          prop="detail"
          width="100px"
        />
        <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
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.page"
            :limit.sync="queryParams.pageSize"
            @pagination="getAllList"
          />
        </div>
      </div>
 
      <span slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="handleOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
 
<script>
import { getTjHyBgList } from "@/api/hosp/project";
 
export default {
  name: "Packagese",
  props: {
    selectedId: {
      type: String,
      default: "",  // 父组件传递的选中ID
    },
  },
  data() {
    return {
      open: false,
      title: "",
      dataList: [],
      loading: false,
      queryParams: {
        pid: "",
        pname: "",
        pym: "",
        checktype: "deptdict",
        page: 1,
        pageSize: 10,
      },
      total: 0,
      list: [],
    };
  },
  
  watch: {
    // 监听父组件传递的 selectedId
    selectedId(newVal) {
      // 如果 selectedId 为空,清空所有选中的项
      if (!newVal) {
        this.clearSelection();
      } else {
        this.handleSelectionEcho(newVal);
      }
    },
  },
 
  methods: {
    // 获取数据
    getAllList() {
      this.loading = true;
      getTjHyBgList(this.queryParams).then((res) => {
        this.dataList = res.data.list;
        this.total = res.data.total;
        this.loading = false;
        
        // 在获取数据后回显选中项
        this.handleSelectionEcho(this.selectedId);
      });
    },
 
    // 根据 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[selection.length - 1]);  // 重新选择最后一项
      }
 
      // 获取最后选择的行
      this.list = [selection[selection.length - 1]];
    },
 
    // 确定选择
    handleOk() {
      this.open = false;
      this.queryParams = {
        pid: "",
        pname: "",
        pym: "",
        checktype: "deptdict",
        page: 1,
        pageSize: 10,
      };
 
      if (this.list.length > 0) {
        const pac = [
          {
            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%;
  display: flex;
  justify-content: center;
}
 
.pag1 {
  width: 60%;
}
</style>