赞
踩
递归调用
一、什么叫做函数的递归调用?
函数直接或间接的调用自己就是函数的递归调用。
【函数调用自己】
二、【算法1】通过函数的递归调用计算n!
n!=1*2*3*...*n 当n=5时:5!=1*2*3*4*5 n!=1 (当n=1) n*(n-1)! (当n>1) 当n=5时:5*4! 4!=4*3! 3!=3*2! 2!=2*1! function fact(n){ if(n===1){ return 1; }else{ return n*fact(n-1); } } var x=5; var y=fact(x); console.log("x!=" + y);
三、递归算法的组成部分:
1、递归算法有 递推阶段 和 回归阶段 。
2、要想实现递归算法,必须找出递推公式和回归初值。
四、【算法2】通过函数的递归调用解决斐波那契数列。
1,1,2,3,5,8,13,…
要求:输出斐波那契数列的第n=20项。
fib(n)=1, (当n=1,或n=2)
fib(n-1)+fib(n-2) (当n>2时)
function fib(n){
if(n===1 || n===2){
return 1;
}else{
return fib(n-1)+fib(n-2);
}
}
var y=fib(7);
console.log(y);
五、函数如何在内部调用自己:
1、利用函数名直接调用自己。
2、利用arguments.callee()调用自己。
六、Element UI框架下树形表格的使用:
1、树形表格绑定的数据必须具备children属性。
(1)children属性取值是一个数组。
(2)children数组中的元素是对象,对象的属性和第一层对象的属性一致。
2、表格必须具备row-key属性,用来为行设置主关键字。
3、为表格设置 :tree-props="{children:‘children’,hasChildren:‘hasChildren’}"
(1)键名children:指定数据中哪个字段用来存放子元素数据。
(2)键名hasChildren:取值为逻辑值,指定数据是否具备子元素。
例:
<template> <div class="hello"> <!--修改对话框--> <el-dialog title="设备数据编辑" :visible.sync="isVisible" > <el-form label-width="100px" :model="formData" > <el-form-item label="配件名称"> <el-input v-model="formData.deviceName"></el-input> </el-form-item> <el-form-item label="数量"> <el-input v-model="formData.count"></el-input> </el-form-item> <el-form-item label="供应商"> <el-input v-model="formData.provider"></el-input> </el-form-item> <el-form-item label="联系电话"> <el-input v-model="formData.phone"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="editData">编辑数据</el-button> <el-button type="danger">取消</el-button> </el-form-item> </el-form> </el-dialog> <el-row> <el-col :span="18" :offset="3"> <el-table :tree-props="{children:'children',hasChildren:'hasChildren'}" row-key="deviceId" lazy :data="tableData" :load="tableLoad" > <el-table-column label="配件名称" prop="itemName" align="center"></el-table-column> <el-table-column label="数量" prop="count" align="center"></el-table-column> <el-table-column label="供应商" prop="provider" align="center"></el-table-column> <el-table-column label="联系电话" prop="phone" align="center"></el-table-column> <el-table-column label="操作" width="200px" align="center"> <template slot-scope="scope"> <el-button size="mini" type="primary" @click="openDialog(scope.row)">编辑</el-button> <el-button size="mini" type="danger" @click="deleteData(scope.row.deviceId)" :disabled="scope.row.count!=0 && scope.row.hasChildren===true">删除 </el-button> </template> </el-table-column> </el-table> </el-col> </el-row> </div> </template> <script> export default { name: 'HelloWorld', data() { return { tableData: [], isVisible: false, formData: { deviceName: '', count: '', provider: '', phone: '' }, currentId: '' } }, mounted() { let url = "/api/getDevice.jsp" this.$axios.get(url).then(res => { for (let i in res.data) { let temp = { deviceId: res.data[i].did, itemName: res.data[i].devicename, count: res.data[i].count, provider: res.data[i].provider, phone: "", children: null, hasChildren: true } this.tableData.push(temp) } }) }, methods: { tableLoad(row, treeNode, resolve) { if (row.deviceId.length === 1) { //用户单击了第一层箭头,封装第二层数据 let url = "/api/getBrand.jsp" let data = { params: { did: row.deviceId } } this.$axios.get(url, data).then(res => { let tempArray = [] for (let i in res.data) { let temp = { deviceId: row.deviceId + (parseInt(i) + 1), itemName: res.data[i].brand, count: res.data[i].count, provider: res.data[i].address, phone: res.data[i].phone, children: null }; if (parseInt(res.data[i].count) === 0) { temp.hasChildren = false } else { temp.hasChildren = true } tempArray.push(temp) } row.children = tempArray; resolve(tempArray) }) } if (row.deviceId.length === 2) { //用户单击了第二层箭头,封装第三层数据 let url = "/api/getProduct.jsp" let data = { params: { brand: row.itemName } } this.$axios.get(url, data).then(res => { let tempArray = [] for (let i in res.data) { let temp = { deviceId: row.deviceId + (parseInt(i) + 1), itemName: res.data[i].proname, count: res.data[i].count, provider: res.data[i].address, phone: res.data[i].phone, children: null, hasChildren: false } tempArray.push(temp) } row.children = tempArray resolve(tempArray) }) } }, // 点击编辑出现弹框 openDialog(row) { this.formData.deviceName = row.itemName; this.formData.count = row.count; this.formData.provider = row.provider; this.formData.phone = row.phone; this.currentId = row.deviceId; this.isVisible = true }, //重点:递归调用找到元素 findObject(obj, id) { for (let i in obj) { if (obj[i].deviceId === id) { return obj[i] } else { if (obj[i].children) { return this.findObject(obj[i].children, id) } } } return -1 }, // 点击编辑出现弹框|编辑数据 editData() { let temp = this.findObject(this.tableData, this.currentId); temp.itemName = this.formData.deviceName; temp.count = this.formData.count; temp.provider = this.formData.provider; temp.phone = this.formData.phone; this.isVisible = false }, //重点:递归调用删除元素 deleteObject(obj,id){ for(let i in obj){ if(obj[i].deviceId===id){ obj.splice(i,1) }else { if(obj[i].children){ this.deleteObject(obj[i].children,id) } } } }, //点击删除 deleteData(id) { this.$confirm("您确定要删除这条记录吗?").then(()=>{ this.deleteObject(this.tableData,id) }) } } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> </style>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。