supplier.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <script>
  2. import { initParams } from "./utils/init-something";
  3. import { list } from "./api/index";
  4. export default {
  5. name: "SupplierInputDialog",
  6. props: ["title", "value"],
  7. components: {},
  8. data() {
  9. const columns = [
  10. {
  11. key: "code",
  12. title: "供应商编码",
  13. type: "Input",
  14. search: true,
  15. },
  16. {
  17. key: "name",
  18. title: "供应商名称",
  19. type: "Input",
  20. search: true,
  21. },
  22. ];
  23. const initTableColumns = () => columns.filter((column) => column.key);
  24. const initSearchColumns = () => columns.filter((column) => column.search);
  25. return {
  26. // global
  27. size: "mini",
  28. width: "50%",
  29. page: { pageNum: 1, pageSize: 25, total: 0 },
  30. pageSizes: [25, 50],
  31. layout: "prev, pager, next, jumper",
  32. api: "puSupplier",
  33. showKey: "name",
  34. // dialog
  35. visible: false,
  36. loading: false,
  37. // search
  38. searchColumns: initSearchColumns(),
  39. params: initParams(initSearchColumns()),
  40. // table
  41. tableColumns: initTableColumns(),
  42. data: [],
  43. currentData: null,
  44. };
  45. },
  46. computed: {},
  47. watch: {},
  48. methods: {
  49. // set dialog visible
  50. setVisible(prop) {
  51. this.visible = prop;
  52. },
  53. // do something before dialog open
  54. beforeOpen() {
  55. const { value } = this.$props;
  56. this.params[this.showKey] = value;
  57. this.fetchList(this.params, this.page);
  58. },
  59. // fetch table data
  60. async fetchList(prop, page) {
  61. try {
  62. this.loading = true;
  63. const { pageNum, pageSize } = page;
  64. const { code, msg, rows, total } = await list(this.api, {
  65. ...prop,
  66. pageNum,
  67. pageSize,
  68. });
  69. if (code === 200) {
  70. this.data = rows;
  71. this.page.total = total;
  72. this.$notify.success({ title: msg });
  73. } else {
  74. this.$notify.warning({ title: msg });
  75. }
  76. } catch (err) {
  77. this.$notify.error({ title: "error", message: err });
  78. } finally {
  79. this.loading = false;
  80. this.setCurrentData(
  81. this.data.find(
  82. (column) => column[this.showKey] === this.currentData[this.showKey]
  83. )
  84. );
  85. }
  86. },
  87. // setting up to fetch table data
  88. queryList() {
  89. this.fetchList(this.params, this.page);
  90. },
  91. // reset to fetch table data
  92. resetList() {
  93. this.page.pageNum = 1;
  94. this.params = initParams(this.searchColumns);
  95. this.fetchList(this.params, this.page);
  96. },
  97. // size change to fetch table data
  98. pageSizeChange(prop) {
  99. this.page.pageSize = prop;
  100. this.fetchList(this.params, this.page);
  101. },
  102. // number change to fetch table data
  103. pageNumberChange(prop) {
  104. this.page.pageNum = prop;
  105. this.fetchList(this.params, this.page);
  106. },
  107. // select row data
  108. selectCurrentData(row) {
  109. this.currentData = row;
  110. },
  111. // set row data highlight
  112. setCurrentData(row) {
  113. this.$refs.singleTable.setCurrentRow(row);
  114. },
  115. //
  116. confirm() {
  117. if (this.currentData) {
  118. this.$emit("confirm", this.currentData);
  119. }
  120. this.setVisible(false);
  121. },
  122. },
  123. created() {},
  124. mounted() {},
  125. destroyed() {},
  126. };
  127. </script>
  128. <template>
  129. <el-dialog
  130. :title="title"
  131. :visible.sync="visible"
  132. :width="width"
  133. append-to-body
  134. @open="beforeOpen"
  135. >
  136. <el-form :size="size" :inline="true" :model="params">
  137. <el-form-item
  138. v-for="(column, index) in searchColumns"
  139. :key="index"
  140. :label="column.title"
  141. >
  142. <el-input v-model="params[column.key]" @change="queryList"> </el-input>
  143. </el-form-item>
  144. <el-form-item>
  145. <el-button icon="el-icon-refresh" @click="resetList"></el-button>
  146. </el-form-item>
  147. </el-form>
  148. <el-table
  149. v-loading="loading"
  150. :data="data"
  151. :size="size"
  152. ref="singleTable"
  153. height="45vh"
  154. highlight-current-row
  155. style="width: 100%; margin-bottom: 20px"
  156. @row-click="selectCurrentData"
  157. >
  158. <el-table-column
  159. v-for="(column, index) in tableColumns"
  160. :key="index"
  161. :prop="column.key"
  162. :label="column.title"
  163. :width="column.width"
  164. show-overflow-tooltip
  165. >
  166. </el-table-column>
  167. </el-table>
  168. <div
  169. style="display: flex; justify-content: space-between; align-items: center"
  170. >
  171. <p>
  172. <span style="font-size: 12px">已选择 :</span>
  173. <el-tag v-if="currentData" :size="size">{{
  174. currentData[showKey]
  175. }}</el-tag>
  176. <span v-else>无</span>
  177. </p>
  178. <el-pagination
  179. :layout="layout"
  180. :total="page.total"
  181. :page-sizes="pageSizes"
  182. :page-size="page.pageSize"
  183. :current-page="page.pageNum"
  184. :small="size === 'mini'"
  185. background
  186. @size-change="pageSizeChange"
  187. @current-change="pageNumberChange"
  188. >
  189. </el-pagination>
  190. </div>
  191. <div style="margin-top: 20px; text-align: right">
  192. <el-button :size="size" @click="visible = false">取 消</el-button>
  193. <el-button :size="size" type="primary" @click="confirm">确 定</el-button>
  194. </div>
  195. </el-dialog>
  196. </template>
  197. <style scoped></style>