0bac8409408f4e5fa0740d2979bb74ac6a05c32d.svn-base 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <template>
  2. <el-select
  3. v-model="value"
  4. collapse-tags
  5. multiple placeholder="请选择"
  6. style="width:100%;height:100%;"
  7. filterable
  8. remote
  9. :remote-method="remoteMethod"
  10. :disabled="disableMuti"
  11. >
  12. <el-option
  13. v-for="item in options"
  14. :key="item.value"
  15. :label="item.label"
  16. :value="item.value">
  17. <span style="float: left">{{ item.label }}</span>
  18. <span style="float: right; color: #8492a6; font-size: 13px" v-if="subLabelName&&subLabelName!=''">{{ item[subLabelName] }}</span>
  19. </el-option>
  20. </el-select>
  21. </template>
  22. <script>
  23. export default {
  24. name: 'multipleSelect',
  25. props:['initalValue','selectData','valueName','labelName','otherSearchParams','tagRemoveMethod','tagAddMethod','valueType','subLabelName','disableMuti'],
  26. watch: {
  27. // disableMuti(oldValue,newValue){
  28. // this.disableMuti = newValue;
  29. // },
  30. initalValue:function(){
  31. console.log('initalValue变化');this.init();
  32. },
  33. value:function(newValue,oldValue){
  34. let changeMark = false;
  35. let changeType = '';
  36. let changeElement = '';
  37. let changeIndex = '';
  38. newValue.forEach((e,index)=>{
  39. if(this.cacheSimpleValue.indexOf(e)<0){
  40. changeMark = true;
  41. changeType="add";
  42. changeIndex = index;
  43. }
  44. });
  45. if(!changeMark){
  46. if(newValue.length != this.cacheSimpleValue.length){
  47. changeMark = true;
  48. this.cacheSimpleValue.forEach((e,index)=>{
  49. if(newValue.indexOf(e)<0){
  50. changeMark = true;
  51. changeType="delete";
  52. changeIndex = index;
  53. }
  54. });
  55. }
  56. }
  57. let emitValue = [];
  58. let _self = this;
  59. if(this.intalValuetype=='Object'){
  60. for(var newValueIndex = 0;newValueIndex<newValue.length;newValueIndex++){
  61. for(var cacheRemoteOptionIndex = 0;cacheRemoteOptionIndex<_self.cacheRemoteOption.length;cacheRemoteOptionIndex++){
  62. if(_self.cacheRemoteOption[cacheRemoteOptionIndex][_self.valueName]==newValue[newValueIndex]){
  63. emitValue.push(_self.cacheRemoteOption[cacheRemoteOptionIndex]);
  64. break;
  65. }
  66. }
  67. }
  68. if(changeType=='delete'){
  69. for(let cacheRemoteOptionIndex = 0;cacheRemoteOptionIndex<_self.cacheRemoteOption.length;cacheRemoteOptionIndex++){
  70. if(_self.cacheRemoteOption[cacheRemoteOptionIndex][_self.valueName]==this.cacheSimpleValue[changeIndex]){
  71. changeElement = _self.cacheRemoteOption[cacheRemoteOptionIndex];
  72. break;
  73. }
  74. }
  75. // let rules = _self.cacheRemoteOption;
  76. // let rest = rules.filter(e => {
  77. // return e.valueName === this.cacheSimpleValue[changeIndex];
  78. // });
  79. // changeElement = rest[0];
  80. }else if(changeType=='add'){
  81. changeElement = emitValue[emitValue.length -1];
  82. }
  83. }else{
  84. emitValue = newValue;
  85. changeElement = this.cacheSimpleValue[changeIndex];
  86. }
  87. if(!this.initProgress&&changeMark){
  88. // this.cacheRemoteOption = [];
  89. this.$emit('multiInputData',this.value);
  90. this.$emit('valueChange',{'newValue':emitValue,'changeType':changeType,'changeElement':changeElement});
  91. }
  92. }
  93. },
  94. data () {
  95. return {
  96. initProgress:true,
  97. cacheRemoteOption:[],
  98. cacheSimpleValue:[],
  99. options: [],
  100. value: [],
  101. remoteMethods:null,
  102. intalValuetype:'',//对象或者字面量
  103. }
  104. },
  105. created () {
  106. this.init();
  107. },
  108. methods: {
  109. init:function(){
  110. this.intalValuetype = 'simple';
  111. this.initProgress = true;
  112. let _self =this;
  113. if(!this.valueName || this.valueName == ''){
  114. this.valueName = 'value';
  115. }
  116. if(!this.labelName || this.labelName == ''){
  117. this.labelName = 'label';
  118. }
  119. this.value = [];
  120. if(!this.initalValue || this.initalValue == null){
  121. this.value = [];
  122. }else{
  123. this.value = this.initalValue;
  124. }
  125. this.intalValuetype = this.valueType?this.valueType:null;
  126. this.options = [];
  127. if(this.selectData instanceof Function){
  128. // console.log('可选项是方法(远端)');
  129. this.remoteMethods = this.selectData;
  130. }else if(this.selectData instanceof Array){
  131. this.options = [].concat(this.selectData);
  132. // console.log('可选项是数组(本端)')
  133. }
  134. this.cacheSimpleValue = [];
  135. this.value.forEach(element => {
  136. if(element instanceof Object){
  137. this.intalValuetype = 'Object';
  138. this.cacheSimpleValue.push(element[this.valueName]);
  139. this.options.push(element);
  140. this.cacheRemoteOption.push(element);
  141. }
  142. // console.log('输入是对象.simple是'+this.cacheSimpleValue);
  143. this.value = this.cacheSimpleValue;
  144. });
  145. this.options.forEach((item,index)=>{
  146. item['value'] = item[_self.valueName];
  147. item['label'] = item[_self.labelName];
  148. });
  149. this.initProgress = false;
  150. },
  151. remoteMethod(query){
  152. let _self = this;
  153. console.log('调用远端搜索方法');
  154. this.remoteMethods(query,this.otherSearchParams).then(s=>{
  155. this.options = s.result;
  156. this.options.forEach((item,index)=>{
  157. item['value'] = item[_self.valueName];
  158. item['label'] = item[_self.labelName];
  159. this.cacheRemoteOption.push(item);
  160. });
  161. },e=>{});
  162. },
  163. removeTagMethod:function(removeTag){
  164. console.log('removeTagMethodHandler'+removeTag);
  165. if(this.tagRemoveMethod && this.tagRemoveMethod instanceof Function){
  166. this.tagRemoveMethod(removeTag,this.otherSearchParams);
  167. }
  168. }
  169. },
  170. }
  171. </script>
  172. <style>
  173. </style>