2c2b1dbe6c60ea3e66c6560aaff4d571c6b8ba41.svn-base 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <el-table
  3. class="editTable"
  4. :data="editableTableData"
  5. style="width: 100%"
  6. :border='true'
  7. :highlight-current-row = 'true'
  8. :row-class-name="tableRowClassName"
  9. @current-change="currentRowChangeHandler"
  10. @click="tableClick"
  11. >
  12. <el-table-column v-for="(column,index) in tableColumnConfig"
  13. :label="column.title"
  14. :prop="column.prop"
  15. :key = 'index'
  16. :show-overflow-tooltip = "true"
  17. >
  18. <template slot-scope="scope">
  19. <!-- 针对文字 -->
  20. <transition name="el-fade-in-linear">
  21. <el-input v-model="scope.row[column.prop]" v-if="column.editable && column.type == 'text' && currentRow == scope.row" @change="cellContentChangeHandler($event,column,scope.row,column.inputParams.changeHandler)"></el-input>
  22. </transition>
  23. <transition name="el-fade-in-linear">
  24. <p v-if=" column.type == 'text' && currentRow != scope.row">{{scope.row[column.prop]}}</p>
  25. </transition>
  26. <transition name="el-fade-in-linear">
  27. <p v-if=" column.type == 'text' && currentRow == scope.row && !column.editable">{{scope.row[column.prop]}}</p>
  28. </transition>
  29. <!-- 针对多选 -->
  30. <transition name="el-fade-in-linear">
  31. <mulitpleSelect
  32. :initalValue="scope.row[column.prop]"
  33. :selectData="column['inputParams']['selectData']"
  34. :valueName="column.inputParams['valueName']"
  35. :labelName="column['inputParams']['labelName']"
  36. :valueType="column.inputParams['valueType']"
  37. :otherSearchParams="scope.row"
  38. :subLabelName="column['inputParams']['subLabelName']"
  39. @valueChange="rowCellDataChangeHandler($event,scope.row,column)"
  40. v-if="column.type == 'mulitpleSelect' && currentRow == scope.row"
  41. ></mulitpleSelect>
  42. </transition>
  43. <transition name="el-fade-in-linear">
  44. <p v-if="column.type == 'mulitpleSelect' && currentRow != scope.row">{{showTargetLabels(scope.row[column.prop],column['inputParams']['labelName'])}}</p>
  45. </transition>
  46. <transition name="el-fade-in-linear">
  47. <p v-if="column.type == 'mulitpleSelect' && currentRow == scope.row && !column.editable">{{showTargetLabels(scope.row[column.prop],column['inputParams']['labelName'])}}</p>
  48. </transition>
  49. <!-- 针对单选 -->
  50. <transition name="el-fade-in-linear">
  51. <singleSelect
  52. :initalValue="scope.row[column.prop]"
  53. :selectData="column['inputParams']['selectData']"
  54. :valueName="column.inputParams['valueName']"
  55. :labelName="column['inputParams']['labelName']"
  56. :otherSearchParams="scope.row"
  57. @valueChange="rowCellDataChangeHandler($event,scope.row,column)"
  58. v-if="column.type == 'select' && currentRow == scope.row"
  59. ></singleSelect>
  60. </transition>
  61. <transition name="el-fade-in-linear">
  62. <p v-if="column.type == 'select' && currentRow != scope.row">{{showTargetLabel(scope.row[column.prop],column['inputParams']['labelName'])}}</p>
  63. </transition>
  64. <transition name="el-fade-in-linear">
  65. <p v-if="column.type == 'select' && currentRow == scope.row && !column.editable">{{showTargetLabel(scope.row[column.prop],column['inputParams']['labelName'])}}</p>
  66. </transition>
  67. <!-- 针对日期 -->
  68. </template>
  69. </el-table-column>
  70. <el-table-column
  71. fixed="right"
  72. label="操作"
  73. width="100">
  74. <template slot-scope="scope">
  75. <!-- <el-button v-show="currentRow == scope.row" @click="singleRowSubmit(scope.row)" type="text" size="small">提交</el-button> -->
  76. <!-- <el-button v-show="currentRow == scope.row" @click="rollback(scope.row)" type="text" size="small">取消</el-button> -->
  77. <el-button v-show="currentRow == scope.row" @click="singleRowSubmit(scope.row)" type="text" size="small">确定</el-button>
  78. <el-button v-show="currentRow == scope.row" @click="singleRowDelete(scope.row)" type="text" size="small">删除</el-button>
  79. </template>
  80. </el-table-column>
  81. </el-table>
  82. </template>
  83. <script>
  84. import mulitpleSelect from '@baseWidget/multipleSelect.vue'
  85. import singleSelect from '@baseWidget/singleSelect.vue'
  86. export default {
  87. name: 'editabletable',
  88. props:['columnData','tableData','tableOtherParams'],
  89. watch: {
  90. tableData:function(){
  91. console.log('tableData变化了');
  92. this.editableTableData = this.tableData;
  93. },
  94. columnData:function(){
  95. if(this.columnData){
  96. this.tableColumnConfig = this.columnData;
  97. }
  98. if(this.tableData){
  99. this.editableTableData = this.tableData;
  100. this.editableTableData.forEach((row)=>{
  101. row['_rowState'] = '-';
  102. });
  103. }
  104. }
  105. },
  106. components: {
  107. 'mulitpleSelect':mulitpleSelect,
  108. 'singleSelect':singleSelect,
  109. },
  110. data () {
  111. return {
  112. currentRow:{},
  113. rememberRowData:{},
  114. editableTableData: [],
  115. tableColumnConfig:[]
  116. }
  117. },
  118. created () {
  119. if(this.columnData){
  120. this.tableColumnConfig = this.columnData;
  121. }
  122. if(this.tableData){
  123. this.editableTableData = this.tableData;
  124. this.editableTableData.forEach((row)=>{
  125. row['_rowState'] = '-';
  126. });
  127. }
  128. },
  129. methods: {
  130. cellContentChangeHandler:function(event,columnConfig,row,callBackHandler){
  131. console.log("表格数据编辑,发生变化的列是"+columnConfig.title+',行内容是'+row);
  132. console.log("变化后表格数据");
  133. console.log(this.tableData);
  134. if(callBackHandler){
  135. callBackHandler(event,row,row[columnConfig.prop],this);
  136. }
  137. },
  138. currentRowChangeHandler:function(currentRow){
  139. console.log('当前行变化');
  140. this.currentRow = currentRow;
  141. this.rememberRowData = Object.assign({}, this.currentRow);
  142. },
  143. showTargetLabels:function(ar,labelProp){
  144. let result = [];
  145. if(ar&&ar instanceof Array){
  146. ar.forEach((e)=>{
  147. result.push(e[labelProp]);
  148. });
  149. }
  150. return result.join(',');
  151. },
  152. showTargetLabel:function(obj,labelProp){
  153. if(obj){
  154. return obj[labelProp];
  155. }else{
  156. return '';
  157. }
  158. },
  159. addOneRow:function(newRowObj){
  160. let newRow = Object.assign({}, newRowObj);
  161. this.tableColumnConfig.forEach((column)=>{
  162. if(column.type=='text'){
  163. newRow[column.prop] = '';
  164. if(newRowObj.hasOwnProperty(column.prop)){
  165. newRow[column.prop] = newRowObj[column.prop];
  166. }
  167. }
  168. if(column.type=='mulitpleSelect'){
  169. newRow[column.prop] = [];
  170. if(newRowObj.hasOwnProperty(column.prop)){
  171. newRow[column.prop] = newRowObj[column.prop];
  172. }
  173. }
  174. });
  175. // newRow['id.'] = newRowObj['id'];
  176. newRow['_rowState'] = 'add';
  177. this.editableTableData.push(newRow);
  178. this.currentRow = newRow;
  179. },
  180. rowCellDataChangeHandler:function(anyEvent,row,column){
  181. console.log("rowCellDataChangeHandler");
  182. this.currentRow[column.prop] = anyEvent['newValue'];
  183. this.currentRow['_rowState'] = 'change';
  184. if(column.type == 'mulitpleSelect'){
  185. if(anyEvent['changeType'] == 'add'){
  186. if(column['inputParams']['addCallBackFunction']&&column['inputParams']['addCallBackFunction'] instanceof Function){
  187. column['inputParams']['addCallBackFunction'](anyEvent.changeElement,row,this);
  188. }
  189. }else if(anyEvent['changeType'] == 'delete'){
  190. if(column['inputParams']['deleteCallBackFunction']&&column['inputParams']['deleteCallBackFunction'] instanceof Function){
  191. column['inputParams']['deleteCallBackFunction'](anyEvent.changeElement,row,this);
  192. }
  193. }
  194. }else if(column.type == 'select'){
  195. if(anyEvent['changeType'] == 'add'){
  196. if(column['inputParams']['addCallBackFunction']&&column['inputParams']['addCallBackFunction'] instanceof Function){
  197. column['inputParams']['addCallBackFunction'](anyEvent.changeElement,row,this);
  198. }
  199. }else if(anyEvent['changeType'] == 'delete'){
  200. if(column['inputParams']['deleteCallBackFunction']&&column['inputParams']['deleteCallBackFunction'] instanceof Function){
  201. column['inputParams']['deleteCallBackFunction'](anyEvent.changeElement,row,this);
  202. }
  203. }else if(anyEvent['changeType'] == 'change'){
  204. if(column['inputParams']['changeCallBackFunction']&&column['inputParams']['changeCallBackFunction'] instanceof Function){
  205. column['inputParams']['changeCallBackFunction'](anyEvent,row,this);
  206. }
  207. }
  208. }
  209. },
  210. tableRowClassName:function(row,rowIndex){
  211. if (row['_rowState'] == 'change') {
  212. return 'warning-row';
  213. } else if (row['_rowState'] == 'add') {
  214. return 'success-row';
  215. }
  216. return '';
  217. },
  218. singleRowSubmit:function(row){
  219. this.currentRow = null;
  220. this.$emit('rowEditDone',{});
  221. },
  222. rollback:function(row){
  223. this.currentRow = this.rememberRowData;
  224. this.currentRow = null;
  225. },
  226. cleanCurrentSelectedRow:function(){
  227. this.currentRow = null;
  228. },
  229. tableClick:function(){
  230. console.log('tableClick');
  231. },
  232. singleRowDelete:function(row){
  233. this.$emit('rowDelete',{'row':row});
  234. }
  235. },
  236. }
  237. </script>
  238. <style>
  239. .editTable .el-select .el-input.is-focus .el-input__inner{
  240. border : 0px !important;
  241. }
  242. .el-table .warning-row {
  243. background: oldlace;
  244. }
  245. .el-table .success-row {
  246. background: #f0f9eb;
  247. }
  248. </style>