productBind.vue 5.3 KB

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