element-ui封裝 el-table el-pagination

components 文件夾 創(chuàng)建 MyTable
MyTable 下 index .vue
方法一 使用插槽的方式
方法二 使用 render 方式

<template>
  <div id="commonTable">
    <!-- :max-height="maxHeight" -->
    <el-table
      v-bind="$attrs"
      v-on="$listeners"
      :data="data"
      tooltip-effect="light"
    >
      <!--region 選擇框-->
      <el-table-column v-if="isSelection" type="selection"> </el-table-column>

      <!--新增操作欄-->
      <el-table-column v-if="isAction" label="操作" fixed="left">
        <template slot-scope="{ row, $index }">
          <slot :rowInfo="{ row, $index }" name="action"></slot>
        </template>
      </el-table-column>

      <!-- 方法二 -->
      <el-column
        v-bind="$attrs"
        v-for="(item, index) in columns"
        :key="index"
        :column="item"
      >
      </el-column>

      <!--  方法一 -->
      <!-- <el-table-column
        v-for="item in columns"
        :key="item.prop"
        :label="item.label"
        :min-width="item.minWidth"
        :fixed="item.fixed"
        :align="item.align"
        :type="item.type"
      >

        <template slot-scope="{ row, $index }">
          {{ !$scopedSlots[item.prop] ? row[item.prop] : "" }}
          <slot
            :rowInfo="{ row, $index }"
            :name="item.prop"
            v-if="$scopedSlots[item.prop]"
          ></slot>
        </template>

      </el-table-column> -->
    </el-table>
    <!-- @size-change="handleSizeChange"
      @current-change="handleCurrentChange" -->
    <el-pagination
      v-if="isPagination"
      v-bind="$attrs"
      v-on="$listeners"
      style="margin-top: 20px; text-align: right;"
    ></el-pagination>
  </div>
</template>

<script>
import ElColumn from "./el-column";
export default {
  name: "MyTable",
  components: {
    ElColumn
  },
  props: {
    data: {
      type: Array,
      required: true
    },
    columns: {
      type: Array,
      required: true
    },
    isSelection: {
      type: Boolean,
      default: false
    },
    isPagination: {
      type: Boolean,
      default: true
    },
    isAction: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {};
  },
  mounted() {
    // console.log(this.rowKey, "表格組件", this.$scopedSlots);
    // 沒有在 props 寫接收才看得到
    console.log(this.$attrs, "表格組件", this.$listeners);
  },
  methods: {}
};
</script>

el-column .vue

<template>
  <el-table-column
    v-bind="$attrs"
    :key="column.prop"
    :label="column.label"
    :min-width="column.minWidth"
    :fixed="column.fixed"
    :align="column.align"
    :type="column.type"
  >
    <template slot-scope="scope">
      <el-render :scope="scope" :render="column.render"> </el-render>
    </template>
  </el-table-column>
</template>

<script>
import ElRender from "./el-render";
export default {
  name: "ElColumn",
  components: {
    ElRender
  },
  props: {
    column: Object
  },
  data() {
    return {
      prop: this.column.prop
    };
  },
  mounted() {
    console.log(this.$attrs, "表格組件子項", this.column);
  },
  watch: {
    column: {
      handler() {
        this.setColumn();
      },
      immediate: true
    }
  },
  methods: {
    setColumn() {
      if (!this.column.render) {
        this.column.render = (h, scope) => {
          return <span>{scope.row[this.prop]}</span>;
        };
      }
    }
  }
};
</script>

el-render.vue

<script>
export default {
  name: "ElRender",
  functional: true,
  props: {
    scope: Object,
    render: Function
  },
  render: (h, ctx) => {
    // console.log(ctx.props.scope, "什么上下文", ctx);
    return ctx.props.render ? ctx.props.render(h, ctx.props.scope) : "";
  }
};
</script>

index.js 全局注冊組件

import MyTable from "./MyTable";

export default Vue => {
  // 注冊組件
  // 使用Vue.component()注冊組件時,全局id自動作為組件的名稱
  // 也就是說搬味,此時第一參數(shù)為組件的名稱
  Vue.component("my-table", MyTable);
};

main.js

import components from './components/index.js';

Vue.use(components);

