table组件 和 分页组件来自iview,在这里我根据公司业务再次做了一次封装,使用slot进行内容分发,可以随意放置input输入框和button按钮 ,再使用props向子组件传递参数,使用emit属性向父组件传递事件,
github地址:https://github.com/husanfeng/vue-components-web
代码如下
《子组件》
<template>
<div style="">
<div class="search-bar run-search-bar" style="background:none;">
<div>
<!-- align="right" style="margin:5px" -->
<slot name="handle-bar"></slot>
</div>
<div>
<slot name="search-bar"></slot>
</div>
</div>
<div class="single-table-con">
<div class="table-bar">
<slot name="table-bar"></slot>
</div>
<Table size="small" ref="table" :loading="loading" @on-current-change="onCurrentChange" :highlight-row="highlightRow" :data="tableData" :columns="tableColumns" @on-selection-change="selectionChange" @on-sort-change="sortHandle" @on-row-click="rowClickHandle" stripe></Table>
<div style="margin: 10px;overflow: hidden" v-if="isPage">
<div style="float: right;">
<Page :placement="placement" :total="total" :show-total="showTotal" :page-size-opts="pageSizeOpts" :show-sizer="showSizer" :page-size="param.page.pageSize" :current="param.page.currentPage" @on-change="changePage" size="small" @on-page-size-change="changePageSize"></Page>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: "EasyVueTable",
components: {},
props: {
resource: {
type: Array,
default() {
return [];
}
},
useCatch: {
type: Boolean,
default() {
return false;
}
},
catchParams: {
type: Object,
default() {
return {};
}
},
highlightRow: {
type: Boolean,
default() {
return false;
}
},
action: {
type: String,
default() {
return "";
}
},
params: {
type: Object,
default() {
return {};
}
},
server: {
type: Object,
default() {
return {};
}
},
columns: {
type: Array,
default() {
return [];
}
},
columnsFn: {
type: Function
},
initParam: {
type: Object,
default() {
return {};
}
},
loadCallback: {
type: Function,
default() {
return function(data) {
};
}
},
autoFirst: {
type: Boolean,
default() {
return true;
}
},
pageSize: {
type: Number,
default() {
return 10;
}
},
showTotal: {
type: Boolean,
default() {
return true;
}
},
showSizer: {
type: Boolean,
default() {
return true;
}
},
pageSizeOpts: {
type: Array,
default() {
return [10, 20, 50, 100];
}
},
isPage: {
type: Boolean,
default() {
return true;
}
},
onSelectionChange: {
type: Function
},
rowClickHandle: {
type: Function
}
},
data() {
return {
tableData: [],
tableColumns: [],
total: 0,
currentPage: 1,
selection: [],
loading: false,
param: {
page: {
currentPage: 1,
// pageNum: 0,
pageSize: this.pageSize
},
params: this.params,
sortDTO: {
fieldName: "",
orderBy: ""
}
},
messageJob: undefined,
isSelectionChange: false,
currentRow: undefined
};
},
computed: {
placement() {
let pageTotal = 0;
if (this.total % this.pageSize == 0) {
pageTotal = this.total / this.pageSize;
} else {
pageTotal = this.total / this.pageSize + 1;
}
pageTotal = parseInt(pageTotal);
return this.total % this.pageSize < 3 && this.currentPage == pageTotal
? "bottom"
: "top";
},
lang() {
return $store.state.lang;
}
},
created: function() {
if (!!this.action && this.autoFirst) {
this.load(this.initParam);
} else if(!this.action) {
this.loadLocal();
}
for (let c of this.columns) {
if (!~"selection".indexOf(c.type)) {
c.ellipsis = true;
}
}
this.getColumns();
},
beforeMount: function() {},
mounted: function() {},
beforeDestroy: function() {},
destroyed: function() {},
methods: {
onCurrentChange(currentRow, oldCurrentRow) {
this.currentRow = Object.assign({}, currentRow);
},
getHighlightRow() {
return this.currentRow;
},
getColumns() {
if (typeof this.columnsFn == "function") {
this.tableColumns = [].concat(this.columnsFn());
} else {
this.tableColumns = [].concat(this.columns);
}
},
refresh() {
this.param.page.currentPage = 1;
this.$nextTick(() => {
this.load();
});
},
load(param = {}, page) {
this.selection = [];
for (let p in param) {
this.param.params[p] = param[p];
}
this.loading = true;
if (!!page && typeof page == "number") {
this.param.page.currentPage = page;
}
this.param.page.recordCount = this.total;
!!this.action ? this.loadAjax() : this.loadLocal();
},
loadLocal() {
this.loadCallback(this.resource);
if (!this.isPage) {
this.$nextTick(() => {
this.tableData = [].concat(this.resource);
this.loading = false;
});
return;
}
this.total = this.resource.length;
let start = (this.param.page.currentPage - 1) * this.param.page.pageSize;
let end = start + this.param.page.pageSize;
end = this.resource.length > end ? end : this.resource.length;
this.tableData = [];
for (let i = start; i < end; i++) {
this.tableData.push(this.resource[i]);
}
this.$nextTick(() => {
this.loading = false;
});
},
loadAjax() {
if (this.server[this.action]) {
this.server[this.action](this.param).then(res => {
this.loading = false;
if (!res) return;
this.currentPage = res.data.pageNum;
this.total = res.data.total;
this.tableData = [];
let _list = [];
this.loadCallback(res.data.list);
for (let item of res.data.list) {
this.tableData.push(item);
}
});
} else {
$store.dispatch(this.action, this.param).then(res => {
this.loading = false;
if (!res) return;
this.currentPage = res.data.pageNum;
this.total = res.data.total;
this.tableData = [];
let _list = [];
this.loadCallback(res.data.list);
for (let item of res.data.list) {
this.tableData.push(item);
}
});
}
},
changePage(page) {
this.param.page.currentPage = page;
this.load();
},
changePageSize(page) {
this.param.page.pageSize = page;
this.load();
},
selectionChange(selection) {
this.isSelectionChange = true;
this.selection = selection;
if (typeof this.onSelectionChange == "function") {
this.onSelectionChange(selection);
}
this.$emit("on-selection-change",selection);
},
getSelectioned() {
return this.isSelectionChange ? this.selection : undefined;
},
sortHandle(obj) {
this.param.sortDTO.fieldName = obj.key;
this.param.sortDTO.orderBy = obj.order;
if (this.param.sortDTO.orderBy == "normal") {
this.param.sortDTO = {
fieldName: "",
orderBy: ""
};
}
this.refresh();
},
getSortData() {
return this.param.sortDTO;
},
getTableObj() {
return this.$refs["table"];
}
},
watch: {
lang() {
this.getColumns();
},
resource(newVal, oldVal) {
this.resource = newVal;
}
},
directives: {}
};
</script>
<style lang="less" scoped>
</style>
 
