知道了slot 怎么用,才可以理解table这样封装的原因
table插件部分
<template>
<div>
<!-- 关键字部分 -->
<div class="pre_search" v-show="show_key">
<label>关键字:</label>
<input type="text" placeholder="请输入关键字" v-model="key_word" />
</div>
<!-- table部分 -->
<div class="pre_tab">
<table class="pre_dataintable">
<thead>
<slot name="tab_title"></slot>
</thead>
<tbody>
<slot
name="tab_tr"
v-for="data_tr in data_tab"
:item="data_tr"
></slot>
</tbody>
<tbody>
<slot v-if="show_add" name="tab_add"></slot>
<slot v-if="show_upd" name="tab_upd"></slot>
</tbody>
</table>
</div>
<!-- 分页部分 -->
<div class="pre_page" v-show="show_page">
<ul style="text-align: right; margin-right: 20px;">
<li>
当前页:<label>{{ nowpage }}</label
> 合计页:<label>{{ maxpage }}</label
> <a href="#" @click="firstpage">首页</a>|
<a href="#" @click="beforepage">上一页</a>|
<a href="#" @click="nextpage">下一页</a>|
<a href="#" @click="lastpage">尾页</a>
<input type="text" v-model="jumppage" value="" /><a
href="#"
@click="gopage"
class="go"
>Go</a
>
</li>
</ul>
</div>
<!-- 组件引入部分 弹窗组件和等待组件 -->
<div>
<per-message ref="msg_back" :message_data="msg_obj" />
<pre-loading :pre_show_loading="fullloading" />
</div>
</div>
</template>
<script >
/*
名称:Table组件
日期:2019-03-21
作者:hj
目标:1、传入一组数据,自动构建标题和内容=>两个数组 1个数组是标题 1个数组是内容 方便构造
2、所有的增、删、改、查、均在组件内容完成。
3、还可以分页
分析:1、增、删、改、查 调用api主动配置。=>容易
2、标题主动配置。=>容易
3、每一列的校验方式 =>配置
4、数据绑定?通过 solo 占位符实现
*/
export default {
name: "pre_select_one",
components: {
'per-message':()=>import("./pre_message.vue"),
'pre-loading':()=>import("./pre_loading.vue")
},
data() {
return {
data_tab: [],
show_key:true,
key_word:'',
show_page:true,
maxpage:'',
nowpage:'',
jumppage:'',
msg_obj:{},
fullloading:false,
show_add:false,
show_upd:false
};
},
props: {
api_url:String,
pre_post:String,
pre_post_add:{ type:String,default:'' },
pre_post_del:{ type:String,default:'' },
pre_post_upd:{ type:String,default:'' },
},
created:function(){
this.nowpage=1;
},
watch:{
pre_post(val){
this.loaddata();
},
pre_post_add(val){
if(String(val)!='')
this.data_add();
},
pre_post_del(val){
if(String(val)!='')
this.data_del();
},
pre_post_upd(val){
if(String(val)!='')
this.data_upd();
}
},
mounted() {
this.initTable();
this.loaddata();
},
methods: {
initTable:function(){
if(Number(this.maxpage)==0){
this.show_page=false;
}else{
this.show_page=true;
}
},
firstpage:function(){
if(Number(this.nowpage)>1){
this.nowpage=1;
this.refresh_data();
}else{
this.msg_obj={'showtype':'warning','note':'已经是第一页'};
}
},
beforepage:function(){
if(Number(this.nowpage)>1){
this.nowpage--;
this.refresh_data();
}else{
this.msg_obj={'showtype':'warning','note':'已经是第一页'};
}
},
nextpage:function(){
if(Number(this.nowpage)<Number(this.maxpage)){
this.nowpage++;
this.refresh_data();
}else{
this.msg_obj={'showtype':'warning','note':'已经是最后一页'};
}
},
lastpage:function(){
if(Number(this.nowpage)<Number(this.maxpage)){
this.nowpage=this.maxpage;
this.refresh_data();
}else{
this.msg_obj={'showtype':'warning','note':'已经是最后一页'};
}
},
gopage:function(){
console.log('跳转到='+this.jumppage);
if(Number(this.jumppage)>0 && Number(this.jumppage)<=Number(this.maxpage)){
this.nowpage=this.jumppage;
this.refresh_data();
}else{
this.msg_obj={'showtype':'warning','note':'跳转页数不在规定范围内,请检查'+this.jumppage};
}
},
loaddata:function() {
// console.log(this.api_url);
// console.log(this.pre_post);
this.fullloading=true;
this.$post(
this.api_url,
this.pre_post,
).then(res => {
this.fullloading=false;
this.show_add=false;
this.show_upd=false;
try {
res=JSON.parse(res);
// console.log(res.data);
if (res.data.status == 1) {
this.maxpage=res.data.allpage;
this.data_tab=res.data.data;
} else {
this.msg_obj={'showtype':'warning','note':res.msg};
}
this.initTable();
} catch (ex) {
this.msg_obj={'showtype':'warning','note':"查询失败"+ex};
}
});
},
data_add:function(){
// 只是 Postjson 和 提示结果不一样 其他一样。没有必要重新定义方法,所以只控制展示
this.show_add=true;
this.show_upd=false;
},
data_upd:function(){
this.show_add=false;
this.show_upd=true;
},
refresh_data() {
console.log('刷新');
this.$emit("tab-event", this.nowpage,this.key_word);
}
}
};
</script>
<style scoped >
/* pre_search */
.pre_search {
text-align: left;
}
.pre_search > label {
position: relative;
left: 2%;
}
.pre_search > input {
position: relative;
left: 2%;
}
.pre_search > input :focus {
border: 1px solid #024aee;
background-color: #aaa;
}
/* table 整体样式 */
.pre_dataintable {
margin-top: 15px;
border-collapse: collapse;
border: 1px solid #aaa;
width: 95%;
/* padding 没有作用 */
/* padding: 0px 5px 10px 5px; */
position: relative;
left: 2%;
}
.pre_dataintable th {
vertical-align: baseline;
padding: 5px 15px 5px 6px;
background-color: #c1ffc1;
border: 1px solid #3f3f3f;
text-align: center;
color: #000000;
font-size: 16px;
font-weight: bolder;
}
.pre_dataintable td {
/* vertical-align: text-top; */
padding: 5px 8px 5px 8px;
border: 1px solid #aaa;
text-align: left;
}
/* 表格内按钮 */
.pre_dataintable td button {
width: 80px;
padding: 6px 6px 6px 6px;
border: 1px solid #aaa;
text-align: center;
display: inline-block;
}
/* 表格内输入框 */
.pre_dataintable td input {
width: 100%;
height: 100%;
border: 0px;
text-align: left;
padding: 2px 2px 2px 2px;
/* font-size: 16px; */
}
.pre_dataintable tr {
cursor: pointer;
}
.pre_dataintable tr:nth-child(odd) {
background-color: #f5f5f5;
}
.pre_dataintable tr:nth-child(even) {
background-color: #fff;
}
.pre_dataintable tr:hover {
background-color: #e2fde2;
}
.tab_add {
width: 95%;
display: flex;
margin-top: 5px;
margin-left: 2%;
background-color: rgba(103, 233, 77, 0.644);
}
.tab_upd {
width: 95%;
display: flex;
margin-left: 2%;
background-color: rgba(209, 212, 36, 0.644);
}
/* 翻页样式 */
.pre_page {
width: 100%;
margin-top: 5px;
margin-bottom: 5px;
}
.pre_page > ul {
text-align: right;
list-style: none;
}
.pre_page > ul > li > a {
text-decoration: none;
}
.pre_page > ul > li > .go {
display: inline-block;
width: 40px;
height: 20px;
border: 1px solid #cccccc;
background: #024aee;
color: #fff;
border-radius: 4px;
text-align: center;
margin-left: 5px;
}
.pre_page > ul > li > a:hover {
color: #394656;
}
</style>
调用部分
<template>
<div class="mydiv">
<h2>Table插件測試</h2>
<pre-table ref="file_table" @tab-event="refresh_data" :api_url="load_url" :pre_post="load_json">
<!-- 设定标题 -->
<template slot="tab_title">
<tr>
<th style="display:none;">ID</th>
<th>上传用户</th>
<th>文件名称</th>
<th>文件类型</th>
<th>文件大小</th>
<th>上传时间</th>
<th>操作</th>
</tr>
</template>
<!-- user 相当于一个wrapper,容器,是自己定义的 -->
<!-- user.item 对应 子组件的 :item="data_tr" 这里 item -->
<!-- table可以封装到 表格,数据加载,刷新,分页 只需要传入URL就可以了 具体的增删改业务 单独完成 -->
<template slot="tab_tr" slot-scope="user">
<tr>
<td style="display:none;">{{ user.item.id }}</td>
<td class="td_name">{{ user.item.username }}</td>
<td>{{ user.item.upfile_name }}</td>
<td class="td_type">{{ user.item.upfile_type }}</td>
<td>{{ user.item.upfile_size }}</td>
<td>{{ user.item.sys_date }}</td>
<td style="text-align: center;">
<button @click="fun_add()">增加</button>
<button @click="fun_del(user.item.id)">删除</button>
<button @click="fun_upd(user.item.id)">修改</button>
</td>
</tr>
</template>
<!-- 构建新增模板 -->
<template slot="tab_add">
<!-- <h2>名称:</h2>
<input type="text" v-model="upd_upfile_name" />
<button @click="fun_save('add')">保存新增</button>-->
<tr>
<td style="display:none;"></td>
<td>
<input type="text" v-model="t_username">
</td>
<td>
<input type="text" v-model="t_name">
</td>
<td>
<input type="text" v-model="t_type">
</td>
<td>
<input type="text" v-model="t_size">
</td>
<td></td>
<td style="text-align: center">
<button @click="fun_save('add')">保存新增</button>
</td>
</tr>
</template>
<!-- 构建修改模板 -->
<template slot="tab_upd">
<tr>
<td style="display:none;"></td>
<td>
<input type="text" v-model="t_username">
</td>
<td>
<input type="text" v-model="t_name">
</td>
<td>
<input type="text" v-model="t_type">
</td>
<td>
<input type="text" v-model="t_size">
</td>
<td></td>
<td style="text-align: center">
<button @click="fun_save('upd')">保存修改</button>
</td>
</tr>
</template>
</pre-table>
</div>
</template>
<script>
import { GetShort, BackSpecial } from "@/utils/localstore.js";
// 这个是把postjson数据过程封装起来
import { tab_file_select } from "@/utils/loadhttpdata.js";
export default {
components: {
"pre-table": () => import("./personmode/pre_table.vue")
},
data() {
return {
load_url: "",
load_json: "",
upd_upfile_name: "",
t_username: "",
t_name: "",
t_type: "",
t_size: ""
};
},
created: function() {
this.loaddata();
},
methods: {
loaddata: function() {
this.load_url = "/Data_Back";
this.load_json = tab_file_select("", "1");
},
refresh_data: function(page, keyword) {
console.log("刷新结果page=" + page + " keyword=" + keyword);
this.load_url = "/Data_Back";
this.load_json = tab_file_select(keyword, page);
},
fun_add: function() {
// 展示新增位置
this.$refs.file_table.data_add();
},
fun_del: function(id) {
console.log("删除 id=" + id);
},
fun_upd: function(id) {
// 展示修改位置
this.$refs.file_table.data_upd();
},
fun_save: function(type) {
console.log("操作:" + type + " 内容" + this.upd_upfile_name);
}
}
};
</script>
<style scoped>
.td_type {
width: 200px;
height: 20px;
/* white-space: nowrap;
word-break: break-all;
word-wrap: break-word; */
overflow: hidden;
text-overflow: ellipsis;
}
</style>
运行结果