目录
结果展示
先上成果
项目简介
使用前端技术框架vue搭建的动态网站-忍者管理系统.
项目内容介绍
通过表格展示忍者的信息,可以删除忍者,修改忍者信息,学习忍术,以及查看其所学忍术,以及最后的添加忍者。
开发过程
一.使用vue-cli,快速搭建项目,写好组件名showRenzhe.vue,下载好相关element-ui的依赖等。
二.开发过程-构建showRenzhe.vue
1.构建好框架,一个el-header为标题,一个el-main为主体
2.构建el-main 里面的内容------一个表格,来展示数据,并在其中每行放入操作按钮
<el-table :data="tableData">
<el-table-column
prop="name"
label="忍者名称"
width="200"
></el-table-column>
<el-table-column
prop="fight"
label="忍者战力"
width="200"
></el-table-column>
<el-table-column
prop="level"
label="忍者级别"
width="200"
></el-table-column>
<el-table-column label="操作" width="600">
<template slot-scope="scope">
<el-button
type="danger"
round
size="small"
circle="true"
@click="deleteRenZhe(scope.row.id)"
>删除忍者</el-button
>
<el-button
type="primary"
size="small"
circle="true"
@click="modifyRenZhe(scope.row.id)"
>修改忍者</el-button
>
<el-button
type="primary"
size="small"
circle="true"
@click="studySkill(scope.row.id)"
>学习技能</el-button
>
<el-button
type="primary"
size="small"
circle="true"
@click="scanSkill(scope.row.id)"
>查看技能</el-button
>
<el-button @click="addRenZhe()" type="primary">添加忍者</el-button>
</template>
</el-table-column>
</el-table>
3.开始构造相关按钮触发的函数代码
studySkill(id) {
this.dialogFormVisible = true;
this.form.id = id;
// this.study();
},
modifyRenZhe(id) {
this.dialogFormVisible2 = true;
this.form2.id = id;
// this.modify();
},
addRenZhe() {
// console.log('调用addRenZhe');
this.dialogFormVisible3 = true;
},
deleteRenZhe(id) {
var that = this.tableData;
for (var i = 0; i < that.length; i++) {
if (that[i].id == id) {
that.splice(i, 1);
}
}
alert("成功删除");
},
scanSkill(id) {
var that = this.tableData;
for (var i = 0; i < that.length; i++) {
if (that[i].id == id) {
this.gridData = that[i].skill;
}
}
this.dialogTableVisible = true;
},
4.这里每个按钮都会触发弹出框除了deleteRenZhe()
弹出框代码
<el-dialog title="技能学习" :visible.sync="dialogFormVisible">
<el-form :model="form">
<el-form-item label="技能名称" :label-width="formLabelWidth">
<el-input v-model="form.name" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="技能等级" :label-width="formLabelWidth">
<el-input v-model="form.level" autocomplete="off"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取 消</el-button>
<el-button type="primary" @click="study()">确 定</el-button>
</div>
</el-dialog>
<el-dialog title="修改忍者" :visible.sync="dialogFormVisible2">
<el-form :model="form">
<el-form-item label="忍者名" :label-width="formLabelWidth">
<el-input v-model="form2.rname" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="忍者战力" :label-width="formLabelWidth">
<el-input v-model="form2.rfight" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="忍者等级" :label-width="formLabelWidth">
<el-input v-model="form2.rlevel" autocomplete="off"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取 消</el-button>
<el-button type="primary" @click="modify()">确 定</el-button>
</div>
</el-dialog>
<el-dialog title="添加忍者" :visible.sync="dialogFormVisible3">
<el-form :model="form">
<el-form-item label="忍者名" :label-width="formLabelWidth">
<el-input v-model="form3.rname" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="忍者战力" :label-width="formLabelWidth">
<el-input v-model="form3.rfight" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="忍者等级" :label-width="formLabelWidth">
<el-input v-model="form3.rlevel" autocomplete="off"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible3 = false">取 消</el-button>
<el-button type="primary" @click="add()">确 定</el-button>
</div>
</el-dialog>
<el-dialog title="技能信息" :visible.sync="dialogTableVisible">
<el-table :data="gridData">
<el-table-column
property="name"
label="技能名"
width="150"
></el-table-column>
<el-table-column
property="level"
label="技能等级"
width="150"
></el-table-column>
</el-table>
</el-dialog>
5.这些弹出框中的确定按钮,会触发函数,来实现数据变化。
下面是对应的代码
study() {
// console.log("调用study");
var that = this.tableData;
// console.log("调用study1");
for (var i = 0; i < that.length; i++) {
// console.log("调用study1");
if (that[i].id == this.form.id) {
that[i].skill.push({ name: this.form.name, level: this.form.level });
this.dialogFormVisible = false;
alert(that[i].name + "成功学习技能:" + this.form.name);
// console.log("111",this.tableData);
return;
}
}
},
modify() {
var that = this.tableData;
for (var i = 0; i < that.length; i++) {
if (that[i].id == this.form2.id) {
that[i].name = this.form2.rname;
that[i].fight = this.form2.rfight;
that[i].level = this.form2.rlevel;
this.dialogFormVisible2 = false;
// alert("成功修改");
return;
}
}
},
add() {
// console.log('调用add');
this.tNum++;
var t = { id: "", name: "", fight: "", level: "", skill: "" },
that = this.form3;
// console.log('调用add');
t.id = this.tNum;
t.name = that.rname;
t.fight = that.rfight;
t.level = that.rlevel;
t.skill = [];
console.log("调用add");
this.tableData.push(t);
this.dialogFormVisible3 = false;
// alert("成功添加");
},
6.添加上一些样式
<style>
.el-table th {
display: table-cell !important;
}
.el-header,
.el-footer {
background-color: #b3c0d1;
color: #333;
text-align: center;
line-height: 60px;
}
.el-main {
background-color: #e9eef3;
color: #333;
text-align: center;
line-height: 160px;
}
</style>
当然按钮的样式,已经在前面的template代码里面。
三.遇到的问题以及解决方法
1.开始写好所有代码,开始测试时,发现按钮没响应。
解决方法:通过console.log()打印对应的数据信息,发发现控制台也没有变化。最后才发现template的函数名和js的函数不一样,js的函数有一些命名没有遵从驼峰法,但上面的template代码是遵守的。
2.写好页面后,发现“操作”和下面的五个按钮对不齐
解决方法:反复观察源码,发现“操作”所在的el-table-column本应该也包含五个按钮,但我当时操作和五个按钮写到两个el-table-column去了
3.弹出框的标题占位置过多啦,但没有找到对应的样式,来修正它
这里"技能学习"就占太多空白啦,本来上下一共三行大小就好看啦.
总结
通过该作业,进一步加深了我对代码规范--驼峰命名法的重要性,还有遇到前端bug后,可以采用console.log(),加在相关代码对应地方,看看是哪一步数据没拿到,还是自己封装错啦,等等
最后该项目可以通过https://lumingfei0616.github.io/webFinalHomeWork/访问
附上该项目的该组件的全部代码
<template>
<div>
<el-container>
<el-header>忍者管理系统 </el-header>
<el-main>
<el-table :data="tableData">
<el-table-column
prop="name"
label="忍者名称"
width="200"
></el-table-column>
<el-table-column
prop="fight"
label="忍者战力"
width="200"
></el-table-column>
<el-table-column
prop="level"
label="忍者级别"
width="200"
></el-table-column>
<el-table-column label="操作" width="600">
<template slot-scope="scope">
<el-button
type="danger"
round
size="small"
circle="true"
@click="deleteRenZhe(scope.row.id)"
>删除忍者</el-button
>
<el-button
type="primary"
size="small"
circle="true"
@click="modifyRenZhe(scope.row.id)"
>修改忍者</el-button
>
<el-button
type="primary"
size="small"
circle="true"
@click="studySkill(scope.row.id)"
>学习技能</el-button
>
<el-button
type="primary"
size="small"
circle="true"
@click="scanSkill(scope.row.id)"
>查看技能</el-button
>
<el-button @click="addRenZhe()" type="primary">添加忍者</el-button>
</template>
</el-table-column>
</el-table>
<el-dialog title="技能学习" :visible.sync="dialogFormVisible">
<el-form :model="form">
<el-form-item label="技能名称" :label-width="formLabelWidth">
<el-input v-model="form.name" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="技能等级" :label-width="formLabelWidth">
<el-input v-model="form.level" autocomplete="off"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取 消</el-button>
<el-button type="primary" @click="study()">确 定</el-button>
</div>
</el-dialog>
<el-dialog title="修改忍者" :visible.sync="dialogFormVisible2">
<el-form :model="form">
<el-form-item label="忍者名" :label-width="formLabelWidth">
<el-input v-model="form2.rname" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="忍者战力" :label-width="formLabelWidth">
<el-input v-model="form2.rfight" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="忍者等级" :label-width="formLabelWidth">
<el-input v-model="form2.rlevel" autocomplete="off"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取 消</el-button>
<el-button type="primary" @click="modify()">确 定</el-button>
</div>
</el-dialog>
<el-dialog title="添加忍者" :visible.sync="dialogFormVisible3">
<el-form :model="form">
<el-form-item label="忍者名" :label-width="formLabelWidth">
<el-input v-model="form3.rname" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="忍者战力" :label-width="formLabelWidth">
<el-input v-model="form3.rfight" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="忍者等级" :label-width="formLabelWidth">
<el-input v-model="form3.rlevel" autocomplete="off"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible3 = false">取 消</el-button>
<el-button type="primary" @click="add()">确 定</el-button>
</div>
</el-dialog>
<el-dialog title="技能信息" :visible.sync="dialogTableVisible">
<el-table :data="gridData">
<el-table-column
property="name"
label="技能名"
width="150"
></el-table-column>
<el-table-column
property="level"
label="技能等级"
width="150"
></el-table-column>
</el-table>
</el-dialog>
</el-main>
</el-container>
</div>
</template>
<script>
export default {
name: "showRenzhe",
methods: {
studySkill(id) {
this.dialogFormVisible = true;
this.form.id = id;
// this.study();
},
study() {
// console.log("调用study");
var that = this.tableData;
// console.log("调用study1");
for (var i = 0; i < that.length; i++) {
// console.log("调用study1");
if (that[i].id == this.form.id) {
that[i].skill.push({ name: this.form.name, level: this.form.level });
this.dialogFormVisible = false;
alert(that[i].name + "成功学习技能:" + this.form.name);
// console.log("111",this.tableData);
return;
}
}
},
modifyRenZhe(id) {
this.dialogFormVisible2 = true;
this.form2.id = id;
// this.modify();
},
modify() {
var that = this.tableData;
for (var i = 0; i < that.length; i++) {
if (that[i].id == this.form2.id) {
that[i].name = this.form2.rname;
that[i].fight = this.form2.rfight;
that[i].level = this.form2.rlevel;
this.dialogFormVisible2 = false;
// alert("成功修改");
return;
}
}
},
addRenZhe() {
// console.log('调用addRenZhe');
this.dialogFormVisible3 = true;
},
add() {
// console.log('调用add');
this.tNum++;
var t = { id: "", name: "", fight: "", level: "", skill: "" },
that = this.form3;
// console.log('调用add');
t.id = this.tNum;
t.name = that.rname;
t.fight = that.rfight;
t.level = that.rlevel;
t.skill = [];
console.log("调用add");
this.tableData.push(t);
this.dialogFormVisible3 = false;
// alert("成功添加");
},
deleteRenZhe(id) {
var that = this.tableData;
for (var i = 0; i < that.length; i++) {
if (that[i].id == id) {
that.splice(i, 1);
}
}
alert("成功删除");
},
scanSkill(id) {
var that = this.tableData;
for (var i = 0; i < that.length; i++) {
if (that[i].id == id) {
this.gridData = that[i].skill;
}
}
this.dialogTableVisible = true;
},
},
mounted() {},
data() {
return {
//{sdept,smajor,sclass,sname}
tNum: 5,
tableData: [
{ id: 0, name: "漩涡鸣人(幼年)", fight: 20, level: "C", skill: [] },
{ id: 1, name: "宇智波佐助(幼年)", fight: 20, level: "C", skill: [] },
{ id: 2, name: "小樱(幼年)", fight: 20, level: "C", skill: [] },
{
id: 3,
name: "卡卡西(青年)",
fight: 100,
level: "A",
skill: [
{ name: "雷切", level: "S" },
{ name: "螺旋丸", level: "S" },
],
},
{ id: 4, name: "宇智波佐助(博人传)", fight: 20, level: "D", skill: [] },
],
// bridgename: "",
tName: "",
tLevel: "",
tFight: "",
tSkill: "",
dialogFormVisible: false,
dialogFormVisible2: false,
dialogFormVisible3: false,
form: {
id: "",
name: "",
level: "",
delivery: false,
type: [],
},
form2: {
id: "",
rname: "",
rfight: "",
rlevel: "",
},
form3: {
id: "",
rname: "",
rfight: "",
rlevel: "",
},
gridData: [],
dialogTableVisible: false,
};
},
};
</script>
<style>
.el-table th {
display: table-cell !important;
}
.el-header,
.el-footer {
background-color: #b3c0d1;
color: #333;
text-align: center;
line-height: 60px;
}
.el-main {
background-color: #e9eef3;
color: #333;
text-align: center;
line-height: 160px;
}
</style>