itemChoose.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <div>
  3. <!-- 数据项选择 -->
  4. <el-dialog v-if="visible" :title="title" width="30%" custom-class="dialog-med" top="10vh" center :before-close="dialogClose"
  5. :visible.sync="visible" :close-on-click-modal="false" :append-to-body="true">
  6. <el-form ref="itemGroupForm" :model="itemGroupForm" :rules="groupRules" label-width="80px">
  7. <el-form-item label="组名称" prop="groupName">
  8. <el-input v-model="itemGroupForm.groupName" type="text" auto-complete="off" maxlength="20"
  9. placeholder="请输入组名称"></el-input>
  10. </el-form-item>
  11. <el-form-item label="备注" prop="groupDescribe">
  12. <el-input type="textarea" v-model="itemGroupForm.groupDescribe" placeholder="请输入备注"
  13. maxlength="100"></el-input>
  14. </el-form-item>
  15. <el-divider content-position="left">请选择数据源</el-divider>
  16. <div class="cy-line">
  17. <el-form-item prop="dataSourceId">
  18. <el-select v-model="itemGroupForm.dataSourceId" filterable placeholder="请选择数据源"
  19. @change="dataSourceChange" style="width: calc(100% - 110px)">
  20. <el-option v-for="dict in dataSourceList" :key="dict.id" :label="dict.dataSourceName"
  21. :value="dict.id"></el-option>
  22. </el-select>
  23. <el-button size="mini" @click="addDataItem"
  24. style="float: right; width: 100px; margin-top: 4px">添加数据项</el-button>
  25. </el-form-item>
  26. <el-tag size="mini" v-if="itemGroupForm.itemList && itemGroupForm.itemList.length > 0"
  27. class="cy-item-tag">
  28. 已选择({{ itemGroupForm.itemList.length }})项
  29. </el-tag>
  30. </div>
  31. </el-form>
  32. <span slot="footer">
  33. <el-button type="primary" @click="saveOrUpdateItemGroup">保存</el-button>
  34. <el-button @click="dialogClose">取消</el-button>
  35. </span>
  36. </el-dialog>
  37. <!-- 数据项选择树 -->
  38. <ItemChooseTree ref="itemChooseTree" @saveItemChoose="saveItemChoose"></ItemChooseTree>
  39. </div>
  40. </template>
  41. <script>
  42. import { showLoading } from '@/utils/cqcy'
  43. import ItemChooseTree from './itemChooseTree.vue'
  44. import {
  45. getNextAllItem, getDriverItemBySouceId,
  46. saveOrUpdateItemGroup, addItemList
  47. } from '@/api/source/itemGroup';
  48. export default {
  49. components: {
  50. ItemChooseTree
  51. },
  52. data() {
  53. return {
  54. visible: false,
  55. title: '新增数据组',
  56. itemGroupForm: {
  57. id: null,
  58. groupName: "",
  59. groupDescribe: "",
  60. dataSourceId: null,
  61. itemList: [],
  62. },
  63. dataSourceList: [],
  64. groupRules: {
  65. groupName: [
  66. { required: true, message: "组名称不能为空", trigger: "blur" },
  67. {
  68. min: 2,
  69. max: 20,
  70. message: "组名称长度必须介于 2 和 20 之间",
  71. trigger: "blur",
  72. },
  73. ],
  74. },
  75. }
  76. },
  77. methods: {
  78. /** 关闭弹出层 */
  79. dialogClose() {
  80. if (this.$refs['itemGroupForm']) this.$refs['itemGroupForm'].resetFields()
  81. this.itemGroupForm = {
  82. id: null,
  83. groupName: "",
  84. groupDescribe: "",
  85. dataSourceId: null,
  86. itemList: [],
  87. },
  88. this.visible = false
  89. },
  90. /** 选择数据源改变事件 */
  91. dataSourceChange() {
  92. this.itemGroupForm.itemList = [];
  93. },
  94. /** 添加数据项 */
  95. addDataItem() {
  96. if (!this.itemGroupForm.groupName) {
  97. this.$message({
  98. message: '请输入数据组名称',
  99. type: 'warning'
  100. })
  101. return
  102. }
  103. if (!this.itemGroupForm.dataSourceId) {
  104. this.$message({
  105. message: '请选择数据源',
  106. type: 'warning'
  107. })
  108. return
  109. }
  110. const loading = showLoading(this, "加载中,请稍候···");
  111. //获取根节点的树信息
  112. getNextAllItem(this.itemGroupForm.dataSourceId, null).then((res) => {
  113. loading.close();
  114. let data = res.data;
  115. let leaves = [];
  116. if (data.leaves && data.leaves.length > 0) {
  117. data.leaves.forEach((t) => {
  118. t.label = t.itemName;
  119. t.value = t.itemReadName;
  120. t.checked = false;
  121. leaves.push(t);
  122. });
  123. }
  124. this.$refs.itemChooseTree.leavesList = leaves;
  125. this.$refs.itemChooseTree.leavesListF = JSON.parse(JSON.stringify(leaves));
  126. let items = [];
  127. if (data.label && data.label.length > 0) {
  128. data.label.forEach((t) => {
  129. items.push({
  130. dataSourceId: this.itemGroupForm.dataSourceId,
  131. label: t.itemName,
  132. leaf: false,
  133. children: [],
  134. });
  135. });
  136. }
  137. // 区分驱动返回数据类型
  138. this.$refs.itemChooseTree.lableListByTree = items;
  139. this.$refs.itemChooseTree.lableListByTreeF = JSON.parse(
  140. JSON.stringify(items)
  141. );
  142. this.$refs.itemChooseTree.title = '选择数据项'
  143. this.$refs.itemChooseTree.visible = true
  144. this.$refs.itemChooseTree.isSelectAllItem = false
  145. this.$refs.itemChooseTree.itemList = this.itemGroupForm.itemList
  146. let arr = [];
  147. //将原来的数据项加载到,右侧作为已选择的
  148. for (let i = 0; i < this.itemGroupForm.itemList.length; i++) {
  149. let temp = {
  150. nodeIdentifier: null,
  151. dataType: null,
  152. label: this.itemGroupForm.itemList[i].itemName,
  153. nodeIndex: this.itemGroupForm.itemList[i].nodeIndex,
  154. itemName: this.itemGroupForm.itemList[i].itemName,
  155. itemReadName: this.itemGroupForm.itemList[i].itemReadName,
  156. dataModelId: this.itemGroupForm.itemList[i].dataModelId,
  157. value: this.itemGroupForm.itemList[i].itemReadName,
  158. checked: false,
  159. };
  160. arr.push(temp)
  161. }
  162. this.$refs.itemChooseTree.chooseItemDataListByTree = arr
  163. this.getDriverItemBySouceId(this.itemGroupForm.dataSourceId)
  164. }).catch((e) => {
  165. loading.close();
  166. this.$message({
  167. message: e,
  168. type: 'warning'
  169. })
  170. });
  171. },
  172. /** 查询改数据源下的驱动数据项信息 */
  173. getDriverItemBySouceId(id) {
  174. this.$refs.itemChooseTree.queryParams.id = id;
  175. getDriverItemBySouceId(id).then((res) => {
  176. if (res.code === 200) {
  177. this.$refs.itemChooseTree.DriverItemData = res.data;
  178. }
  179. });
  180. },
  181. /** 给数据组的数据项赋值 */
  182. saveItemChoose(datas) {
  183. this.itemGroupForm.itemList = datas
  184. //如果界面没有弹出,则说明此界面是从添加数据项进来的,不是新增修改数据组进来的,则默认提交
  185. if (!this.visible) {
  186. this.addItemList()
  187. }
  188. },
  189. /** 保存或修改数据组 */
  190. saveOrUpdateItemGroup() {
  191. this.$refs["itemGroupForm"].validate((valid) => {
  192. if (this.checkParams() && valid) {
  193. // 参数封装
  194. let params = JSON.parse(JSON.stringify(this.itemGroupForm));
  195. let type = ''
  196. if (params.id) {
  197. type = '修改'
  198. } else {
  199. type = '保存'
  200. }
  201. const loading = showLoading(this, type + "中,请稍候···");
  202. saveOrUpdateItemGroup(params).then((res) => {
  203. loading.close();
  204. if (res.data) {
  205. this.$message({
  206. message: type + "成功!",
  207. type: 'success'
  208. })
  209. this.dialogClose()
  210. this.$emit('getAllItemGroup')
  211. return;
  212. }
  213. this.$message({
  214. message: type + "失败!",
  215. type: 'warning'
  216. })
  217. }).catch((e) => {
  218. loading.close();
  219. this.$message({
  220. message: e,
  221. type: 'warning'
  222. })
  223. });
  224. }
  225. });
  226. },
  227. /** 添加数据项 */
  228. addItemList() {
  229. let params = {
  230. id: this.itemGroupForm.id,
  231. itemList: this.itemGroupForm.itemList,
  232. }
  233. addItemList(params).then((res) => {
  234. if (res.data) {
  235. this.$message({
  236. message: "添加数据项成功!",
  237. type: 'success'
  238. })
  239. this.$emit('getItemGroupById', this.itemGroupForm.id)
  240. return;
  241. }
  242. }).catch((e) => {
  243. this.$message({
  244. message: e,
  245. type: 'warning'
  246. })
  247. });
  248. },
  249. /** 参数检查 */
  250. checkParams() {
  251. if (!this.itemGroupForm.groupName) {
  252. this.$message({
  253. message: '组名称不能为空!',
  254. type: 'warning'
  255. })
  256. return false;
  257. }
  258. if (this.itemGroupForm.itemList.length == 0) {
  259. this.$message({
  260. message: '请添加数据项!',
  261. type: 'warning'
  262. })
  263. return false;
  264. }
  265. if (!this.itemGroupForm.dataSourceId) {
  266. this.$message({
  267. message: '请选择数据源!',
  268. type: 'warning'
  269. })
  270. return false;
  271. }
  272. return true;
  273. }
  274. }
  275. }
  276. </script>