使用如下:(如有疑问随时联系博主)
《父组件》
<template>
<div class="run-mod-box">
<EasyVueTable:params="searchForm" ref="BankInfoTable" :server="server" action="queryBanks" :columns="columns" :initParams="searchForm" :highlight-row="true" @on-selection-change="onSelectionChange">
<Form :model="searchForm" ref="searchForm" slot="search-bar" label-position="top" class="ivu-form-no-margin-bottom" inline>
<Form-item prop="vendorName" label="供应商名称">
<Input type="text" v-model="searchForm.vendorName" size="small"></Input>
</Form-item>
<Form-item prop="bankName" label="开户行">
<Input type="text" v-model="searchForm.bankName" size="small"></Input>
</Form-item>
</Form>
<div slot="handle-bar">
<Button size="small" @click.native="search" type="warning" icon="search">搜索</Button>
<Button size="small" @click.native="reset" type="info" icon="loop">重置</Button>
</div>
</EasyVueTable>
</div>
</template>
 
 

vue.js table组件封装的更多相关文章

  1. Vue.js 自定义组件封装实录——基于现有控件的二次封装(以计时器为例)

    在本人着手开发一个考试系统的过程中,出现了如下一个需求:制作一个倒计时的控件显示在试卷页面上.本文所记录的就是这样的一个过程. 前期工作 对于这个需求,自然我想到的是有没有现成的组件可以直接使用(本着 ...

  2. 【Vue课堂】Vue.js 父子组件之间通信的十种方式

    这篇文章介绍了Vue.js 父子组件之间通信的十种方式,不管是初学者还是已经在用 Vue 的开发者都会有所收获.无可否认,现在无论大厂还是小厂都已经用上了 Vue.js 框架,简单易上手不说,教程详尽 ...

  3. Vue.js多重组件嵌套

    Vue.js多重组件嵌套 Vue.js中提供了非常棒的组件化思想,组件提高了代码的复用性.今天我们来实现一个形如 <app> <app-header></app-head ...

  4. 【Vue.js实战案例】- Vue.js递归组件实现组织架构树和选人功能

    大家好!先上图看看本次案例的整体效果. 浪奔,浪流,万里涛涛江水永不休.如果在jq时代来实这个功能简直有些噩梦了,但是自从前端思想发展到现在的以MVVM为主流的大背景下,来实现一个这样繁杂的功能简直不 ...

  5. Vue.js之组件传值

    Vue.js之组件传值 属性传值可以从父组件到子组件,也可以从子组件到父组件. 这里讲一下从父组件到子组件的传值 还以上次的demo为例,demo里有APP.vue是父组件,Header.vue,Us ...

  6. Vue.js之组件嵌套小demo

    Vue.js之组件嵌套的小demo项目 第一步:初始化一个wabpack项目,这里不在复述.第二步:在components文件夹下新建Header.vue Footer.vue和Users.vue三个 ...

  7. Vue.js之组件嵌套

    Vue.js中组件嵌套有两种方式 第一种:注册全局组件 例如在components文件夹下新建一个User.vue组件,然后在main.js文件中注册全局组件 //注册全局组件 Vue.compone ...

  8. vue.js 同级组件之间的值传递方法(uni-app通用)

    vue.js 兄弟组件之间的值传递方法 https://blog.csdn.net/jingtian678/article/details/81634149

  9. Vue.js说说组件

    什么是组件:组件是Vue.js最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码.在较高层面上,组件是自定义的元素,Vue.js的编译器为它添加特殊功能.在有些情况下,组件也可以是原生HTM ...

随机推荐

  1. javac不是内部或外部命令在win10上的解决方案

    Path环境变量能够让你在任何路径都能使用命令,可能你百度谷歌了各种方案都无法解决javac无法使用的问题,那么你可以试试如下解决方案: 首先博主配置了JAVA_HOME 参数为 C:\Program ...

  2. 【转】Django之Model层的F对象,Q对象以及聚合函数

    转自:https://blog.csdn.net/wsy_666/article/details/86692050 一.F对象: 作用:用于处理类属性(即model的某个列数据),类属性之间的比较.使 ...

  3. Hive分析窗口函数(一) SUM,AVG,MIN,MAX

    Hive分析窗口函数(一) SUM,AVG,MIN,MAX Hive分析窗口函数(一) SUM,AVG,MIN,MAX Hive中提供了越来越多的分析函数,用于完成负责的统计分析.抽时间将所有的分析窗 ...

  4. 2-sat问题简记

    关于2-sat问题,这里笔者主要是做一些简记,如要详细了解,可以读一读此dalao的文章:https://blog.csdn.net/jarjingx/article/details/8521690 ...

  5. kubernetes配置dashborad,web界面

    一,将kubernetes-dashboard.yaml-1.10和admin-rbac.yaml和token.sh的上传到k8s的计算机上 .如图 二,切入到这三个文件所在的目录下,执行命令:kub ...

  6. Electron-Vue工程初始化,以及需要掌握的相关知识

    1.安装nodejs 下载地址:http://nodejs.cn/ 需要重启系统 2.安装electron npm install electron -g 3.安装vue npm install vu ...

  7. Bader分析

    一.背景 理查德·贝德(Richard Bader)开发了一种将分子分解为原子的直观方法.他对原子的定义纯粹基于电子电荷密度.Bader使用所谓的零磁通表面来划分原子.零通量表面是2D表面,其上电荷密 ...

  8. 博客侧边栏公告html代码

    前一小段是时钟: 后一段是百度统计,但是显示不出来. 网站概况 - 百度统计 https://tongji.baidu.com/web/25880379/overview/index 趋势分析 - 百 ...

  9. 安装ubuntu双系统

    今天在win7下安装ubuntu14.1双系统,别折磨了一个下午.主要是开机系统引导问题. 引导程序是位于硬盘最前面的一段程序,由于扇区大部分是引导程序,故也成引导扇区.此外包含有硬盘的分区信息,共6 ...

  10. 记一次 Vue 组件内存泄漏的坑

    概述 最近在开发 Vue 项目的时候遇到了内存泄漏问题,记录下来,供以后开发时参考,相信对其他人也有用. 背景 背景是需要用 three.min.js 和 vanta.net.min.js 给首页加上 ...