sa-td.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <!-- 自定义slot -->
  3. <el-table-column 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 v-else-if="type == 'selection'" type="selection" :width="width || '45px'" :min-width="minWidth"></el-table-column>
  10. <!-- index -->
  11. <el-table-column v-else-if="type == 'index'" type="index" :label="name" :width="width || '80px'" :min-width="minWidth"></el-table-column>
  12. <!-- 普通td -->
  13. <el-table-column v-else-if="type == 'text'" :label="name" :width="width" :min-width="minWidth">
  14. <template slot-scope="s">
  15. <span v-if="s.row[prop]">{{s.row[prop]}}</span>
  16. <span v-else>{{not}}</span>
  17. </template>
  18. </el-table-column>
  19. <!-- num 数字 -->
  20. <el-table-column v-else-if="type == 'num'" :label="name" :width="width" :min-width="minWidth" class-name="tc-num">
  21. <template slot-scope="s">
  22. <span v-if="s.row[prop]">{{s.row[prop]}}</span>
  23. <span v-else>{{not}}</span>
  24. </template>
  25. </el-table-column>
  26. <!-- icon -->
  27. <el-table-column v-else-if="type == 'icon'" :label="name" :width="width" :min-width="minWidth">
  28. <template slot-scope="s">
  29. <i v-if="s.row[prop]" :class="s.row[prop]" style="font-size: 1.3em;"></i>
  30. <span v-else>{{not}}</span>
  31. </template>
  32. </el-table-column>
  33. <!-- text-list -->
  34. <el-table-column v-else-if="type == 'text-list'" :label="name" :width="width" :min-width="minWidth || '120px'">
  35. <template slot-scope="s">
  36. <div v-if="s.row[prop]">
  37. <p v-for="item in value_to_arr(s.row[prop])" class="s-text-list-p" style="white-space: nowrap; word-break:keep-all;">
  38. {{item}}
  39. </p>
  40. </div>
  41. <div v-else>{{not}}</div>
  42. </template>
  43. </el-table-column>
  44. <!-- img -->
  45. <el-table-column v-else-if="type == 'img'" :label="name" :width="width" :min-width="minWidth">
  46. <template slot-scope="s">
  47. <img v-if="s.row[prop]" :src="s.row[prop]" class="td-img" @click="sa.showImage(s.row[prop], '400px', '400px')" />
  48. <span v-else>{{not}}</span>
  49. </template>
  50. </el-table-column>
  51. <!-- audio、video、file -->
  52. <el-table-column v-else-if="type == 'audio' || type == 'video' || type == 'file'" :label="name" :width="width" :min-width="minWidth">
  53. <template slot-scope="s">
  54. <el-link type="info" :href="s.row[prop]" target="_blank" v-if="!sa.isNull(s.row[prop])">预览</el-link>
  55. <span v-else>{{not}}</span>
  56. </template>
  57. </el-table-column>
  58. <!-- img-list -->
  59. <el-table-column v-else-if="type == 'img-list'" :label="name" :width="width" :min-width="minWidth || '120px'" show-overflow-tooltip>
  60. <template slot-scope="s">
  61. <div @click="sa.showImageList(value_to_arr(s.row[prop]))" style="cursor: pointer;" v-if="s.row[prop]">
  62. <img :src="value_to_arr(s.row[prop])[0]" class="td-img" />
  63. <span style="color: #999; padding-left: 0.5em;">点击预览</span>
  64. </div>
  65. <div v-else>{{not}}</div>
  66. </template>
  67. </el-table-column>
  68. <!-- xxx-list -->
  69. <el-table-column v-else-if="type == 'audio-list' || type == 'video-list' || type == 'file-list' || type == 'img-video-list'" :label="name" :width="width" :min-width="minWidth">
  70. <template slot-scope="s">
  71. <span v-if="s.row[prop]" style="color: #666;">共 {{value_to_arr(s.row[prop]).length}} 个</span>
  72. <span v-else>{{not}}</span>
  73. </template>
  74. </el-table-column>
  75. <!-- textarea -->
  76. <el-table-column v-else-if="type == 'textarea'" :label="name" :width="width" :min-width="minWidth" show-overflow-tooltip>
  77. <template slot-scope="s">
  78. <span v-if="s.row[prop]">{{sa.maxLength(s.row[prop], 100)}}</span>
  79. <span v-else>{{not}}</span>
  80. </template>
  81. </el-table-column>
  82. <!-- richtext 富文本 -->
  83. <el-table-column v-else-if="type == 'richtext' || type == 'f'" :label="name" :width="width" :min-width="minWidth" show-overflow-tooltip>
  84. <template slot-scope="s">
  85. <span>{{sa.maxLength(sa.text(s.row[prop]), 100)}}</span>
  86. </template>
  87. </el-table-column>
  88. <!-- link -->
  89. <el-table-column v-else-if="type == 'link'" :label="name" :width="width" :min-width="minWidth">
  90. <template slot-scope="s">
  91. <el-link type="primary" :href="s.row[prop]" target="_blank" v-if="!sa.isNull(s.row[prop])">{{s.row[prop]}}</el-link>
  92. <div v-else>无</div>
  93. </template>
  94. </el-table-column>
  95. <!-- link-btn -->
  96. <el-table-column v-else-if="type == 'link-btn'" :label="name" :width="width" :min-width="minWidth">
  97. <template slot-scope="s">
  98. <el-link type="primary" @click="$emit('click', s)" v-if="!sa.isNull(s.row[prop])">{{s.row[prop]}}</el-link>
  99. <div v-else>无</div>
  100. </template>
  101. </el-table-column>
  102. <!-- 钱 money (单位 元) -->
  103. <el-table-column v-else-if="type == 'money'" :label="name" :width="width" :min-width="minWidth">
  104. <template slot-scope="s">
  105. <b class="c-price" v-if="!sa.isNull(s.row[prop])">¥{{s.row[prop]}}</b>
  106. <div v-else>无</div>
  107. </template>
  108. </el-table-column>
  109. <!-- 钱 price-f (单位 分) -->
  110. <el-table-column v-else-if="type == 'money-f'" :label="name" :width="width" :min-width="minWidth">
  111. <template slot-scope="s">
  112. <b class="c-price" v-if="!sa.isNull(s.row[prop])">¥{{s.row[prop] / 100}}</b>
  113. <div v-else>无</div>
  114. </template>
  115. </el-table-column>
  116. <!-- 显示枚举 j、num -->
  117. <el-table-column v-else-if="type == 'enum' || type == 'j'" :label="name" :width="width" :min-width="minWidth">
  118. <template slot-scope="s">
  119. <b v-for="j in jvList" :key="j.key" :style="{color: j.color || '#606266'}" v-if="s.row[prop] == j.key">{{j.value}}</b>
  120. </template>
  121. </el-table-column>
  122. <!-- switch 开关 -->
  123. <el-table-column v-else-if="type == 'switch'" :label="name" :width="width" :min-width="minWidth">
  124. <template slot-scope="s">
  125. <el-switch
  126. v-model="s.row[prop]" v-if='jvList.length >= 2'
  127. :active-value="jvList[0].key" :inactive-value="jvList[1].key"
  128. :active-color="jvList[0].color || '#409EFF'" :inactive-color="jvList[1].color || '#ccc'"
  129. @change="$emit('change', s)">
  130. </el-switch>
  131. <span v-for="j in jvList" :key="j.key" :style="{color: '#999'}" v-if="s.row[prop] == j.key">{{j.value}}</span>
  132. </template>
  133. </el-table-column>
  134. <!-- rate 评分 -->
  135. <el-table-column v-else-if="type == 'rate'" :label="name" :width="width" :min-width="minWidth">
  136. <template slot-scope="s">
  137. <el-rate :value="s.row[prop] <= 5 ? s.row[prop] : 5" show-text disabled v-if="!sa.isNull(s.row[prop])"></el-rate>
  138. <div v-else>无</div>
  139. </template>
  140. </el-table-column>
  141. <!-- date 日期 -->
  142. <el-table-column v-else-if="type == 'date'" :label="name" :width="width" :min-width="minWidth" class-name="tc-date">
  143. <template slot-scope="s"><span>{{sa.forDate(s.row[prop]) || not}}</span></template>
  144. </el-table-column>
  145. <!-- datetime 日期时间 -->
  146. <el-table-column v-else-if="type == 'datetime'" :label="name" :width="width" :min-width="minWidth" class-name="tc-date">
  147. <template slot-scope="s"><span>{{sa.forDate(s.row[prop], 2) || not}}</span></template>
  148. </el-table-column>
  149. <!-- time 时间 -->
  150. <el-table-column v-else-if="type == 'time'" :label="name" :width="width" :min-width="minWidth" class-name="tc-date">
  151. <template slot-scope="s"><span>{{s.row[prop] || not}}</span></template>
  152. </el-table-column>
  153. <!-- 用户头像 -->
  154. <el-table-column v-else-if="type == 'user-avatar'" :label="name" :width="width" :min-width="minWidth">
  155. <template slot-scope="s">
  156. <!-- 无数据的时候显示的 -->
  157. <p v-if="sa.isNull(s.row[prop.split(',')[0]]) && sa.isNull(s.row[prop.split(',')[1]])">暂无</p>
  158. <p v-else>
  159. <img :src="s.row[prop.split(',')[1]]" class="td-img"
  160. style="vertical-align: middle; margin-right: 5px;"
  161. @click="sa.showImage(s.row[prop.split(',')[1]], '400px', '400px')" />
  162. <b>{{s.row[prop.split(',')[0]]}}</b>
  163. </p>
  164. </template>
  165. </el-table-column>
  166. </template>
  167. <script>
  168. module.exports = {
  169. // props: ['name', 'value'],
  170. props: {
  171. // text、img、
  172. type: {
  173. default: 'text'
  174. },
  175. // label提示文字
  176. name: {},
  177. label: {},
  178. // 绑定的属性
  179. prop: {},
  180. // 宽度
  181. width: {},
  182. // 最小宽度
  183. minWidth: {},
  184. // type=menu时,值列表 -- 形如:{1: '正常[green]', 2: '禁用[red]'}
  185. jv: {default: ''},
  186. // 空值时显示的文字
  187. not: {default: '无'}
  188. },
  189. data() {
  190. return {
  191. // type=menu时,解析的值列表 -- 形如:[{key: 1, value: '正常', color: 'green'}]
  192. jvList: [],
  193. // type = img-list 时,解析的元素List
  194. value_arr: [],
  195. }
  196. },
  197. methods: {
  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>