unitSelect2.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. (function (vc) {
  2. vc.extends({
  3. propTypes: {
  4. parentModal: vc.propTypes.string,
  5. callBackListener: vc.propTypes.string, //父组件名称
  6. callBackFunction: vc.propTypes.string //父组件监听方法
  7. },
  8. data: {
  9. unitSelect2Info: {
  10. units: [],
  11. floorId: '-1',
  12. unitId: '-1',
  13. unitNum: '',
  14. unitName: '',
  15. unitSelector:{}
  16. }
  17. },
  18. watch: {
  19. unitSelect2Info: {
  20. deep: true,
  21. handler: function () {
  22. vc.emit($props.callBackListener, $props.callBackFunction, this.unitSelect2Info);
  23. console.log('是否执行 watch',$props.callBackListener, $props.callBackFunction, this.unitSelect2Info);
  24. vc.emit($namespace, 'roomSelect2', "transferRoom", this.unitSelect2Info);
  25. }
  26. }
  27. },
  28. _initMethod: function () {
  29. this._initUnitSelect2();
  30. },
  31. _initEvent: function () {
  32. //监听 modal 打开
  33. /* $('#'+$props.parentModal).on('show.bs.modal', function () {
  34. this._initUnitSelect2();
  35. })*/
  36. vc.on('unitSelect2', "transferFloor", function (_param) {
  37. vc.copyObject(_param, this.unitSelect2Info);
  38. });
  39. vc.on('unitSelect2', 'setUnit', function (_param) {
  40. vc.copyObject(_param, this.unitSelect2Info);
  41. /* $(".unitSelector").val(_param.unitId).select2();*/
  42. var option = new Option(_param.unitNum , _param.unitId, true, true);
  43. this.unitSelect2Info.unitSelector.append(option);
  44. });
  45. },
  46. methods: {
  47. _initUnitSelect2: function () {
  48. console.log("调用_initUnitSelect2 方法");
  49. $.fn.modal.Constructor.prototype.enforceFocus = function () {
  50. };
  51. $.fn.select2.defaults.set('width', '100%');
  52. this.unitSelect2Info.unitSelector = $('#unitSelector').select2({
  53. placeholder: '必填,请选择单元',
  54. allowClear: true,//允许清空
  55. escapeMarkup: function (markup) {
  56. return markup;
  57. }, // 自定义格式化防止xss注入
  58. ajax: {
  59. url: "/callComponent/unitSelect2/loadUnits",
  60. dataType: 'json',
  61. delay: 250,
  62. data: function (params) {
  63. console.log("param", params);
  64. var _term = "";
  65. if (params.hasOwnProperty("term")) {
  66. _term = params.term;
  67. }
  68. return {
  69. unitNum: _term,
  70. page: 1,
  71. row: 10,
  72. floorId: this.unitSelect2Info.floorId,
  73. communityId: vc.getCurrentCommunity().communityId
  74. };
  75. },
  76. processResults: function (data) {
  77. console.log(data, this._filterUnitData(data));
  78. return {
  79. results: this._filterUnitData(data)
  80. };
  81. },
  82. cache: true
  83. }
  84. });
  85. $('#unitSelector').on("select2:select", function (evt) {
  86. //这里是选中触发的事件
  87. //evt.params.data 是选中项的信息
  88. console.log('select', evt);
  89. this.unitSelect2Info.unitId = evt.params.data.id;
  90. this.unitSelect2Info.unitName = evt.params.data.text;
  91. });
  92. $('#unitSelector').on("select2:unselect", function (evt) {
  93. //这里是取消选中触发的事件
  94. //如配置allowClear: true后,触发
  95. console.log('unselect', evt)
  96. });
  97. },
  98. _filterUnitData: function (_units) {
  99. var _tmpUnits = [];
  100. for (var i = 0; i < _units.length; i++) {
  101. var _tmpUnit = {
  102. id: _units[i].unitId,
  103. text: _units[i].unitNum
  104. };
  105. _tmpUnits.push(_tmpUnit);
  106. }
  107. return _tmpUnits;
  108. }
  109. }
  110. });
  111. })(window.vc);