ListData.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. module.exports = {
  2. name: 'list-data',
  3. data() {
  4. return {
  5. batch_flag: true, //符合批量删除为true,否则为false
  6. batch_datas: [],
  7. batch_ids: [],
  8. image_host: this.ImageHost, //如果列表中有图片,并且不是绝对路径的话,可以传入这个参数
  9. list: this.List, //列表数组
  10. fields: this.FieldList, //字段数组
  11. selection: this.Selection, //是否需要批量选择
  12. expand:this.Expand,
  13. btn_info: this.BtnInfo,
  14. pagination: this.Pagination,
  15. }
  16. },
  17. methods: {
  18. /**
  19. * 表格列表触发CheckBox的事件
  20. * @param {array} val 当前选中的用户信息数组,每个元素是用户信息对象
  21. */
  22. onSelectionChange(val) {
  23. this.batch_datas = val;
  24. this.batch_ids = [];
  25. if (val.length) {
  26. this.batch_flag = false;
  27. for (var i = 0; i < val.length; i++) {
  28. this.batch_ids.push(val[i].id);
  29. }
  30. } else {
  31. this.batch_flag = true;
  32. }
  33. /**
  34. * 改变CheckBox事件,第一个参数是ID数组,第二个参数二维数组,每个数组是选中的对象
  35. */
  36. this.$emit('onSelectionChange', this.batch_ids, this.batch_datas);
  37. this.$emit('onSelectionChangeObj', {
  38. ids: this.batch_ids,
  39. datas: this.batch_datas
  40. });
  41. },
  42. /**
  43. * 删除事件
  44. * @param {object || boolean} user 当前信息对象或者为布尔值,为布尔值时,代表是批量删除
  45. * @param {number} index 当前列表索引
  46. */
  47. onDelete(data, index) {
  48. var opts = {};
  49. if (data === true) {
  50. opts.batch_ids = this.batch_ids;
  51. opts.batch_datas = this.batch_datas;
  52. } else {
  53. opts.data = data;
  54. opts.index = index;
  55. }
  56. /**
  57. * 删除事件,参数为对象
  58. * 分两种情况,一种是单个删除,一种是批量删除,属性分别如下
  59. * 1:单个删除
  60. * opts.data 当前要删除的数据对象
  61. * opts.index 当前要删除的索引
  62. * opts.list 当前列表数组
  63. * 2:批量删除
  64. * opts.batch_ids 一维数组,需要删除的ID数组
  65. * opts.batch_datas 二维数组,每个元素为对象(需要删除的数据对象)
  66. */
  67. this.$emit('onDelete', opts);
  68. },
  69. onAdd(){
  70. this.$emit('onAdd', {});
  71. },
  72. /**
  73. * 获取行信息事件
  74. * @param {object} row 当前行对象
  75. * @param {number} index 当前行索引
  76. * @param {array} list 当前列表数组
  77. */
  78. onGetInfo(row, index, list, type) {
  79. this.$emit('onGetInfo', {
  80. row,
  81. index,
  82. list,
  83. type
  84. });
  85. },
  86. onBtnEvent(type,row={},index=-1,list=[]){
  87. this.$emit('onBtn'+type, {
  88. row,
  89. index,
  90. list
  91. });
  92. },
  93. onUpdateBtn(data, index, list) {
  94. if (this.btn_info.update && this.btn_info.update.path) {
  95. var path = this.btn_info.update.path,
  96. param_keys = this.btn_info.update.param_keys || [],
  97. query_keys = this.btn_info.update.query_keys || [],
  98. query = {};
  99. for (var i = 0; i < param_keys.length; i++) {
  100. path += '/' + data[param_keys[i]];
  101. }
  102. for (var i = 0; i < query_keys.length; i++) {
  103. query[query_keys[i]] = data[query_keys[i]];
  104. }
  105. // console.log(path);
  106. // console.log(query);
  107. this.$router.push({
  108. path: path,
  109. query: query
  110. });
  111. } else {
  112. this.onGetInfo(data, index, list, 'update');
  113. }
  114. },
  115. /**
  116. * 内置删除事件执行成功后,更新列表方法
  117. * 分两种情况,一种是批量删除,一种是单个删除
  118. * 1:单个删除
  119. * row 当前需要删除行的索引
  120. * 2:批量删除
  121. * row 一维数组,需要删除的ID数组
  122. */
  123. onUpdateList(row) {
  124. if (!Array.isArray(row)) {
  125. this.list.splice(row, 1);
  126. } else {
  127. this.list = this.list.filter(function(item, idx) {
  128. return row.indexOf(item.id) === -1;
  129. });
  130. }
  131. },
  132. /**
  133. * 改变当前页码事件
  134. * @param {number} page 当前页面
  135. */
  136. onChangeCurrentPage(page) {
  137. this.$emit('onChangeCurrentPage', page);
  138. },
  139. /**
  140. * 改变每页显示的数量事件
  141. * @param {number} page_size 每页显示的数量
  142. */
  143. onChangePageSize(page_size) {
  144. this.$emit('onChangePageSize', page_size);
  145. }
  146. },
  147. mounted() {
  148. // console.log(this.list);
  149. },
  150. /**
  151. * 接收参数
  152. * @type {Object}
  153. */
  154. props: {
  155. ImageHost: {
  156. type: String,
  157. default: ''
  158. },
  159. List: {
  160. type: Array,
  161. required: true
  162. },
  163. FieldList: {
  164. type: Array,
  165. required: true
  166. },
  167. BtnInfo: {
  168. type: Object,
  169. default () {
  170. return {};
  171. }
  172. },
  173. Selection: {
  174. type: Boolean,
  175. default: false
  176. },
  177. Expand: {
  178. type: Object,
  179. default(){
  180. return {
  181. show:false,
  182. position:"left"
  183. };
  184. }
  185. },
  186. Pagination: {
  187. type: Object,
  188. default () {
  189. return {};
  190. }
  191. }
  192. },
  193. /**
  194. * 监控参数
  195. * @type {Object}
  196. */
  197. watch: {
  198. ImageHost(v) {
  199. if (v) {
  200. this.image_host = v;
  201. }
  202. },
  203. List(v) {
  204. if (v) {
  205. this.list = v;
  206. }
  207. },
  208. FieldList(v) {
  209. if (v) {
  210. this.fields = v;
  211. }
  212. },
  213. Selection(v) {
  214. this.selection = v;
  215. },
  216. Expand(v){
  217. this.expand=v;
  218. },
  219. BtnInfo(v) {
  220. this.btn_info = v;
  221. },
  222. Pagination(v) {
  223. this.pagination = v;
  224. }
  225. }
  226. }