productBind.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <div class="mutiple-bind-dialog-wrap">
  3. <el-dialog title="选择产品" v-model="isShowDialog" width="700px">
  4. <el-form ref="formRef">
  5. <el-form-item label="" prop="name">
  6. <el-input v-model="tableData.param.name" placeholder="请输入产品标识或名称" clearable style="width: 240px" @keyup.enter.native="getProductList" />
  7. <el-button style="margin-left: 20px;" type="primary" @click="getProductList()"><el-icon><ele-Search /></el-icon>查询</el-button>
  8. <el-button style="margin-left: 20px;" :disabled="!deviceKeyList.length" type="danger" @click="confirmBind()"><el-icon><ele-Finished /></el-icon>确定选择</el-button>
  9. </el-form-item>
  10. </el-form>
  11. <el-table ref="productTable" :data="tableData.data" style="width: 100%" @selection-change="handleSelectionChange" @select-all="selectAll" v-loading="tableData.loading">
  12. <el-table-column type="selection" width="55" align="center" />
  13. <el-table-column label="标识" prop="key" show-overflow-tooltip v-col="'key'" />
  14. <el-table-column label="名称" prop="name" show-overflow-tooltip v-col="'name'" />
  15. </el-table>
  16. <pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNum" v-model:limit="tableData.param.pageSize" @pagination="getProductList" />
  17. </el-dialog>
  18. </div>
  19. </template>
  20. <script lang="ts">
  21. import { toRefs, reactive, defineComponent, nextTick, getCurrentInstance, ref } from 'vue'
  22. import { ElMessageBox, ElMessage } from 'element-plus'
  23. import api from '/@/api/device'
  24. interface TableDataState {
  25. isShowDialog: boolean,
  26. productData: object[],
  27. deviceKeyList: string[];
  28. deviceNameList: string[];
  29. checkKeyList: string[];
  30. tableData: {
  31. data: []
  32. total: number
  33. loading: boolean
  34. param: {
  35. status: number
  36. pageNum: number
  37. pageSize: number
  38. name: string
  39. }
  40. },
  41. }
  42. export default defineComponent({
  43. name: 'DeviceBindDialog',
  44. setup(prop, { emit }) {
  45. const { proxy } = getCurrentInstance() as any;
  46. const productTable = ref();
  47. const state = reactive<TableDataState>({
  48. deviceKeyList: [],
  49. deviceNameList: [],
  50. isShowDialog: false,
  51. productData: [],
  52. checkKeyList: [],
  53. tableData: {
  54. data: [],
  55. total: 0,
  56. loading: false,
  57. param: {
  58. status: 1,
  59. pageNum: 1,
  60. pageSize: 10,
  61. name: '',
  62. },
  63. },
  64. })
  65. const getProductList = () => {
  66. state.isShowDialog = true;
  67. state.tableData.loading = true;
  68. api.product.getList(state.tableData.param).then((res: any) => {
  69. state.tableData.data = res.product;
  70. state.tableData.total = res.total;
  71. changeSelect();
  72. }).finally(() => (state.tableData.loading = false));
  73. };
  74. const openDialog = (checkKeyList: string[]) => {
  75. state.checkKeyList = checkKeyList;
  76. getProductList()
  77. };
  78. // 多选框选中数据
  79. const handleSelectionChange = (selection: any[]) => {
  80. state.deviceKeyList = selection.map((item) => item.key);
  81. state.deviceNameList = selection.map((item) => item.name);
  82. if (selection.length > 1) {
  83. const del_row = selection.shift();
  84. productTable.value.toggleRowSelection(del_row, false)
  85. }
  86. };
  87. const confirmBind = () => {
  88. let msg = '是否确定选择产品?';
  89. if (state.deviceKeyList.length === 0) {
  90. ElMessage.error('请选择要确定绑定的数据。');
  91. return;
  92. }
  93. ElMessageBox.confirm(msg, '提示', {
  94. confirmButtonText: '确认',
  95. cancelButtonText: '取消',
  96. type: 'warning',
  97. }).then(() => {
  98. emit('bindSuccess', state.deviceKeyList, state.deviceNameList)
  99. state.isShowDialog = false;
  100. })
  101. };
  102. const handleChange = () => {
  103. getProductList()
  104. }
  105. const changeSelect = () => {
  106. nextTick(() => {
  107. state.tableData.data.forEach((item: any) => {
  108. if (state.checkKeyList) {
  109. if (state.checkKeyList.includes(item.key)) {
  110. proxy.$refs.multipleTable.toggleRowSelection(item, true);
  111. }
  112. }
  113. });
  114. });
  115. };
  116. const selectAll = (selection: any[]) => {
  117. ElMessage.error('只可单选');
  118. productTable.value.clearSelection()
  119. return
  120. }
  121. return {
  122. openDialog,
  123. confirmBind,
  124. getProductList,
  125. handleSelectionChange,
  126. handleChange,
  127. selectAll,
  128. productTable,
  129. ...toRefs(state),
  130. }
  131. },
  132. })
  133. </script>