AttributeEditPanel.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <template>
  2. <div class="container-fluid">
  3. <div class="row">
  4. <div class="btn-group" role="group">
  5. <button type="button" class="btn btn-default" @click="back">返回</button>
  6. <button type="button" class="btn btn-success" @click="refresh">刷新</button>
  7. <button type="button" class="btn btn-primary" @click="saveAttribute">保存</button>
  8. <button type="button" class="btn btn-info" @click="addAttribute">添加属性</button>
  9. </div>
  10. </div>
  11. <div v-for="attribute in attributes" :key="attribute.name" class="row">
  12. <div class="form-inline">
  13. <div class="form-group">
  14. <label>属性名称</label>
  15. <input v-model="attribute.name" autocomplete="off" type="text" class="form-control" />
  16. </div>
  17. <div class="checkbox">
  18. <label>
  19. <input v-model="attribute.isRequired" autocomplete="off" type="checkbox" /> 必填项
  20. </label>
  21. </div>
  22. <div class="checkbox">
  23. <label>
  24. <input v-model="attribute.isInput" autocomplete="off" type="checkbox" /> 输入项
  25. </label>
  26. </div>
  27. <div class="checkbox">
  28. <label>
  29. <input v-model="attribute.isMutipleSelect" autocomplete="off" type="checkbox" /> 多选
  30. </label>
  31. </div>
  32. <div class="checkbox">
  33. <label>
  34. <input v-model="attribute.isNumber" autocomplete="off" type="checkbox" /> 数字
  35. </label>
  36. </div>
  37. <button type="button" class="btn btn-danger" @click="deleteAttribute(attribute)">删除</button>
  38. <button type="button" class="btn btn-info" @click="addAttributeName(attribute)">添加属性值</button>
  39. </div>
  40. <div v-for="attributeName in attribute.attributeNameDtoList" :key="attributeName.id" class="form-inline form-attributeName">
  41. <div class="form-group">
  42. <label>属性值</label>
  43. <input v-if="attribute.isNumber" v-model="attributeName.nameDec" autocomplete="off" type="number" class="form-control" />
  44. <input v-else v-model="attributeName.nameStr" autocomplete="off" type="text" class="form-control" />
  45. </div>
  46. <button type="button" class="btn btn-danger" @click="deleteAttributeName(attribute, attributeName)">删除</button>
  47. </div>
  48. </div>
  49. </div>
  50. </template>
  51. <script>
  52. import Common from '../../common/Common.js';
  53. import { Notify, Uuid } from 'pc-component-v3';
  54. export default {
  55. components: {
  56. },
  57. props: [],
  58. data: function() {
  59. return {
  60. modelData: {},
  61. attributeKey: null,
  62. attributes: [],
  63. };
  64. },
  65. mounted: function() {
  66. var _self = this;
  67. var uuid = this.$route.params.uuid;
  68. var cacheData = localStorage.getItem(uuid);
  69. if (cacheData != undefined && cacheData.length > 0) {
  70. cacheData = JSON.parse(cacheData);
  71. _self.modelData = cacheData.modelData;
  72. }
  73. console.log(_self.modelData);
  74. _self.initData();
  75. },
  76. methods: {
  77. /**
  78. * 初始化数据
  79. * @return {void}
  80. */
  81. initData: function() {
  82. var _self = this;
  83. if (
  84. _self.modelData.data.attributeKey != undefined &&
  85. _self.modelData.data.attributeKey.displayValue != undefined
  86. ) {
  87. var displayValue =
  88. _self.modelData.data.attributeKey.displayValue;
  89. if (displayValue.length > 0) {
  90. _self.attributeKey = displayValue[0];
  91. _self.getAttribute();
  92. }
  93. }
  94. },
  95. /**
  96. * 获取属性
  97. * @return {void}
  98. */
  99. getAttribute: function() {
  100. var _self = this;
  101. $.ajax({
  102. url: Common.getApiURL('attributeResource/getAttribute'),
  103. type: 'get',
  104. dataType: 'json',
  105. beforeSend: function(request) {
  106. Common.addTokenToRequest(request);
  107. },
  108. // contentType : "application/json",
  109. data: {
  110. classKey: _self.attributeKey,
  111. },
  112. success: function(data) {
  113. console.log('=========>');
  114. console.log(data);
  115. _self.attributes = data;
  116. },
  117. error: function(XMLHttpRequest, textStatus, errorThrown) {
  118. Common.processException(
  119. XMLHttpRequest,
  120. textStatus,
  121. errorThrown,
  122. );
  123. },
  124. });
  125. },
  126. /**
  127. * 添加属性
  128. */
  129. addAttribute: function() {
  130. var _self = this;
  131. var newAttribute = _self.generateAttribute();
  132. _self.attributes.push(newAttribute);
  133. },
  134. /**
  135. * 保存属性
  136. * @return {void}
  137. */
  138. saveAttribute: function() {
  139. var _self = this;
  140. console.log(_self.attributes);
  141. $.ajax({
  142. url: Common.getApiURL('attributeResource/saveAttribute'),
  143. type: 'post',
  144. beforeSend: function(request) {
  145. Common.addTokenToRequest(request);
  146. },
  147. contentType: 'application/json',
  148. data: JSON.stringify(_self.attributes),
  149. success: function(data) {
  150. Notify.success('成功', '保存成功', true);
  151. },
  152. error: function(XMLHttpRequest, textStatus, errorThrown) {
  153. Common.processException(
  154. XMLHttpRequest,
  155. textStatus,
  156. errorThrown,
  157. );
  158. },
  159. });
  160. },
  161. /**
  162. * 刷新
  163. * @return {void}
  164. */
  165. refresh: function() {
  166. var _self = this;
  167. _self.attributes = [];
  168. _self.getAttribute();
  169. },
  170. /**
  171. * 删除属性
  172. * @param {Object} attribute 属性
  173. * @return {void}
  174. */
  175. deleteAttribute: function(attribute) {
  176. var _self = this;
  177. if (attribute.id != undefined && attribute.id > 0) {
  178. Notify.show({
  179. title: _self.$t('lang.AttributeEditPane.deleteConfirmation'),
  180. message:
  181. _self.$t('lang.AttributeEditPane.someWhatThree') +
  182. attribute.name +
  183. _self.$t('lang.AttributeEditPane.someWhatFour'),
  184. buttons: [
  185. {
  186. label: _self.$t('lang.AttributeEditPane.determine'),
  187. cssClass: 'btn-primary',
  188. action: function(dialogItself) {
  189. dialogItself.close();
  190. $.ajax({
  191. url: Common.getApiURL(
  192. 'attributeResource/deleteAttribute',
  193. ),
  194. type: 'get',
  195. dataType: 'json',
  196. beforeSend: function(request) {
  197. Common.addTokenToRequest(request);
  198. },
  199. data: {
  200. attributeId: attribute.id,
  201. },
  202. success: function(data) {
  203. var index = _self.attributes.indexOf(
  204. attribute,
  205. );
  206. if (index >= 0) {
  207. _self.attributes.splice(index, 1);
  208. }
  209. },
  210. error: function(
  211. XMLHttpRequest,
  212. textStatus,
  213. errorThrown,
  214. ) {
  215. Common.processException(
  216. XMLHttpRequest,
  217. textStatus,
  218. errorThrown,
  219. );
  220. },
  221. });
  222. },
  223. },
  224. {
  225. label: _self.$t('lang.AttributeEditPane.cancel'),
  226. action: function(dialogItself) {
  227. dialogItself.close();
  228. },
  229. },
  230. ],
  231. });
  232. } else {
  233. var index = _self.attributes.indexOf(attribute);
  234. if (index >= 0) {
  235. _self.attributes.splice(index, 1);
  236. }
  237. }
  238. },
  239. /**
  240. * 添加属性名称
  241. * @param {void} attribute
  242. */
  243. addAttributeName: function(attribute) {
  244. var _self = this;
  245. var newAttributeName = _self.generateAttributeName();
  246. if (attribute.id != undefined && attribute.id > 0) {
  247. newAttributeName.attributeId = attribute.id;
  248. }
  249. attribute.attributeNameDtoList.push(newAttributeName);
  250. },
  251. /**
  252. * 删除属性名称
  253. * @param {Object} attribute 属性
  254. * @param {Object} attributeName 属性名称
  255. * @return {void}
  256. */
  257. deleteAttributeName: function(attribute, attributeName) {
  258. var _self = this;
  259. if (attributeName.id != undefined && attributeName.id > 0) {
  260. Notify.show({
  261. title:_self.$t('lang.AttributeEditPane.deleteConfirmation'),
  262. message:
  263. _self.$t('lang.AttributeEditPane.someWhatOne') +
  264. attributeName.name +
  265. _self.$t('lang.AttributeEditPane.someWhatTwo'),
  266. buttons: [
  267. {
  268. label: _self.$t('lang.AttributeEditPane.determine'),
  269. cssClass: 'btn-primary',
  270. action: function(dialogItself) {
  271. dialogItself.close();
  272. $.ajax({
  273. url: Common.getApiURL(
  274. 'attributeResource/deleteAttributeName',
  275. ),
  276. type: 'get',
  277. dataType: 'json',
  278. beforeSend: function(request) {
  279. Common.addTokenToRequest(request);
  280. },
  281. data: {
  282. attributeNameId: attributeName.id,
  283. },
  284. success: function(data) {
  285. var index = attribute.attributeNameDtoList.indexOf(
  286. attributeName,
  287. );
  288. if (index >= 0) {
  289. attribute.attributeNameDtoList.splice(
  290. index,
  291. 1,
  292. );
  293. }
  294. },
  295. error: function(
  296. XMLHttpRequest,
  297. textStatus,
  298. errorThrown,
  299. ) {
  300. Common.processException(
  301. XMLHttpRequest,
  302. textStatus,
  303. errorThrown,
  304. );
  305. },
  306. });
  307. },
  308. },
  309. {
  310. label: _self.$t('lang.AttributeEditPane.cancel'),
  311. action: function(dialogItself) {
  312. dialogItself.close();
  313. },
  314. },
  315. ],
  316. });
  317. } else {
  318. var index = attribute.attributeNameDtoList.indexOf(
  319. attributeName,
  320. );
  321. if (index >= 0) {
  322. attribute.attributeNameDtoList.splice(index, 1);
  323. }
  324. }
  325. },
  326. /**
  327. * 生成Attribute
  328. * @return {Object} 属性
  329. */
  330. generateAttribute: function() {
  331. var _self = this;
  332. var newAttribute = {
  333. id: -1,
  334. name: '',
  335. help: '',
  336. classKey: _self.attributeKey,
  337. isRequired: false,
  338. isInput: false,
  339. isSearchable: false,
  340. isMutipleSelect: false,
  341. isNumber: false,
  342. attributeNameDtoList: [],
  343. };
  344. return newAttribute;
  345. },
  346. /**
  347. * 生成AttributeName
  348. * @return {Object} 属性值
  349. */
  350. generateAttributeName: function() {
  351. var _self = this;
  352. var newAttributeName = {
  353. id: -1,
  354. attributeId: null,
  355. nameStr: '',
  356. nameDec: null,
  357. };
  358. return newAttributeName;
  359. },
  360. /**
  361. * 返回
  362. * @return {void}
  363. */
  364. back: function() {
  365. window.history.back();
  366. },
  367. },
  368. };
  369. </script>
  370. <style scoped>
  371. .checkbox {
  372. margin-left: 10px;
  373. }
  374. .form-attributeName {
  375. margin-left: 100px;
  376. }
  377. </style>