sa-td.vue 9.2 KB

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