add_region.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>添加角色</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  7. <script src="../../../admin/index.js" door type="text/javascript" charset="utf-8"></script>
  8. </head>
  9. <body>
  10. <form class="layui-form" action="" style="padding: 20px 30px 0 0;">
  11. <div id="areaSelect"></div>
  12. <div class="layui-form-item layui-layer-btn">
  13. <div class="layui-input-block">
  14. <button type="reset" class="layui-btn layui-btn-primary" id="closeWin">取消</button>
  15. <button class="layui-btn" lay-submit lay-filter="formDemo">保存</button>
  16. </div>
  17. </div>
  18. </form>
  19. <script type="text/javascript">
  20. require('js/layuiPlugins/layui-xtree.js');
  21. layui.config({
  22. base: "../../../admin/js/layuiPlugins/"
  23. }).use(["form", "tree", "table", 'cascade'], function () {
  24. var form = layui.form,
  25. $ = layui.$,
  26. cascade = layui.cascade,
  27. table = layui.table;
  28. form.verify({
  29. namelength: [
  30. /^.{2,8}$/,
  31. "长度限制在2-8个字符噢"
  32. ],
  33. });
  34. var region = {
  35. id: (JSON.stringify(location.searchObj()) != "{}") ? location.searchObj().id || null : null, //带参传入ID
  36. currentData: {}, //当前角色对象
  37. infoUrl: "get_roleInfo", //角色信息接口
  38. areaPort: {//地区,参数名:接口
  39. provinceId: 'getProvince',
  40. cityId: 'getCity',
  41. countyId: 'getCounty'
  42. },
  43. addChange: "regionFormUrl", //新增修改接口
  44. closeModal: function () { //关闭当前窗口
  45. parent.layer.close(parent.layer.getFrameIndex(window.name));
  46. }
  47. };
  48. //初始化
  49. region.main = function () {
  50. var _this = this;
  51. //初始化地区
  52. _this.areaCascade = cascade.render({
  53. title: "请选择",
  54. elem: '#areaSelect',
  55. url: _this.areaPort
  56. }).val({
  57. provinceId: location.searchObj().provinceId,
  58. cityId: location.searchObj().cityId,
  59. countyId: '-1000',
  60. });
  61. if (this.id) {
  62. _this.getCurrent(_this.id); //获取并初始化表单角色信息
  63. } else {
  64. }
  65. return this;
  66. };
  67. //获取当前信息
  68. region.getCurrent = function (id) {
  69. var _this = this;
  70. _this.currentData = location.searchObj();
  71. _this.formVal();
  72. $.ajax({
  73. url: this.infoUrl,
  74. data: {
  75. id: id || this.id //当前角色ID
  76. },
  77. success: function (res) {
  78. _this.currentData = res.data[0];
  79. _this.formVal();
  80. }
  81. });
  82. return this;
  83. };
  84. //表单赋值
  85. region.formVal = function () {
  86. form.val(this.formLayFilter, this.currentData);
  87. this.areaCascade.val(this.currentData);
  88. return this;
  89. };
  90. //合并表单内容
  91. region.fusionData = function (obj) {
  92. this.currentData = $.extend(this.currentData, obj);
  93. return this;
  94. };
  95. //提交新增修改
  96. region.submitInfo = function () {
  97. var _this = this;
  98. // if (this.currentData.provinceId && !this.currentData.cityId) {
  99. // this.currentData.regionId = this.currentData.provinceId
  100. // } else if (this.currentData.cityId && !this.currentData.countyId) {
  101. // this.currentData.regionId = this.currentData.cityId
  102. // } else if (this.currentData.countyId) {
  103. // this.currentData.regionId = this.currentData.countyId
  104. // }
  105. console.log("sdfsdafadasda", this.currentData);
  106. $.ajax({
  107. type: this.id ? 'PUT' : 'POST',
  108. url: this.addChange + '?id=' + this.currentData.id,
  109. data: JSON.stringify(this.currentData),
  110. contentType: 'application/json',
  111. success: function (res) {
  112. if (!(res.code - 0)) {
  113. parent.layer.msg(res.msg, {
  114. icon: 1
  115. });
  116. _this.closeModal();
  117. }
  118. }
  119. });
  120. return false;
  121. };
  122. //监听提交
  123. form.on('submit(formDemo)', function (obj) {
  124. //必须return false;
  125. return region.fusionData(obj.field).submitInfo();
  126. });
  127. //关闭窗口
  128. $("#closeWin").on("click", function () {
  129. region.closeModal();
  130. });
  131. region.main();
  132. });
  133. </script>
  134. </body>
  135. </html>