vue中點擊切換按鈕:點啟用后按鈕變為禁用


實現:

三步:在template中設置2個按鈕,通過v-if ,v-show來控制;data中設置按鈕的默認值;methods中控制點擊按鈕切換效果。

  1 <template>
  2   <el-table
  3     :data="tableData"
  4     border
  5     style="width: 100%">
  6     <el-table-column
  7       fixed
  8       prop="date"
  9       label="日期"
 10       width="200">
 11     </el-table-column>
 12      <el-table-column
 13       prop="state"
 14       label="狀態"
 15       width="150">
 16     </el-table-column>
 17     <el-table-column
 18       prop="name"
 19       label="姓名"
 20       width="120">
 21       <template slot-scope="scope">
 22             <el-input placeholder="請輸入內容" v-show="scope.row.show" v-model="scope.row.姓名">  
 23             </el-input>
 24             <span v-show="!scope.row.show">{{scope.row.姓名}}
 25             </span>
 26         </template>
 27     </el-table-column>
 28     <el-table-column
 29       prop="province"
 30       label="省份"
 31       width="120">
 32     </el-table-column>
 33     <el-table-column
 34       prop="city"
 35       label="市區"
 36       width="120">
 37     </el-table-column>
 38     <el-table-column
 39       prop="address"
 40       label="地址"
 41       width="300"
 42        :show-overflow-tooltip="true" >
 43     </el-table-column>
 44     <el-table-column
 45       prop="zip"
 46       label="郵編"
 47       width="120">
 48     </el-table-column>
 49     <el-table-column
 50       fixed="right"
 51       label="操作"
 52       width="300">
 53       <template slot-scope="scope">
 54         <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
 55         <el-button @click="scope.row.show =true" type="text" size="small">編輯</el-button>
 56         <el-button @click="scope.row.show =false" type="text" size="small">保存</el-button>
 57         <el-button @click="changeStatus" type="text" size="small" v-if="btnStatus == 0">啟用</el-button>
 58         <el-button @click="changeStatus" type="text" size="small" v-show="btnStatus == 1">禁用</el-button>
 59         
 60       </template>
 61       
 62     </el-table-column>
 63   </el-table>
 64 </template>
 65 
 66 <script>
 67   export default {
 68     methods: {
 69       handleClick(row) {
 70         console.log(row);
 71       },
 72       changeStatus(){
 73                 this.btnStatus = this.btnStatus === 0 ? 1 : 0;
 74             }
 75     },
 76 
 77     data() {
 78       return {
 79           btnStatus: 0,
 80         tableData: [{
 81           date: '2016-05-02',
 82           
 83           name: '王小虎王小虎王小虎王小虎王小虎王小虎王小虎王小虎王小虎王小虎王小虎王小虎王小虎王小虎王小虎王小虎王小虎王小虎王小虎',
 84           province: '上海',
 85           city: '普陀區',
 86           address: '上海市普陀區金沙江路 1518 弄上海市普陀區金沙江路 1518 弄',
 87           zip: 200333,
 88           show:true
 89         }, {
 90           date: '2016-05-04',
 91           
 92           name: '王小虎',
 93           province: '上海',
 94           city: '普陀區',
 95           address: '上海市普陀區金沙江路 1517 弄',
 96           zip: 200333,
 97           show:true
 98         }]
 99       }
100     }
101 }
102 </script>
View Code

 

另外,注意下,data中,按鈕的默認值要放在data下,圖1。

不能放在table中,否則會按鈕顯示不出來,且會報錯,圖2:Property or method "btnStatus" is not defined on the instance but referenced during render.

這個報錯原因是:在template里面或者在方法里面使用了 “btnStatus” ,但是在data里面沒有定義。

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM