SelectPage.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. const template=`
  2. <el-select
  3. @change="changeSelect"
  4. v-model="data"
  5. :multiple="multiple"
  6. remote
  7. @clear="clearSelect"
  8. :clearable="true"
  9. filterable
  10. :size="size"
  11. reserve-keyword
  12. style="width: 100%"
  13. remote-show-suffix
  14. :placeholder="placeholder"
  15. :remote-method="remoteMethod"
  16. :loading="loading">
  17. <div class="el-pagination-box">
  18. <el-option
  19. v-for="item in list"
  20. :key="item[keyField]"
  21. :label="item[labelField]"
  22. :value="item[keyField]">
  23. </el-option>
  24. <el-pagination
  25. style="justify-content: center;"
  26. :page-size="pageSize"
  27. layout="total, prev, pager, next"
  28. :pager-count="7"
  29. small
  30. :total="total"
  31. @current-change="changePage">
  32. </el-pagination>
  33. </div>
  34. </el-select>
  35. `;
  36. export default {
  37. name: "SelectPage",
  38. data: function () {
  39. return {
  40. loading:false,
  41. list:[],
  42. pageNumber:1,
  43. query:'',
  44. total:0,
  45. data:'',
  46. }
  47. },
  48. mounted:function (){
  49. if(this.multiple){
  50. if(!this.value){
  51. this.data=[];
  52. }
  53. if(typeof this.value==='string'){
  54. this.data=this.value.split(',');
  55. }
  56. if(this.value instanceof Array){
  57. this.data=this.value.map(res=>{
  58. return res.toString();
  59. });
  60. }
  61. }else{
  62. if(this.value===0 || this.value==='0'){
  63. this.data='';
  64. }else{
  65. this.data=this.value?this.value.toString():'';
  66. }
  67. }
  68. this.initList();
  69. },
  70. props:{
  71. value:'',
  72. labelField:{
  73. type:String,
  74. default:'name'
  75. },
  76. keyField:{
  77. type:String,
  78. default:'id'
  79. },
  80. field:{
  81. type:String,
  82. default:''
  83. },
  84. placeholder:{
  85. type:String,
  86. default:''
  87. },
  88. url:{
  89. type:String,
  90. required:true
  91. },
  92. pageSize:{
  93. type:Number,
  94. default:7
  95. },
  96. size:{
  97. type:String,
  98. default:'default'
  99. },
  100. multiple:{
  101. type:Boolean,
  102. default:false
  103. }
  104. },
  105. template:template,
  106. emits:['change'],
  107. methods:{
  108. initList:function (){
  109. let keyValue=this.value || '';
  110. let postdata={
  111. 'page':this.pageNumber,
  112. 'limit':this.pageSize,
  113. 'keyField':this.keyField,
  114. 'labelField':this.labelField,
  115. 'keyValue':keyValue,
  116. 'labelValue':'',
  117. 'selectpage':true
  118. };
  119. Yunqi.ajax.json(this.url,postdata,false).then(res=>{
  120. this.list=res.rows;
  121. this.total=res.total;
  122. });
  123. },
  124. getList:function (){
  125. let postdata={
  126. 'page':this.pageNumber,
  127. 'limit':this.pageSize,
  128. 'keyField':this.keyField,
  129. 'labelField':this.labelField,
  130. 'keyValue':'',
  131. 'labelValue':this.query,
  132. 'selectpage':true
  133. };
  134. Yunqi.ajax.json(this.url,postdata,false).then(res=>{
  135. this.list=res.rows;
  136. this.total=res.total;
  137. });
  138. },
  139. changeSelect:function (e){
  140. let r=this.data;
  141. if(this.multiple){
  142. r=this.data.join(',');
  143. }
  144. this.$emit('change',this.field?{field: this.field,value:r}:r);
  145. },
  146. clearSelect:function (){
  147. this.pageNumber=1;
  148. this.getList();
  149. },
  150. remoteMethod:function (query){
  151. this.query=query;
  152. if(!query){
  153. return;
  154. }
  155. this.pageNumber=1;
  156. this.getList();
  157. },
  158. changePage:function (e){
  159. this.pageNumber=e;
  160. this.getList();
  161. },
  162. getSelectedData:function (){
  163. let r=[];
  164. for(let i=0;i<this.list.length;i++){
  165. if(this.multiple){
  166. if(this.data.indexOf(this.list[i][this.keyField])>-1){
  167. r.push(this.list[i]);
  168. }
  169. }else{
  170. if(this.data==this.list[i][this.keyField]){
  171. r.push(this.list[i]);
  172. }
  173. }
  174. }
  175. return r;
  176. },
  177. select:function (key){
  178. if(this.multiple){
  179. if(this.data.indexOf(key)>-1){
  180. return;
  181. }
  182. this.data.push(key);
  183. }else{
  184. this.data=key;
  185. }
  186. this.pageNumber=1;
  187. let postdata={
  188. 'page':1,
  189. 'limit':this.pageSize,
  190. 'keyField':this.keyField,
  191. 'labelField':this.labelField,
  192. 'keyValue':this.data,
  193. 'labelValue':'',
  194. 'selectpage':true
  195. };
  196. Yunqi.ajax.json(this.url,postdata,false).then(res=>{
  197. this.list=res.rows;
  198. this.total=res.total;
  199. this.changeSelect();
  200. });
  201. },
  202. remove:function (key){
  203. if(this.multiple){
  204. let i=this.data.indexOf(key);
  205. if(i>-1){
  206. this.data.splice(i,1);
  207. }
  208. }else{
  209. this.data='';
  210. }
  211. let r=this.data;
  212. if(this.multiple){
  213. r=this.data.join(',');
  214. }
  215. this.changeSelect();
  216. }
  217. }
  218. };