index.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. layui.use(['form', 'jquery'], function() {
  2. var form = layui.form,
  3. $ = layui.jquery;
  4. form.verify({
  5. length6: function(value) {
  6. if ((value + "").length > 10) {
  7. return "最大长度10位";
  8. }
  9. }
  10. });
  11. //全局数据数组
  12. var _res = {
  13. };
  14. //提示数组
  15. var msg = [{
  16. t: 0,
  17. r: [{
  18. t: "顾客订单金额的",
  19. u: "%"
  20. }, {
  21. t: "每单固定理货费用",
  22. u: "元"
  23. }]
  24. }];
  25. //查询
  26. $.ajax({
  27. url: 'costsSetUrl',
  28. success: function(res) {
  29. _res = res;
  30. for (var b = 0; b < res.data.length; b++) {
  31. let index = b;
  32. msg.map(function(i){
  33. if(i.t == res.data[index].type){
  34. $("#radioText" + res.data[index].type).text(i.r[res.data[index].allotType].t);
  35. $("#radioUnit" + res.data[index].type).text(i.r[res.data[index].allotType].u);
  36. }
  37. });
  38. form.on('submit(formBtn' + res.data[b].type + ')', function(data) {
  39. res.data[index] = Object.assign(res.data[index], data.field);
  40. return false;
  41. });
  42. form.on('radio(radio' + res.data[b].type + ')', function(data) {
  43. msg.map(function(i){
  44. if(i.t == res.data[index].type){
  45. $("#radioText" + res.data[index].type).text(i.r[data.value].t);
  46. $("#radioUnit" + res.data[index].type).text(i.r[data.value].u);
  47. }
  48. });
  49. });
  50. for (let v in res.data[b]) {
  51. res.data[b][v] += "";
  52. }
  53. form.val("form" + res.data[b].type, res.data[b]);
  54. }
  55. }
  56. });
  57. //保存
  58. form.on('submit(formDemo)', function(data) {
  59. _res.data.map(function(i) {
  60. $('button[lay-filter="formBtn' + i.type + '"]').trigger("click");
  61. });
  62. $.ajax({
  63. url: 'costsSetUrl',
  64. type: 'post',
  65. contentType: 'application/json',
  66. data: JSON.stringify(_res.data),
  67. success: function(res) {
  68. !(res.code - 0) && layui.layer.msg("保存成功!", {
  69. icon: 1
  70. });
  71. }
  72. });
  73. return false;
  74. });
  75. });