unitSelect2.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. }
  16. },
  17. watch:{
  18. unitSelect2Info:{
  19. deep: true,
  20. handler:function(){
  21. vc.emit($props.callBackListener,$props.callBackFunction,vc.component.unitSelect2Info);
  22. vc.emit('roomSelect2', "transferRoom" ,vc.component.unitSelect2Info);
  23. }
  24. }
  25. },
  26. _initMethod:function(){
  27. vc.component._initUnitSelect2();
  28. },
  29. _initEvent:function(){
  30. //监听 modal 打开
  31. /* $('#'+$props.parentModal).on('show.bs.modal', function () {
  32. vc.component._initUnitSelect2();
  33. })*/
  34. vc.on('unitSelect2', "transferFloor",function (_param) {
  35. vc.copyObject(_param, vc.component.unitSelect2Info);
  36. });
  37. },
  38. methods: {
  39. _initUnitSelect2: function () {
  40. console.log("调用_initUnitSelect2 方法");
  41. $.fn.modal.Constructor.prototype.enforceFocus = function () {};
  42. $.fn.select2.defaults.set('width', '100%');
  43. $('.unitSelector').select2({
  44. placeholder: '必填,请选择单元',
  45. ajax: {
  46. url: "/callComponent/unitSelect2/loadUnits",
  47. dataType: 'json',
  48. delay: 250,
  49. data: function (params) {
  50. console.log("param", params);
  51. var _term = "";
  52. if(params.hasOwnProperty("term")){
  53. _term = params.term;
  54. }
  55. return {
  56. unitNum: _term,
  57. page: 1,
  58. row:10,
  59. floorId:vc.component.unitSelect2Info.floorId,
  60. communityId:vc.getCurrentCommunity().communityId
  61. };
  62. },
  63. processResults: function (data) {
  64. console.log(data, vc.component._filterUnitData(data));
  65. return {
  66. results: vc.component._filterUnitData(data)
  67. };
  68. },
  69. cache: true
  70. }
  71. });
  72. $('.unitSelector').on("select2:select", function (evt) {
  73. //这里是选中触发的事件
  74. //evt.params.data 是选中项的信息
  75. console.log('select',evt);
  76. vc.component.unitSelect2Info.unitId = evt.params.data.id;
  77. vc.component.unitSelect2Info.unitName = evt.params.data.text;
  78. });
  79. $('.unitSelector').on("select2:unselect", function (evt) {
  80. //这里是取消选中触发的事件
  81. //如配置allowClear: true后,触发
  82. console.log('unselect',evt)
  83. });
  84. },
  85. _filterUnitData:function (_units) {
  86. var _tmpUnits = [];
  87. for (var i = 0; i < _units.length; i++) {
  88. var _tmpUnit = {
  89. id:_units[i].unitId,
  90. text:_units[i].unitNum
  91. };
  92. _tmpUnits.push(_tmpUnit);
  93. }
  94. return _tmpUnits;
  95. }
  96. }
  97. });
  98. })(window.vc);