vue-easytable使用
参考:https://github.com/huangshuwei/vue-easytablehttp://doc.huangsw.com/vue-easytable/app.html#/table/combinedPaging安装:npm install vue-easytable --save-dev// 引入样式import 'vue-easytable/libs/t...
·
参考:
安装:
npm install vue-easytable --save-dev
// 引入样式
import 'vue-easytable/libs/themes-base/index.css'
// 导入 table 和 分页组件
import {VTable,VPagination} from 'vue-easytable'
// 注册到全局
Vue.component(VTable.name, VTable)
Vue.component(VPagination.name, VPagination)
新建组件:TableDemo.vue
<template>
<div>
<v-table
is-vertical-resize
:vertical-resize-offset='60'
is-horizontal-resize
:is-loading="tableConfig.isLoading"
style="width:100%"
:multiple-sort="false"
:min-height="350"
even-bg-color="#f2f2f2"
:columns="tableConfig.columns"
:table-data="tableConfig.tableData"
row-hover-color="#eee"
row-click-color="#edf7ff"
></v-table>
<v-pagination :total="total" :page-size="pageSize" @page-change="pageChange" @page-size-change="pageSizeChange"></v-pagination>
</div>
</template>
<script>
// 引入样式
import 'vue-easytable/libs/themes-base/index.css'
import axios from '../config/axios.js'
export default {
data: function() {
return {
total: 0,
pageSize: 10,
pageIndex: 1,
tableConfig: {
isLoading: true,
tableData: [],
columns: [{
field: 'name',
title: '姓名',
width: 80,
titleAlign: 'center',
columnAlign: 'center',
isResize: true
}, {
field: 'tel',
title: '手机号码',
width: 80,
titleAlign: 'center',
columnAlign: 'center',
isResize: true
}]
}
}
},
methods: {
pageChange(pageIndex) {
this.pageIndex = pageIndex;
this.loadData();
},
pageSizeChange(newPageSize) {
this.pageSize = newPageSize;
this.loadData();
},
loadData() {
this.tableConfig.isLoading = true;
var context = this;
axios.post('/tables', JSON.stringify({
pageIndex: this.pageIndex,
pageSize: this.pageSize
}))
.then(function(response) {
context.tableConfig.isLoading = false;
context.total = response.data.total;
context.tableConfig.tableData = response.data.data;
console.info(response.data.data);
})
.catch(function(error) {
context.tableConfig.isLoading = false;
console.debug(error);
});
}
},
created() {
this.total = 10000;
this.loadData();
}
}
</script>
<style scoped>
</style>
后台Api:TableController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Vue.Api.Controllers
{
[Route("api/tables")]
[ApiController]
public class TableController : ControllerBase
{
[HttpPost]
public IActionResult GetPageList([FromBody] TablePageSpecification specification)
{
var pageResult = new PageResult<Person>();
pageResult.Total = 100;
var persons = new List<Person>();
for (int i = 0; i < 100; i++)
{
persons.Add(new Person
{
Id = i,
Name = $"LI{i}",
Tel = $"+86 { 1828 * i}"
});
}
pageResult.Data = persons.Skip((specification.PageIndex- 1) *specification.PageSize).Take(specification.PageSize).ToList() ;
return Ok(pageResult);
}
}
public class TablePageSpecification
{
public int PageIndex { get; set; }
public int PageSize { get; set; }
}
public class PageResult<T>
{
public long Total { get; set; }
public List<T> Data { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Tel { get; set; }
}
}
运行效果:
更多推荐
已为社区贡献8条内容
所有评论(0)