edit.ui.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. $(function()
  2. {
  3. $('[id^="stage"] .picker-box').last().on('inited', function(){disableItems();});
  4. });
  5. window.addRow = function(e)
  6. {
  7. const currentGroup = $(e.target).closest('.form-group').clone();
  8. const prevGroup = $(e.target).closest('.form-group').prev('.form-group').clone();
  9. let stageOptions = zui.Picker.query("[name^='stage']").options;
  10. let requiredOptions = zui.Picker.query("[name^='required']").options;
  11. /* 获取已有下拉控件的最大id的值加1赋值给新行. */
  12. let index = 0;
  13. const checkedStages = [];
  14. $(".form-group[id^='stage']").each(function()
  15. {
  16. let id = $(this).attr('id').replace(/[^\d]/g, '');
  17. id = parseInt(id);
  18. id ++;
  19. index = id > index ? id : index;
  20. checkedStages.push($(this).find('input[name^="stage"]').val());
  21. })
  22. stageOptions = JSON.parse(JSON.stringify(stageOptions)); // 保证不会影响原来的options
  23. stageOptions.items.forEach(function(item)
  24. {
  25. if(checkedStages.includes(item.value)) item.disabled = true;
  26. });
  27. stageOptions.defaultValue = '';
  28. currentGroup.attr('id', `required${index}`).find('.btn-delete').removeClass('invisible');
  29. prevGroup.attr('id', `stage${index}`);
  30. /* 重新初始化新一行的下拉控件. */
  31. currentGroup.find('.picker-box').html(`<div class='form-group-wrapper picker-box'></div>`, false);
  32. prevGroup.find('.picker-box').html(`<div class='form-group-wrapper picker-box'></div>`, false);
  33. currentGroup.find('.form-label').remove();
  34. prevGroup.find('.form-label').remove();
  35. $(e.target).closest('.form-group').after(currentGroup);
  36. $(e.target).closest('.form-group').after(prevGroup);
  37. new zui.Picker(`#stage${index} .picker-box`, stageOptions);
  38. new zui.Picker(`#required${index} .picker-box`, requiredOptions);
  39. }
  40. window.deleteRow = function(e)
  41. {
  42. const currentGroup = $(e.target).closest('.form-group');
  43. const prevGroup = currentGroup.prev('.form-group');
  44. currentGroup.remove();
  45. prevGroup.remove();
  46. disableItems();
  47. }
  48. /**
  49. * 禁用已选中的下拉控件。
  50. * Disable the selected items in the dropdown control.
  51. */
  52. window.disableItems = function()
  53. {
  54. let chosenStages= [];
  55. $("[name^='stage']").each(function()
  56. {
  57. chosenStages.push($(this).val());
  58. });
  59. let allItems = zui.Picker.query("[name^='stage']").options.items;
  60. let stageItems = [];
  61. for(i = 0; i < allItems.length; i++)
  62. {
  63. allItems[i].disabled = false;
  64. if(chosenStages.includes(allItems[i].value)) allItems[i].disabled = true;
  65. stageItems[allItems[i].value] = Object.assign({},allItems[i]);
  66. stageItems[allItems[i].value].i = i;
  67. }
  68. $(".form-group[id^='stage']").each(function()
  69. {
  70. const $stage = $(this).find('.picker-box').zui('picker');
  71. const stageID = $(this).find('input[name^="stage"]').val();
  72. let currentStageItems = JSON.parse(JSON.stringify(allItems));
  73. if(stageID != 0) currentStageItems[stageItems[stageID].i].disabled = false;
  74. $stage.render({items: currentStageItems});
  75. });
  76. }