sa-td.vue 9.1 KB

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