cc36907be98f426ce3a81ab9d44b4dcdf1becf87.svn-base 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <el-table
  3. ref="table"
  4. class="editTable"
  5. :data="editableTableData"
  6. style="width: 100%"
  7. :border='true'
  8. :highlight-current-row = 'true'
  9. :row-class-name="tableRowClassName"
  10. @current-change="currentRowChangeHandler"
  11. @click="tableClick"
  12. :height="tableHeight"
  13. >
  14. <slot></slot>
  15. </el-table>
  16. </template>
  17. <script>
  18. import mulitpleSelect from '@baseWidget/multipleSelect.vue'
  19. import singleSelect from '@baseWidget/singleSelect.vue'
  20. export default {
  21. name: 'editabletable',
  22. props:['columnData','tableData'],
  23. watch: {
  24. tableData:function(){
  25. console.log('tableData变化了');
  26. this.editableTableData = this.tableData;
  27. },
  28. columnData:function(){
  29. if(this.columnData){
  30. this.tableColumnConfig = this.columnData;
  31. }
  32. if(this.tableData){
  33. this.editableTableData = this.tableData;
  34. this.editableTableData.forEach((row)=>{
  35. row['_rowState'] = '-';
  36. });
  37. }
  38. }
  39. },
  40. components: {
  41. 'mulitpleSelect':mulitpleSelect,
  42. 'singleSelect':singleSelect,
  43. },
  44. data () {
  45. return {
  46. tableHeight:50,
  47. currentRow:{},
  48. rememberRowData:{},
  49. editableTableData: [],
  50. tableColumnConfig:[]
  51. }
  52. },
  53. created () {
  54. if(this.columnData){
  55. this.tableColumnConfig = this.columnData;
  56. }
  57. if(this.tableData){
  58. this.editableTableData = this.tableData;
  59. this.editableTableData.forEach((row)=>{
  60. row['_rowState'] = '-';
  61. });
  62. }
  63. },
  64. mounted(){
  65. this.$nextTick(function () {
  66. this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 50;
  67. let self = this;
  68. window.onresize = function() {
  69. self.tableHeight = window.innerHeight - self.$refs.table.$el.offsetTop - 50
  70. }
  71. })
  72. },
  73. methods: {
  74. doLayout(){
  75. this.$refs.table.doLayout();
  76. },
  77. cellContentChangeHandler:function(event,columnConfig,row,callBackHandler){
  78. console.log("表格数据编辑,发生变化的列是"+columnConfig.title+',行内容是'+row);
  79. console.log("变化后表格数据");
  80. console.log(this.tableData);
  81. if(callBackHandler){
  82. callBackHandler(event,row,row[columnConfig.prop],this);
  83. }
  84. },
  85. currentRowChangeHandler:function(currentRow){
  86. console.log('当前行变化');
  87. this.currentRow = currentRow;
  88. this.rememberRowData = Object.assign({}, this.currentRow);
  89. },
  90. showTargetLabels:function(ar,labelProp){
  91. let result = [];
  92. if(ar&&ar instanceof Array){
  93. ar.forEach((e)=>{
  94. result.push(e[labelProp]);
  95. });
  96. }
  97. return result.join(',');
  98. },
  99. showTargetLabel:function(obj,labelProp){
  100. if(obj){
  101. return obj[labelProp];
  102. }else{
  103. return '';
  104. }
  105. },
  106. addOneRow:function(newRowObj){
  107. newRowObj['_rowState'] = 'add';
  108. // this.editableTableData.push(newRowObj);
  109. this.editableTableData.unshift(newRowObj);
  110. console.log(this.editableTableData);
  111. this.currentRow = newRowObj;
  112. },
  113. rowCellDataChangeHandler:function(anyEvent,row,column){
  114. console.log("rowCellDataChangeHandler");
  115. this.currentRow[column.prop] = anyEvent['newValue'];
  116. this.currentRow['_rowState'] = 'change';
  117. if(column.type == 'mulitpleSelect'){
  118. if(anyEvent['changeType'] == 'add'){
  119. if(column['inputParams']['addCallBackFunction']&&column['inputParams']['addCallBackFunction'] instanceof Function){
  120. column['inputParams']['addCallBackFunction'](anyEvent.changeElement,row,this);
  121. }
  122. }else if(anyEvent['changeType'] == 'delete'){
  123. if(column['inputParams']['deleteCallBackFunction']&&column['inputParams']['deleteCallBackFunction'] instanceof Function){
  124. column['inputParams']['deleteCallBackFunction'](anyEvent.changeElement,row,this);
  125. }
  126. }
  127. }else if(column.type == 'select'){
  128. if(anyEvent['changeType'] == 'add'){
  129. if(column['inputParams']['addCallBackFunction']&&column['inputParams']['addCallBackFunction'] instanceof Function){
  130. column['inputParams']['addCallBackFunction'](anyEvent.changeElement,row,this);
  131. }
  132. }else if(anyEvent['changeType'] == 'delete'){
  133. if(column['inputParams']['deleteCallBackFunction']&&column['inputParams']['deleteCallBackFunction'] instanceof Function){
  134. column['inputParams']['deleteCallBackFunction'](anyEvent.changeElement,row,this);
  135. }
  136. }else if(anyEvent['changeType'] == 'change'){
  137. if(column['inputParams']['changeCallBackFunction']&&column['inputParams']['changeCallBackFunction'] instanceof Function){
  138. column['inputParams']['changeCallBackFunction'](anyEvent,row,this);
  139. }
  140. }
  141. }
  142. },
  143. tableRowClassName:function(row,rowIndex){
  144. if (row['_rowState'] == 'change') {
  145. return 'warning-row';
  146. } else if (row['_rowState'] == 'add') {
  147. return 'success-row';
  148. }
  149. return '';
  150. },
  151. singleRowSubmit:function(row){
  152. this.currentRow = null;
  153. this.$emit('rowEditDone',{});
  154. },
  155. rollback:function(row){
  156. this.currentRow = this.rememberRowData;
  157. this.currentRow = null;
  158. },
  159. cleanCurrentSelectedRow:function(){
  160. this.currentRow = null;
  161. },
  162. tableClick:function(){
  163. console.log('tableClick');
  164. },
  165. singleRowDelete:function(row){
  166. this.$emit('rowDelete',{'row':row});
  167. }
  168. },
  169. }
  170. </script>
  171. <style>
  172. .editTable .el-select .el-input.is-focus .el-input__inner{
  173. border : 0px !important;
  174. }
  175. .el-table .warning-row {
  176. background: oldlace;
  177. }
  178. .el-table .success-row {
  179. background: #f0f9eb;
  180. }
  181. </style>