vue+element table結合后端自定義table寬度


1、接口代碼結構示意,is_hide為表頭字段隱藏使用,width為設置表頭寬度

{
    "id": {
        "is_hide": 1,
        "width": ""
    },
    "name": {
        "is_hide": 0,
        "width": ""
    },
    "parent_id": {
        "is_hide": 0,
        "width": ""
    },
    "sort": {
        "is_hide": 0,
        "width": "232"
    },
    "status": {
        "is_hide": 0,
        "width": ""
    }
}

2、前端代碼示意,增加element table代碼樣式

<el-table
      v-loading="loading"
      :data="product_infoList"
      row-key="id"
      @header-dragend="handleHeaderDragend"
      @sort-change="sortChange"
      border
      style="width: 100%">
      <el-table-column
        show-overflow-tooltip
        type="selection"
        align="center"/>
      <el-table-column
        fixed
        v-for="(item, index) in fieldsList"
        :key="index"
        :fixed="index==0"
        :prop="item.Field"
        :label="item.Name"
        :width="item.Width"
        sortable="custom"
      >
        <template slot-scope="scope">
          <template v-if="item.Field == 'product_status'"> {{ statusFormat(scope.row, scope.column) }}</template>
          <template v-else-if="item.Field == 'created_at'">
            <span>{{ parseTime(scope.row.created_at) }}</span>
          </template>
          <template v-else-if="item.Field == 'updated_at'">
            <span>{{ parseTime(scope.row.updated_at) }}</span>
          </template>
          <template v-else-if="item.Field == 'description'">
            <span v-html="scope.row.description"></span>
          </template>
          <template v-else-if="item.Field == 'product_image'">
            <el-image
              v-if="scope.row.product_image!=''"
              style="width: 60px; height: 50px"
              :src="apiUrl+'/'+scope.row.product_image"
              fit="fill"
            />
          </template>
          <template v-else>{{ scope.row[scope.column.property] }}</template>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot="header" slot-scope="{ column, $index}" >
          操作 &nbsp;&nbsp;&nbsp;&nbsp; <i @click="getTableHead" class="el-icon-setting"></i>
        </template>
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >刪除
          </el-button>
          
        </template>
      </el-table-column>
    </el-table>

3、增加js方法

methods: {
    // 當拖動表頭改變了列的寬度的時候會觸發該事件
    handleHeaderDragend(newWidth, oldWidth, column, event) {
      if (column.property) {
        const params = {
          types: 'cms_category',
          width: newWidth,
          field: column.property
        }
        setFieldStyle(params).then(response => {
          
        })
      }
    },
/** 刪除按鈕操作 */
handleDelete(row) {

},
/** 修改按鈕操作 */
handleUpdate(row) {

},
}

 

4、通過鼠標對table表格拖拽,改變寬度后傳到后端接口,自定定義不同用戶需求


免責聲明!

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



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