組件使用
其他參數(shù)按照 element-ui 組件標(biāo)注

<my-table
  v-loading="loading"
  v-bind="{ ...tableData, data: list }"
  :current-page.sync="page"
  :page-sizes="[10, 20, 30, 40]"
  :page-size.sync="per_page"
  :total="total"
  rowKey="id"
  border
  @size-change="handleSizeChange"
  @current-change="handleCurrentChange"
  @selection-change="handleSelectionChange"
  layout="total,  prev, pager, next, sizes, jumper"
  style="width: 100%;margin-top: 20px;"
>
  <template v-slot:action="slotData">
    <el-button @click="del(slotData.rowInfo)" size="small"
      >刪除</el-button
    >
  </template>
  <!--  方法一 -->
  <!-- <template v-slot:type="slotData">
    <el-button @click="del(slotData.rowInfo)" size="small"
      >刪除</el-button
    >
  </template> -->
</my-table>

// 數(shù)據(jù)示例
tableData: {
  isSelection: true,
  // isAction: true,
  // data: this.list,
  columns: [
    {
      // fixed: true,
      type: "expand",
      // 方法二 render 方式
      render: (h, scope) => {
        return <span>{scope.row.type}</span>;
      }
    },
    {
      prop: "id",
      label: "ID",
      minWidth: 260,
      // fixed: true,
      align: "center"
    }
    // {
    //   prop: "type",
    //   label: "類型"
    // },
  ]
}

參考:lb-element-table

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末最冰,一起剝皮案震驚了整個濱河市释涛,隨后出現(xiàn)的幾起案子葛虐,更是在濱河造成了極大的恐慌罗售,老刑警劉巖锅劝,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件臀稚,死亡現(xiàn)場離奇詭異,居然都是意外死亡肋联,警方通過查閱死者的電腦和手機威蕉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來橄仍,“玉大人韧涨,你說我怎么就攤上這事牍戚。” “怎么了虑粥?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵如孝,是天一觀的道長。 經(jīng)常有香客問我娩贷,道長第晰,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任彬祖,我火速辦了婚禮茁瘦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘储笑。我一直安慰自己甜熔,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布南蓬。 她就那樣靜靜地躺著纺非,像睡著了一般。 火紅的嫁衣襯著肌膚如雪赘方。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天弱左,我揣著相機與錄音窄陡,去河邊找鬼。 笑死拆火,一個胖子當(dāng)著我的面吹牛跳夭,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播们镜,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼币叹,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了模狭?” 一聲冷哼從身側(cè)響起颈抚,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎嚼鹉,沒想到半個月后贩汉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡锚赤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年匹舞,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片线脚。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡赐稽,死狀恐怖叫榕,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情姊舵,我是刑警寧澤翠霍,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站蠢莺,受9級特大地震影響寒匙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜躏将,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一锄弱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧祸憋,春花似錦会宪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至拦赠,卻和暖如春巍沙,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背荷鼠。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工句携, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人允乐。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓矮嫉,卻偏偏與公主長得像,于是被迫代替她去往敵國和親牍疏。 傳聞我的和親對象是個殘疾皇子蠢笋,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容

  • vue-cli搭建項目 確保安裝了node與npm 再目標(biāo)文件夾下打開終端 執(zhí)行cnpm i vue-cli -g...
    Akiko_秋子閱讀 3,243評論 1 22
  • element ui Table 二次封裝 表格的分頁 在利用element ui Table編寫項目時,會存在表...
    NingWei閱讀 4,971評論 3 2
  • Vue 實例 屬性和方法 每個 Vue 實例都會代理其 data 對象里所有的屬性:var data = { a:...
    云之外閱讀 2,221評論 0 6
  • 基于Vue的一些資料 內(nèi)容 UI組件 開發(fā)框架 實用庫 服務(wù)端 輔助工具 應(yīng)用實例 Demo示例 element★...
    嘗了又嘗閱讀 1,156評論 0 1
  • 經(jīng)歷春運座位荒 沒座也敢把上車 無奈行李無處放 抱著大箱和小箱 汗味煙臭滿車廂 憋的人心直發(fā)慌 十余小時歷練后 還...
    神于天圣于地閱讀 189評論 0 0