template.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Template</title>
  8. <link rel="stylesheet" href="./js/layui/css/layui.css">
  9. <style>
  10. .all {
  11. padding-top: 30px;
  12. padding-left: 30px;
  13. padding-right: 30px;
  14. }
  15. .c {
  16. float: left;
  17. }
  18. .c1 {
  19. margin: 10px;
  20. float: left;
  21. }
  22. .t1 {
  23. width: 15%;
  24. }
  25. .row {
  26. height: 50px;
  27. }
  28. .t2 {
  29. width: 10%;
  30. }
  31. .view {
  32. width: 75%;
  33. }
  34. #timeType {
  35. float: left;
  36. width: 10%;
  37. }
  38. #time1 {
  39. float: left;
  40. width: 10%;
  41. }
  42. .btn {
  43. float: left;
  44. width: 10%;
  45. }
  46. .label {
  47. height: 50px;
  48. line-height: 32px;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <div class="all">
  54. <div class="c t1">
  55. <button type="button" class="layui-btn layui-btn-sm" onclick="addTemplate()">添加指标模板</button>
  56. <button type="button" class="layui-btn layui-btn-sm" onclick="deleteTemplate()">删除指标模板</button>
  57. <div id="allArea"></div>
  58. <div id="indicatorTemplate"></div>
  59. <div id="add-template" style="display: none;">
  60. <form class="layui-form" action="">
  61. <div class="layui-form-item" style="margin-top: 20px">
  62. <label class="layui-form-label">模板名称</label>
  63. <div class="layui-input-block" style="width: 400px">
  64. <input id="templateName" type="text" name="templateName" required lay-verify="required"
  65. placeholder="请输入模板名称" autocomplete="off" class="layui-input">
  66. </div>
  67. </div>
  68. </form>
  69. <div id="allIndicator"></div>
  70. </div>
  71. </div>
  72. <div class="c t2">
  73. <form class="layui-form" action="">
  74. <div class="column" id="searchType">
  75. <input type="radio" name="where" value="eci" title="按eci查询" lay-filter="where" checked>
  76. <input type="radio" name="where" value="all" title="全网选择" lay-filter="where">
  77. </div>
  78. <textarea id="condition" cols="14" rows="19"
  79. style="border-width:1px;border-style: solid;border-color: #eee;"></textarea>
  80. </form>
  81. </div>
  82. <div class="c view">
  83. <div class="row layui-form">
  84. <div class="c1 label"> 粒度:</div>
  85. <div class="timeTypeDiv c1" style="width: 10%;">
  86. <select name="timeType" id="timeType">
  87. <option value="quater">15分钟</option>
  88. <option value="hour">小时</option>
  89. <option value="day">天</option>
  90. <!--<option value="week">星期</option>-->
  91. <!--<option value="month">月</option>-->
  92. </select>
  93. </div>
  94. <div class="c1 label" style="margin-left: 120px;">时间:</div>
  95. <input type="text" class="layui-input c1" style="width: 300px;" id="time1">
  96. <button class="layui-btn btn c1" style="margin-left: 20px;" onclick="search()">查询</button>
  97. <button class="layui-btn btn c1" style="margin-left: 20px;" onclick="conditionSearch()">条件查询</button>
  98. </div>
  99. <div class="row">
  100. <table id="demo" lay-filter="test" style="width: 200px;"></table>
  101. </div>
  102. </div>
  103. </div>
  104. <script src="./js/jquery-3.5.1.min.js"></script>
  105. <script src="./js/layui/layui.js"></script>
  106. <script>
  107. var dataA;
  108. var layer;
  109. var tree;
  110. var table;
  111. layui.use(['layer', 'tree', 'form', 'laydate', 'table'], function () {
  112. tree = layui.tree;
  113. layer = layui.layer;
  114. table = layui.table;
  115. var form = layui.form,
  116. laydate = layui.laydate;
  117. // layer.msg('Hello Zhou')
  118. table.render({
  119. elem: '#demo',
  120. height: 320,
  121. // url: 'https://www.layui.com/demo/table/user/' , //数据接口
  122. page: true, //开启分页
  123. toolbar: true,
  124. defaultToolbar: [{
  125. title: '导出' //标题
  126. ,
  127. layEvent: 'export' //事件名,用于 toolbar 事件中使用
  128. ,
  129. icon: 'layui-icon-export' //图标类名
  130. }],
  131. limit: 50,
  132. limits: [50, 200, 500],
  133. cols: [
  134. ],
  135. data: []
  136. });
  137. //工具条事件
  138. table.on('toolbar(test)', function (obj) { //注:tool 是工具条事件名,test 是 table 原始容器的属性 lay-filter="对应的值"
  139. var data = obj.data; //获得当前行数据
  140. var layEvent = obj.event; //获得 lay-event 对应的值(也可以是表头的 event 参数对应的值)
  141. if (layEvent === 'export') {
  142. // layer.alert('Hi,头部工具栏扩展的右侧图标。');
  143. var tableData = table.cache['demo']
  144. obj.config.title = "地市指标"
  145. // console.log(JSON.stringify(dataA))
  146. table.exportFile(obj.config.id, dataA.datas, 'csv')
  147. }
  148. });
  149. laydate.render({
  150. elem: '#time1',
  151. type: 'datetime',
  152. range: true,
  153. });
  154. initTreeCity(tree)
  155. initTreeIndicator(tree)
  156. initTreeIndicatorTemplate(tree)
  157. })
  158. var treeCity;
  159. function initTreeCity(tree) {
  160. console.log('initTreeCity')
  161. $.ajax({
  162. type: "GET",
  163. // url: './json/citys.json',
  164. url: './initTreeCitys',
  165. data: {
  166. "spread": false
  167. },
  168. success: function (r) {
  169. // console.log(r)
  170. let o = r instanceof Object
  171. if (!o) {
  172. r = JSON.parse(r)
  173. }
  174. treeCity = tree.render({
  175. elem: '#allArea',
  176. showCheckbox: true,
  177. accordion: true,
  178. id: 'Id1',
  179. data: r
  180. });
  181. return false;
  182. }
  183. });
  184. }
  185. var treeIn;
  186. // 渲染指标模板
  187. function initTreeIndicatorTemplate(tree) {
  188. console.log('initTreeIndicatorTemplate')
  189. $.ajax({
  190. type: "GET",
  191. url: './initTreeIndicatorTemplate',
  192. data: {
  193. "spread": false
  194. },
  195. success: function (r) {
  196. let o = r instanceof Object
  197. if (!o) {
  198. r = JSON.parse(r)
  199. }
  200. treeIn = tree.render({
  201. elem: '#indicatorTemplate',
  202. showCheckbox: true,
  203. accordion: true,
  204. id: 'Id3',
  205. data: r
  206. });
  207. return false;
  208. }
  209. });
  210. }
  211. function initTreeIndicator(tree) {
  212. console.log('initTreeIndicator')
  213. $.ajax({
  214. type: "GET",
  215. url: './initTreeIndicator',
  216. data: {
  217. "spread": false
  218. },
  219. success: function (r) {
  220. // console.log(r)
  221. let o = r instanceof Object
  222. if (!o) {
  223. r = JSON.parse(r)
  224. }
  225. treeIn = tree.render({
  226. elem: '#allIndicator',
  227. showCheckbox: true,
  228. accordion: true,
  229. id: 'Id2',
  230. data: r
  231. });
  232. return false;
  233. }
  234. });
  235. }
  236. function renderTable(table, citys, quxians, indicators, ttype, sdate) {
  237. console.log('renderTable')
  238. $.ajax({
  239. type: "POST",
  240. url: './renderTable',
  241. async: false,
  242. data: {
  243. "citys": citys,
  244. "quxians": quxians,
  245. "indicators": indicators,
  246. "ttype": ttype,
  247. "sdate": sdate
  248. },
  249. success: function (r) {
  250. dataA = r;
  251. // 没有权限提示
  252. if (r?.msg && r.msg.length > 0) {
  253. alert(r.msg);
  254. return;
  255. }
  256. let o = r instanceof Object
  257. if (!o) {
  258. r = JSON.parse(r)
  259. }
  260. table.render({
  261. elem: '#demo',
  262. height: 320,
  263. // url: 'https://www.layui.com/demo/table/user/' , //数据接口
  264. page: true, //开启分页
  265. toolbar: true,
  266. defaultToolbar: [{
  267. title: '提示' //标题
  268. ,
  269. layEvent: 'export' //事件名,用于 toolbar 事件中使用
  270. ,
  271. icon: 'layui-icon-export' //图标类名
  272. }],
  273. limit: 50,
  274. limits: [50, 200, 500],
  275. cols: [
  276. r.cols
  277. ],
  278. data: r.datas
  279. });
  280. return false;
  281. }
  282. });
  283. }
  284. function conditionRenderTable(table, condition, searchType, indicators, ttype, sdate) {
  285. console.log('conditionRenderTable')
  286. $.ajax({
  287. type: "POST",
  288. url: './conditionRenderTable',
  289. async: false,
  290. data: {
  291. "condition": condition,
  292. "searchType": searchType,
  293. "indicators": indicators,
  294. "ttype": ttype,
  295. "sdate": sdate
  296. },
  297. success: function (r) {
  298. dataA = r;
  299. // console.log(r)
  300. let o = r instanceof Object
  301. if (!o) {
  302. r = JSON.parse(r)
  303. }
  304. table.render({
  305. elem: '#demo',
  306. height: 320,
  307. // url: 'https://www.layui.com/demo/table/user/' , //数据接口
  308. page: true, //开启分页
  309. toolbar: true,
  310. defaultToolbar: [{
  311. title: '提示' //标题
  312. ,
  313. layEvent: 'export' //事件名,用于 toolbar 事件中使用
  314. ,
  315. icon: 'layui-icon-export' //图标类名
  316. }],
  317. limit: 50,
  318. limits: [50, 200, 500],
  319. cols: [
  320. r.cols
  321. ],
  322. data: r.datas
  323. });
  324. return false;
  325. }
  326. });
  327. }
  328. function search() {
  329. // form.on('submit(formDemo)', function (data) {
  330. if ($('#time1').val() == 0) {
  331. alert('填选择时间')
  332. return;
  333. }
  334. if (tree.getChecked('Id1').length == 0) {
  335. alert('请选择地市')
  336. return;
  337. }
  338. if (tree.getChecked('Id3').length == 0) {
  339. alert('请选择指标')
  340. return;
  341. }
  342. alert('开始查询')
  343. // layer.msg(JSON.stringify(data.field));
  344. console.log(JSON.stringify(tree.getChecked('Id1')))
  345. let checkData1 = tree.getChecked('Id1')[0].children;
  346. let checkData2 = tree.getChecked('Id3')[0].children;
  347. console.log(checkData1)
  348. console.log(checkData2)
  349. const quxianA = new Set();
  350. var citysA = [];
  351. checkData1.forEach(eee => {
  352. citysA.push("'" + eee.title + "'")
  353. eee.children.forEach(ee => {
  354. quxianA.add("'" + ee.title + "'")
  355. })
  356. });
  357. const quxians = Array.from(quxianA).join(',')
  358. citys = citysA.join(',')
  359. console.log(`citys: ${citys}`)
  360. console.log(`quxians: ${quxians}`)
  361. var indicatorsA = new Set();
  362. checkData2.forEach(eee => {
  363. eee.children.forEach(ee => {
  364. indicatorsA.add(ee.id)
  365. })
  366. });
  367. const indicators = Array.from(indicatorsA).join(',')
  368. console.log(`indicators: ${JSON.stringify(indicators)}`)
  369. let ttype = $('#timeType').val()
  370. let sdate = $('#time1').val()
  371. console.log(`sdate: ${sdate}`)
  372. renderTable(table, citys, quxians,indicators, ttype, sdate)
  373. return false;
  374. }
  375. function conditionSearch() {
  376. let sdate = $('#time1').val()
  377. if (sdate.length == 0) {
  378. alert('填选择时间')
  379. return;
  380. }
  381. if (tree.getChecked('Id2').length == 0) {
  382. alert('请选择指标')
  383. return;
  384. }
  385. let condition = $('#condition').val();
  386. let ttype = $('#timeType').val()
  387. let searchType = $('#searchType').find('input:checked').val()
  388. if (searchType === 'eci' && $('#condition').val().length == 0) {
  389. alert('填写查询条件')
  390. return;
  391. }
  392. console.log(`searchType: ${searchType}`)
  393. console.log(`condition: ${condition}`)
  394. // layer.msg(JSON.stringify(data.field));
  395. // let checkData1 = tree.getChecked('Id1')[0].children;
  396. let checkData2 = tree.getChecked('Id2')[0].children;
  397. var indicatorsA = new Array();
  398. checkData2.forEach(eee => {
  399. eee.children.forEach(ee => {
  400. indicatorsA.push(ee.id)
  401. })
  402. });
  403. indicators = indicatorsA.join(',')
  404. // console.log(indicators)
  405. conditionRenderTable(table, condition, searchType, indicators, ttype, sdate)
  406. return false;
  407. }
  408. // 添加指标模板
  409. function addTemplate() {
  410. layer.open({
  411. type: 1,
  412. title:"添加指标模板",
  413. content: $("#add-template"),
  414. area: ['600px', '1000px'],
  415. btn: ['确定'],
  416. shadeClose: true,
  417. yes: function(index, layero){
  418. const templateName = $('#templateName').val()
  419. if (!templateName || templateName.length <= 0) {
  420. alert('请输入模板名称!')
  421. return
  422. }
  423. const checkedData = tree?.getChecked('Id2')?.[0]?.children
  424. if (!checkedData || checkedData.length <= 0) {
  425. alert('请选择指标!')
  426. return
  427. }
  428. console.log(`templateName: ${templateName}`)
  429. console.log(`checkedData: ${JSON.stringify(checkedData)}`)
  430. const indicatorsA = []
  431. checkedData.forEach(eee => {
  432. eee.children.forEach(ee => {
  433. indicatorsA.push(ee.id)
  434. })
  435. });
  436. const indicators = indicatorsA.join(',')
  437. console.log(`indicators: ${indicators}`)
  438. $.ajax({
  439. type: "POST",
  440. url: './addTemplate',
  441. async: false,
  442. data: {
  443. "templateName": templateName,
  444. "indicators": indicators,
  445. },
  446. success: function (r) {
  447. console.log(`response: ${JSON.stringify(r)}`)
  448. if (r) {
  449. alert('添加失败!')
  450. } else {
  451. initTreeIndicatorTemplate(tree)
  452. layer.msg('添加成功')
  453. layer.close(index)
  454. }
  455. }
  456. });
  457. },
  458. });
  459. }
  460. // 删除指标模板
  461. function deleteTemplate() {
  462. const checkedData = tree?.getChecked('Id3')?.[0]?.children
  463. const idArray = []
  464. checkedData.forEach(t => {
  465. idArray.push(t.id)
  466. });
  467. if (!checkedData || checkedData.length <= 0) {
  468. alert('请选择要删除的指标模板!')
  469. return
  470. }
  471. layer.confirm('确认删除?', {icon: 3, title:'删除模板'}, function(index){
  472. const ids = idArray.join(',')
  473. console.log(`deleteTemplate: ${JSON.stringify(idArray)}`)
  474. $.ajax({
  475. type: "POST",
  476. url: './deleteTemplate',
  477. async: false,
  478. data: {
  479. "ids": ids,
  480. },
  481. success: function (r) {
  482. console.log(`response: ${JSON.stringify(r)}`)
  483. if (r) {
  484. alert('删除失败!')
  485. } else {
  486. initTreeIndicatorTemplate(tree)
  487. layer.msg('删除成功')
  488. layer.close(index)
  489. }
  490. }
  491. });
  492. });
  493. }
  494. </script>
  495. </body>
  496. </html>