1. 程式人生 > 其它 >vue table表格頭部設定樣式(header-cell-style/header-cell-class-name)

vue table表格頭部設定樣式(header-cell-style/header-cell-class-name)

技術標籤:vue

vue中表格的頭部更改掌握這兩種方法想改哪個地方都能改

  • header-cell-style這種方式可以直接ruturn "color: #fff;background:#00bfbf"
  • header-cell-class-name這種只是返回一個class

至於為什麼,直接上程式碼

HTML示例

<el-table :data="formData.bomList" 
   @selection-change="handleSelectionChange"
   id="table-form"
max-height="300" :header-cell-style="tableHeaderCellStyle" :header-cell-class-name="headerRowStyle" style="margin:20px 0">
<el-table-column type="selection" fixed align="center"></el-table-column> <el-table-column
label="訂單資訊" align="center" background="#027db4">
<el-table-column label="品名" width="150" align="center"> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)"
size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.gradeName">
</el-input> </template> </el-table-column> <el-table-column width="100" align="center"> <template slot="header"> <span class="btnWarn">*</span> <span>圖號</span> </template> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.picNo"></el-input> </template> </el-table-column> <el-table-column width="100" align="center"> <template slot="header"> <span class="btnWarn">*</span> <span>版次</span> </template> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.picVersion"></el-input> </template> </el-table-column> <el-table-column width="150" align="center"> <template slot="header"> <span class="btnWarn">*</span> <span>材質</span> </template> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.materialQuality"></el-input> </template> </el-table-column> <el-table-column label="訂單數量" width="150" align="center"> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.totalNum"></el-input> </template> </el-table-column> <el-table-column width="150" align="center"> <template slot="header"> <span class="btnWarn">*</span> <span>單位</span> </template> <template slot-scope="scope"> <el-select v-model="scope.row.unitNo" placeholder="請選擇" size="mini" @keydown.enter.native="nextFocus(formData.bomList,$event,scope)"> <el-option v-for="item in options7" :key="item.unitNo" :label="item.unitName" :value="item.unitNo"> </el-option> </el-select> </template> </el-table-column> <el-table-column width="150" align="center"> <template slot="header"> <span class="btnWarn">*</span> <span>物料來源</span> </template> <template slot-scope="scope"> <el-select v-model="scope.row.materialSource" placeholder="請選擇" size="mini" @keydown.enter.native="nextFocus(formData.bomList,$event,scope)"> <el-option v-for="item in options8" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"> </el-option> </el-select> </template> </el-table-column> <el-table-column label="交期" width="150" align="center"> <template slot="header"> <span class="btnWarn">*</span> <span>交期</span> </template> <template slot-scope="scope"> <el-date-picker @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" style="width: 100%" v-model="scope.row.deliveryTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="選擇日期"></el-date-picker> </template> </el-table-column> <el-table-column label="備註" width="150" align="center"> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.remark"></el-input> </template> </el-table-column> </el-table-column> <el-table-column label="備料資訊" align="center"> <el-table-column label="材質" width="150" align="center"> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.totalNum"></el-input> </template> </el-table-column> <el-table-column label="品名" width="150" align="center"> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.totalNum"></el-input> </template> </el-table-column> <el-table-column label="料號" width="150" align="center"> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.totalNum"></el-input> </template> </el-table-column> <el-table-column label="材料型別" width="150" align="center"> <template slot="header"> <span class="btnWarn">*</span> <span>交期</span> </template> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.totalNum"></el-input> </template> </el-table-column> <el-table-column label="長度" width="150" align="center"> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.totalNum"></el-input> </template> </el-table-column> <el-table-column label="寬度" width="150" align="center"> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.totalNum"></el-input> </template> </el-table-column> <el-table-column label="厚度" width="150" align="center"> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.totalNum"></el-input> </template> </el-table-column> <el-table-column label="備料件數" width="150" align="center"> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.totalNum"></el-input> </template> </el-table-column> <el-table-column label="重量" width="150" align="center"> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.totalNum"></el-input> </template> </el-table-column> </el-table-column> <el-table-column label="排配資訊" align="center" style="background:00bfbf;"> <el-table-column label="加工製程" width="150" class-name="addColumn" align="center"> <template slot-scope="scope"> <el-input @keydown.enter.native="nextFocus(formData.bomList,$event,scope)" size="mini" placeholder="請輸入內容" maxlength="25" v-model.trim="scope.row.totalNum"></el-input> </template> </el-table-column> </el-table-column> <el-table-column label="操作" width="200" fixed="right" class-name="small-padding fixed-width" align="center"> <!-- <template slot-scope="scope"> --> <!-- <el-button size="mini" type="text" icon="el-icon-delete" class="btn_del" @click="delect(scope.$index)">刪除 </el-button> --> <el-button type="text" size="mini">圖檔上傳</el-button> <el-button type="text" size="mini">委外</el-button> <el-button type="text" size="mini">派工作業</el-button> <!-- </template> --> </el-table-column> </el-table>

圖片示例

在這裡插入圖片描述

1.header-cell-style

tableHeaderCellStyle({row,column,rowIndex, columnIndex}) {
 let cellStyle1;
 let cellStyle2;
 let cellStyle3;
 cellStyle1= "color: #fff;background:#027db4"
 cellStyle2= "color: #fff;background:#70b603"
 cellStyle3= "color: #fff;background:#00bfbf"
 if(columnIndex >= 0 && columnIndex < 9 && rowIndex===1){
   return cellStyle1;
 }
 if(columnIndex > 8 && columnIndex < 18 && rowIndex===1){
   return cellStyle2;
 }
 if(columnIndex === 18 && rowIndex===1){
   return cellStyle3;
 }
},

2.header-cell-class-name

headerRowStyle(obj){
  if(obj.column.type=="selection"){
    return 'ddxxys';
  }
  if(obj.column.label=="訂單資訊"){
    return 'ddxxys';
  }
  if(obj.column.label=="備料資訊"){
    return 'blxxys';
  }
  if(obj.column.label=="排配資訊"){
    return 'ppxxys';
  }
  if(obj.column.label=="操作"){
    return 'czanys';
  }
},
<style>
  .ddxxys {
    background: #027db4 !important;
    color: #fff !important;
  }
  .blxxys {
    background: #70b603 !important;
    color: #fff !important;
  }
  .ppxxys {
    background: #00bfbf !important;
    color: #fff !important;
  }
  .czanys {
    background: #02a7f0 !important;
    color: #fff !important;
  }
</style>

遇到的坑

  1. 可以看到通過這兩種方式都能夠獲取到column,一個新增的是class一個是直接的樣式
  2. 如果你試著想通過elements找到標籤上的class去設定樣式,你會發現樣式設定上去了,但是你點選另一個的表格,再回來就會發現class已經變了
  3. 千萬不要使用column的id去做判斷,這個id是會改變的

結語

本文就分享到這,希望對你有幫助