riku
2025-09-18 c1d2051abc8ca88cd07f0d7c56c0dbf8165d5c33
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
<template>
  <!-- <div v-if="modelValue"> -->
  <el-divider content-position="left">选择{{ label }}</el-divider>
  <div class="select-box">
    <div>
      <el-text size="small" type="info">当前选择</el-text>
    </div>
    <slot name="selected" :row="modelValue"></slot>
  </div>
  <!-- </div> -->
  <el-divider content-position="left">{{ label }}检索</el-divider>
  <FYSearchBar @search="search" :loading="loading">
    <template #options>
      <FYOptionText
        label=""
        :placeholder="placeholder"
        v-model:value="searchText"
        :width="searchTextWidth"
      ></FYOptionText>
    </template>
  </FYSearchBar>
  <div>
    <el-scrollbar v-if="dataList.length > 0" height="38vh" class="item-box">
      <el-space direction="vertical" alignment="start" fill>
        <div v-for="(item, index) in dataList" :key="index">
          <slot :row="item" :click="select"></slot>
        </div>
      </el-space>
    </el-scrollbar>
    <el-empty v-else description="无记录" />
  </div>
  <el-pagination
    v-if="pageShow && dataList.length > 0"
    size="small"
    ref="paginationRef"
    class="el-pagination"
    v-model:current-page="currentPage"
    v-model:page-size="pageSize"
    :page-sizes="pageSizes"
    :background="true"
    layout="total, sizes, prev, pager, next"
    :total="total"
  />
</template>
 
<script>
import { usePagination } from '@/composables/pagination';
 
/**
 * 信息检索及选择框
 */
export default {
  setup() {
    // 分页逻辑
    const { currentPage, pageSize, addPageEvent } = usePagination();
    return { currentPage, pageSize, addPageEvent };
  },
  props: {
    modelValue: Object,
    label: {
      type: String,
      default: '检索项'
    },
    placeholder: {
      type: String,
      default: '输入关键字检索'
    },
    searchTextWidth: {
      type: String,
      default: '200px'
    },
    // 是否显示分页
    pageShow: {
      type: Boolean,
      default: true
    },
    // 每页可选数量
    pageSizes: {
      type: Array,
      default: () => {
        return [10, 20, 50, 100];
      }
    },
    // 默认搜索文本
    defaultText: String
  },
  emits: ['search', 'update:modelValue'],
  data() {
    return {
      searchText: '',
      dataList: [],
      total: 0,
      loading: false
    };
  },
  watch: {
    defaultText: {
      handler(newVal) {
        if (newVal) {
          this.searchText = newVal;
          setTimeout(() => {
            this.search();
          }, 500);
        }
      },
      immediate: true
    }
  },
  methods: {
    search() {
      this.loading = true;
      const param = {
        text: this.searchText,
        page: this.currentPage,
        pageSize: this.pageSize
      };
      this.$emit('search', param, (res) => {
        if (res) {
          this.dataList = res.data;
          this.total = res.total ? res.total : 0;
        }
        this.loading = false;
      });
    },
    select(e) {
      this.$emit('update:modelValue', e);
    }
  },
  mounted() {
    this.addPageEvent(this.search);
  }
};
</script>
<style scoped>
.select-box {
  border: 1px solid var(--el-border-color);
  border-radius: var(--el-border-radius-base);
  padding: 0 8px;
}
.item-box {
  /* border: 1px solid var(--el-border-color);
  border-radius: var(--el-border-radius-base);
  margin-top: 20px; */
}
.el-pagination {
  /* background-color: var(--el-color-white); */
  border-top: 1px solid rgba(0, 0, 0, 0.096);
  /* background-color: aliceblue; */
}
</style>