何利用AngularJS态创建表格态赋值
第二步body元素添加ng-controller指令并面添加图所示:
何利用AngularJS态创建表格态赋值
第三步编写AngularJS初始化函数并声明控制器图所示:
何利用AngularJS态创建表格态赋值
第四步预览该静态页面浏览器查看页面效显示输入框按钮图所示:
何利用AngularJS态创建表格态赋值
第五步控制器添加变量model变量赋值数组图所示:
何利用AngularJS态创建表格态赋值
第六步表格table循环model变量遍历该数组并给表格赋值图所示:
何利用AngularJS态创建表格态赋值
注意事项
注意AngularJS态获取表格数据
注意AngularJS态赋值
angularjs处理动态菜单的实现方法:
1、核心angularjs代码:
var testImg=angular.module("appTest",["ms.leafMenu"])
.controller('testCtr',['$scope',function($scope){
$scope.data=[{"id":"1","pid":"0","name":"第一行","children":[{"id":"3","pid":"1","name":"第一行1.1"},{"id":"4","pid":"1","name":"第一行1.2"}]},{"id":"2","pid":"0","name":"第二行","children":[{"id":"5","pid":"2","name":"第二行2.1"}]}]
}])
angular.module("ms.leafMenu",[])
.directive('msLeafMenu',['$compile',function($compile){
return {
restrict:'EA',
transclude: true,
replace: false,
//template:"<li></li>",
scope:{
data:'=?',
id:'@?',
pid:'@?',
pvalue:'@?',
showname:'@?',
isstandard:'@?'
},
link:function(scope,element,attrs,leafController){
创建节点数据的方法:
function createTreeData(id,pid,pvalue){
var newData=[]
angular.forEach(scope.data,function(item,index){
if(item[pid]==pvalue){
var children=createTreeData(id,pid,item[id])
if(children &&children.length>0){
item.children=children
}
newData.push(item)
}
})
return newData
}
if(!scope.isstandard){
scope.data=createTreeData(scope.id,scope.pid,scope.pvalue)
}
//向节点增加数据
element.append($compile('<ul class="ms_leaf_menu_group"><li ng-repeat="row in data" ng-class="{ms_parent_item:(row.children &&row.children.length>0),ms_child_item:(!row.children || row.children.length<=0)}"><div ng-click="toogle($index)"><a >{{row[showname]}}</a><span class="glyphicon" ng-class="{\'glyphicon-chevron-right\':(row.children &&row.children.length>0 &&!row.isopen),\'glyphicon-chevron-down\':(row.children &&row.children.length>0 && row.isopen)}" aria-hidden="true"></span></div><div ng-show="row.isopen"><ms-leaf-menu data="row.children" id="id" pid="pid" showname="{{showname}}" pvalue="{{row[id]}}"></ms-leaf-menu></div></li></ul>')(scope))
//此处是关键,调用入口处,需要找到index
scope.toogle=function(index){
scope.data[index]["isopen"]=!scope.data[index]["isopen"]
}
}
}
}])
</script>
2、html代码:
<body ng-app="appTest">
<div ng-controller="testCtr" style=" width:200pxmargin-left:automargin-right:auto">
<ms-leaf-menu data="data" id="id" pid="pid" showname="name" pvalue="0"></ms-leaf-menu>
</div>
</body>
3、效果图
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)