赞
踩
uniapp + elementui实现一个可以展示数据列表,
查询:顶部搜索框
增:新增按钮被点击会新增一行空白可编辑行-点击保存将新增数据提交到后台
改:选中数据行,可进入编辑状态,点击保存将数据提交到后台
删除:点击删除直接删除
表格参照使用elementui-table,在此基础上增加可编辑状态
直通车:table
总体效果:
编辑状态下效果:
<template>
<view class="content">
<view>
<mSearch @search="search($event,1)"></mSearch>
</view>
<el-table :data="userList" border style="width: 100%" @selection-change="handleSelectionChange" @cell-click="cellClick">
<el-table-column type="selection" width="55">
</el-table-column>
<el-table-column fixed prop="name" label="姓名" width="80">
<template scope="scope">
<el-input v-model="scope.row.name" v-show="scope.row.editable" @blur="loseFcous(scope.$index, scope.row)" > </el-input>
<span style="margin-left: 10px" v-show="!scope.row.editable">{{ scope.row.name }}</span>
</template>
</el-table-column>
<el-table-column prop="account" label="账号" width="120">
<template scope="scope">
<el-input v-model="scope.row.account" v-show="scope.row.editable" @blur="loseFcous(scope.$index, scope.row)" > </el-input>
<span style="margin-left: 10px" v-show="!scope.row.editable">{{ scope.row.account }}</span>
</template>
</el-table-column>
<el-table-column prop="password" label="密码" width="120">
<template scope="scope">
<el-input v-model="scope.row.password" v-show="scope.row.editable" @blur="loseFcous(scope.$index, scope.row)" > </el-input>
<span style="margin-left: 10px" v-show="!scope.row.editable">{{ scope.row.password }}</span>
</template>
</el-table-column>
<el-table-column fixed="right" label="操作" width="60">
<template slot-scope="scope">
<el-button @click="handleClick(scope.row)" type="text" size="small">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-row>
<el-col :span="4"><el-button type="primary" @click="add"><span id="add">新增</span></el-button></div></div></el-col>
<el-col :span="4"><div class="grid-content bg-purple" style="visibility: hidden;"></div></div></el-col>
<el-col :span="4"><div class="grid-content bg-purple" style="visibility: hidden;"></div></el-col>
<el-col :span="4"><div class="grid-content bg-purple" style="visibility: hidden;"></div></el-col>
<el-col :span="4"><div class="grid-content bg-purple" style="visibility: hidden;"></div></el-col>
<el-col :span="4"><el-button type="success" @click="save">保存</el-button></div></el-col>
</el-row>
</view>
</template>
<script>
import mSearch from '@/components/mehaotian-search/mehaotian-search.vue';
function initUser(_this){
console.table(_this.userinfo);
uni.request({
url: "/api/user/",
data:JSON.stringify(_this.userinfo),
header: {
'X-Requested-With': 'XMLHttpRequest',
"Accept": "application/json",
"Content-Type": "application/json",
},
dataType: 'json',
method:'POST',
success: (res) => {
console.table(res);
if(res.data['code']=='0'){
_this.userList = res.data.data;
}else{
uni.$u.toast('服务器异常,请重新获取');
}
},
error:(res)=>{
uni.$u.toast('网络异常');
}
});
};
function updateUser(_this,aurl){
console.table(_this.userinfo);
uni.request({
url: aurl,
data:JSON.stringify(_this.multipleSelection),
header: {
'X-Requested-With': 'XMLHttpRequest',
"Accept": "application/json",
"Content-Type": "application/json",
},
dataType: 'json',
method:'POST',
success: (res) => {
if(res.data['code']=='0'){
console.info(res.data['msg']);
_this.addOrEdit = false;
}else{
console.error('服务器异常,请重新获取');
}
},
error:(res)=>{
uni.$u.toast('网络异常');
}
});
};
function delUser(_this,uid){
console.log(typeof uid)
console.table(_this.userinfo);
uni.request({
url: "/api/user/delete",
data:{"id":uid},
header: {
'X-Requested-With': 'XMLHttpRequest',
"Accept": "application/json",
"Content-Type": "application/json",
},
dataType: 'json',
method:'POST',
success: (res) => {
if(res.data['code']=='0'){
console.info(res.data['msg']);
_this.addOrEdit = false;
}else{
console.error('服务器异常,请重新获取');
}
},
error:(res)=>{
uni.$u.toast('网络异常');
}
});
};
export default {
data() {
return {
userinfo:{
"name": '',
"account": '',
"password": ''
},
userList:[],
multipleSelection:[],
ifCheck:true,//是否被选中,默认选中
checkRows: [],//临时被选中行
addOrEdit: false,//默认修改保存
}
},
components: {
mSearch
},
methods:{
//搜索方法
search(e, val) {
this.userinfo.name=e;
initUser(this);
},
//
handleClick(row){
delUser(this,row['id']);
for(var i=0;i<this.userList.length;i++){
if(this.userList[i]['id']==row['id']){
this.userList.splice(i,1);
}
}
},
//用来改变状态
changeRowState(val,ifCheck){
for(var i=0,l=val.length;i<l;i++){
val[i].editable=ifCheck;
}
},
//选框是否被点击触发
handleSelectionChange(val) {
//取消选择-->取消编辑
if(val.length==0){
this.changeRowState(this.checkRows,false);
}else{
//修改为可编辑状态
this.changeRowState(val,true);
}
this.checkRows = val;
console.log(this.userList)
this.multipleSelection = val;
},
//新增
add(){
this.addOrEdit = true;
this.userList.push({});
},
//保存
save(){
if(this.addOrEdit){
updateUser(this,"/api/user/insert");
}else{
updateUser(this,"/api/user/update");
}
},
//编辑框失去焦点
loseFcous(index, row) {
row.editable=false;
},
//内容框被点击编辑【该方法不太友好,暂时弃用】
cellClick(row, column) {
row.editable=true;
}
},
mounted() {
var _this = this;
initUser(_this);
}
}
</script>
<style lang="scss">
.content {
.text {
font-size: 40upx;
font-weight: bolder;
text-align: center;
}
.addDataBox {
width: 750upx;
background-color: #333;
color: white;
.group {
padding: 15upx;
border: 1upx solid #eee;
border-radius: 20upx;
margin: 0 auto;
margin: 10upx;
button {
margin-left: 20upx;
}
input {
width: 170upx;
height: 60upx;
border: 1upx solid #eee;
}
}
}
}
.btn-group {
margin-top: 20upx;
display: flex;
}
.popupWindow {
background-color: #eee;
border: 1upx solid #333;
position: relative;
top: 10upx;
}
.input {
width: 170upx;
height: 60upx;
border: 1upx solid #333;
}
</style>
<style>
.el-col {
border-radius: 4px;
}
.bg-purple-dark {
background: #99a9bf;
}
.bg-purple {
background: #d3dce6;
}
.bg-purple-light {
background: #e5e9f2;
}
.grid-content {
border-radius: 4px;
min-height: 36px;
}
</style>
后台数据为测试数据,一个简单user–>json数据,这里就不放了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。