sa-td.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <!-- 自定义slot -->
  3. -<el-table-column align='center' v-if="$slots.default || $scopedSlots.default" :label="name" :width="width" :min-width="minWidth">
  4. <template slot-scope="s">
  5. <slot :row="s.row" :index="s.index"></slot>
  6. </template>
  7. </el-table-column>
  8. <!-- selection框 -->
  9. -<el-table-column align='center' v-else-if="type == 'selection'" type="selection" width="45px" :min-width="minWidth"></el-table-column>
  10. <!-- 普通td -->
  11. -<el-table-column align='center' v-else-if="type == 'text'" :label="name" :width="width" :min-width="minWidth">
  12. <template slot-scope="s">
  13. <span v-if="s.row[prop]">{{s.row[prop]}}</span>
  14. <span v-else>{{not}}</span>
  15. </template>
  16. </el-table-column>
  17. -<el-table-column align='center' v-else-if="type == 'index'" type="index" :label="name" :width="width || '60px'" :min-width="minWidth"></el-table-column>
  18. <!-- num 数字 -->
  19. -<el-table-column align='center' v-else-if="type == 'num'" :label="name" :width="width" :min-width="minWidth" class-name="tc-num">
  20. <template slot-scope="s">
  21. <span v-if="s.row[prop]">{{s.row[prop]}}</span>
  22. <span v-else>{{not}}</span>
  23. </template>
  24. </el-table-column>
  25. <!-- icon -->
  26. -<el-table-column align='center' v-else-if="type == 'icon'" :label="name" :width="width" :min-width="minWidth">
  27. <template slot-scope="s">
  28. <i v-if="s.row[prop]" :class="s.row[prop]" style="font-size: 1.3em;"></i>
  29. <span v-else>{{not}}</span>
  30. </template>
  31. </el-table-column>
  32. <!-- img -->
  33. -<el-table-column align='center' v-else-if="type == 'img'" :label="name" :width="width" :min-width="minWidth">
  34. <template slot-scope="s">
  35. <img v-if="s.row[prop]" :src="s.row[prop]" class="td-img" @click="sa.showImage(s.row[prop], '400px', '400px')" />
  36. <span v-else>{{not}}</span>
  37. </template>
  38. </el-table-column>
  39. <!-- audio、video、file -->
  40. -<el-table-column align='center' v-else-if="type == 'audio' || type == 'video' || type == 'file'" :label="name" :width="width" :min-width="minWidth">
  41. <template slot-scope="s">
  42. <el-link type="info" :href="s.row[prop]" target="_blank" v-if="!sa.isNull(s.row[prop])">预览</el-link>
  43. <span v-else>{{not}}</span>
  44. </template>
  45. </el-table-column>
  46. <!-- img-list -->
  47. -<el-table-column align='center' v-else-if="type == 'img-list'" :label="name" :width="width" :min-width="minWidth || '120px'" show-overflow-tooltip>
  48. <template slot-scope="s">
  49. <span v-for="(item, index) in value_to_arr(s.row[prop])" style="cursor: pointer;margin-left: 2px;">
  50. <img :src="item" class="td-img" @click="sa.showImageList(item)"/>
  51. </span>
  52. </template>
  53. </el-table-column>
  54. <!-- xxx-list -->
  55. -<el-table-column align='center' v-else-if="type == 'audio-list' || type == 'video-list' || type == 'file-list' || type == 'img-video-list'" :label="name" :width="width" :min-width="minWidth">
  56. <template slot-scope="s">
  57. <span v-if="s.row[prop]" style="color: #666;">共 {{value_to_arr(s.row[prop]).length}} 个</span>
  58. <span v-else>{{not}}</span>
  59. </template>
  60. </el-table-column>
  61. <!-- textarea -->
  62. -<el-table-column align='center' v-else-if="type == 'textarea'" :label="name" :width="width" :min-width="minWidth" show-overflow-tooltip>
  63. <template slot-scope="s">
  64. <span v-if="s.row[prop]">{{sa.maxLength(s.row[prop], 100)}}</span>
  65. <span v-else>{{not}}</span>
  66. </template>
  67. </el-table-column>
  68. <!-- richtext 富文本 -->
  69. -<el-table-column align='center' v-else-if="type == 'richtext' || type == 'f'" :label="name" :width="width" :min-width="minWidth" show-overflow-tooltip>
  70. <template slot-scope="s">
  71. <span>{{sa.maxLength(sa.text(s.row[prop]), 100)}}</span>
  72. </template>
  73. </el-table-column>
  74. <!-- link -->
  75. -<el-table-column align='center' v-else-if="type == 'link'" :label="name" :width="width" :min-width="minWidth">
  76. <template slot-scope="s">
  77. <el-link type="primary" :href="s.row[prop]" target="_blank" v-if="!sa.isNull(s.row[prop])">{{s.row[prop]}}</el-link>
  78. <div v-else>-</div>
  79. </template>
  80. </el-table-column>
  81. <!-- link-btn -->
  82. -<el-table-column align='center' v-else-if="type == 'link-btn'" :label="name" :width="width" :min-width="minWidth">
  83. <template slot-scope="s">
  84. <el-link type="primary" @click="$emit('click', s)" v-if="!sa.isNull(s.row[prop])">{{s.row[prop]}}</el-link>
  85. <div v-else>-</div>
  86. </template>
  87. </el-table-column>
  88. <!-- 钱 money (单位 元) -->
  89. -<el-table-column align='center' v-else-if="type == 'money'" :label="name" :width="width" :min-width="minWidth">
  90. <template slot-scope="s">
  91. <b class="c-price" v-if="!sa.isNull(s.row[prop])">¥{{s.row[prop]}}</b>
  92. <div v-else>-</div>
  93. </template>
  94. </el-table-column>
  95. <!-- 钱 price-f (单位 分) -->
  96. -<el-table-column align='center' v-else-if="type == 'money-f'" :label="name" :width="width" :min-width="minWidth">
  97. <template slot-scope="s">
  98. <b class="c-price" v-if="!sa.isNull(s.row[prop])">¥{{s.row[prop] / 100}}</b>
  99. <div v-else>-</div>
  100. </template>
  101. </el-table-column>
  102. <!-- 显示枚举 j、num -->
  103. -<el-table-column align='center' v-else-if="type == 'enum' || type == 'j'" :label="name" :width="width" :min-width="minWidth">
  104. <template slot-scope="s">
  105. <b v-for="j in jvList" :key="j.key" :style="{color: j.color || '#606266'}" v-if="s.row[prop] == j.key">{{j.value}}</b>
  106. </template>
  107. </el-table-column>
  108. <!-- switch 开关 -->
  109. -<el-table-column align='center' v-else-if="type == 'switch'" :label="name" :width="width" :min-width="minWidth">
  110. <template slot-scope="s">
  111. <el-switch
  112. v-model="s.row[prop]" v-if='jvList.length >= 2'
  113. :active-value="jvList[0].key" :inactive-value="jvList[1].key"
  114. :active-color="jvList[0].color || '#409EFF'" :inactive-color="jvList[1].color || '#ccc'"
  115. @change="$emit('change', s)">
  116. </el-switch>
  117. <span v-for="j in jvList" :key="j.key" :style="{color: '#999'}" v-if="s.row[prop] == j.key">{{j.value}}</span>
  118. </template>
  119. </el-table-column>
  120. <!-- rate 评分 -->
  121. -<el-table-column align='center' v-else-if="type == 'rate'" :label="name" :width="width" :min-width="minWidth">
  122. <template slot-scope="s">
  123. <el-rate :value="s.row[prop] <= 5 ? s.row[prop] : 5" show-text disabled v-if="!sa.isNull(s.row[prop])"></el-rate>
  124. <div v-else>-</div>
  125. </template>
  126. </el-table-column>
  127. <!-- date 日期 -->
  128. -<el-table-column align='center' v-else-if="type == 'date'" :label="name" :width="width" :min-width="minWidth" class-name="tc-date">
  129. <template slot-scope="s"><span>{{sa.forDate(s.row[prop]) || not}}</span></template>
  130. </el-table-column>
  131. <!-- datetime 日期时间 -->
  132. -<el-table-column align='center' v-else-if="type == 'datetime'" :label="name" :width="width" :min-width="minWidth" class-name="tc-date">
  133. <template slot-scope="s"><span>{{sa.forDate(s.row[prop], 2) || not}}</span></template>
  134. </el-table-column>
  135. <!-- time 时间 -->
  136. -<el-table-column align='center' v-else-if="type == 'time'" :label="name" :width="width" :min-width="minWidth" class-name="tc-date">
  137. <template slot-scope="s"><span>{{s.row[prop] || not}}</span></template>
  138. </el-table-column>
  139. <!-- 用户头像 -->
  140. -<el-table-column align='center' v-else-if="type == 'user-avatar'" :label="name" :width="width" :min-width="minWidth">
  141. <template slot-scope="s">
  142. <img :src="s.row[prop.split(',')[1]]" class="td-img"
  143. style="vertical-align: middle; margin-right: 5px;"
  144. @click="sa.showImage(s.row[prop.split(',')[1]], '400px', '400px')" />
  145. <b>{{s.row[prop.split(',')[0]]}}</b>
  146. </template>
  147. </el-table-column>
  148. -<el-table-column align='center' v-else-if="type == 'idCard'" :label="name" :width="width" :min-width="minWidth">
  149. <template slot-scope="s">
  150. <span v-if="s.row[prop]">{{maskId(s.row[prop])}}</span>
  151. <span v-else>-</span>
  152. </template>
  153. </el-table-column>
  154. -<el-table-column align='center' v-else-if="type == 'phone'" :label="name" :width="width" :min-width="minWidth">
  155. <template slot-scope="s">
  156. <span v-if="s.row[prop]">{{maskPhone(s.row[prop])}}</span>
  157. <span v-else>-</span>
  158. </template>
  159. </el-table-column>
  160. </template>
  161. <script>
  162. module.exports = {
  163. // props: ['name', 'value'],
  164. props: {
  165. // text、img、
  166. type: {
  167. default: 'text'
  168. },
  169. // label提示文字
  170. name: {},
  171. label: {},
  172. // 绑定的属性
  173. prop: {},
  174. // 宽度
  175. width: {},
  176. // 最小宽度
  177. minWidth: {},
  178. // type=menu时,值列表 -- 形如:{1: '正常[green]', 2: '禁用[red]'}
  179. jv: {default: ''},
  180. // 空值时显示的文字
  181. not: {default: '-'}
  182. },
  183. data() {
  184. return {
  185. // type=menu时,解析的值列表 -- 形如:[{key: 1, value: '正常', color: 'green'}]
  186. jvList: [],
  187. // type = img-list 时,解析的元素List
  188. value_arr: [],
  189. }
  190. },
  191. methods: {
  192. maskId:function (id) {
  193. return id.replace(/^(.{6})(.*)(.{4})$/,'$1********$3');
  194. },
  195. maskPhone:function (phone) {
  196. return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
  197. },
  198. // 解析枚举
  199. parseJv: function() {
  200. for(let key in this.jv) {
  201. let value = this.jv[key];
  202. let color = '';
  203. //
  204. if(value.indexOf('[') != -1 && value.endsWith(']')) {
  205. let index = value.indexOf('[');
  206. color = value.substring(index + 1, value.length - 1);
  207. value = value.substring(0, index);
  208. // console.log(color + ' --- ' + value);
  209. }
  210. //
  211. if(isNaN(key) == false) {
  212. key = parseInt(key);
  213. }
  214. //
  215. this.jvList.push({
  216. key: key,
  217. value: value,
  218. color: color
  219. })
  220. }
  221. },
  222. // 解析 value 为 value_arr
  223. value_to_arr: function(value) {
  224. var value_arr = sa.isNull(value) ? [] : value.split(',');
  225. for (var i = 0; i < value_arr.length; i++) {
  226. if(value_arr[i] == '' || value_arr[i].trim() == '') {
  227. sa.arrayDelete(value_arr, value_arr[i]);
  228. i--;
  229. }
  230. }
  231. // this.value_arr = value_arr;
  232. // this.$nextTick(function() {
  233. // this.value_arr = value_arr;
  234. // })
  235. return value_arr;
  236. },
  237. },
  238. mounted() {
  239. // console.log(this.$slots);
  240. // console.log(this.$scopedSlots.default);
  241. // console.log(this.type);
  242. this.name = this.name || this.label;
  243. // 如果是枚举
  244. if(this.type == 'enum' || this.type == 'j' || this.type == 'switch') {
  245. this.parseJv();
  246. }
  247. // 如果是 img-list 等
  248. // if(this.type == 'img-list' || this.type == 'audio-list' || this.type == 'video-list' || this.type == 'file-list' || this.type == 'img-video-list') {
  249. // this.value_to_arr(this.value);
  250. // }
  251. }
  252. }
  253. </script>
  254. <style scoped>
  255. </style>