本文转自:http://www.cnblogs.com/lin-js/p/linJS.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="lib/angular.js"></script>
<style type="text/css">
table {
border: 1px solid #ccc;
border-collapse: collapse;
}
td,
th {
height: 40px;
width: 90px;
border: 1px solid #ccc;
text-align: center;
}
input {
height: %;
width: %;
border: none;
}
.readonly {
background: green;
}
.active {
background: #ddd;
}
td.options {
width: 400px;
text-align: left;
}
td.options>input {
width: 100px;
}
</style>
</head> <body ng-app="mapp"> <table ng-controller="mtable">
<tr>
<th>姓名</th>
<th>年龄</th>
<th>学号</th>
<th>兴趣</th>
<th>Options</th>
</tr> <tr ng-repeat="item in items" ng-controller="row">
<td>
<input type="text" ng-model="item.name" ng-readonly="varlist.isreadonly" value={ {item.name}} ng-class="{true:'inactive',false:'active'}[varlist.isreadonly]" placeholder="name">
</td>
<td>
<input type="text" ng-model="item.age" ng-readonly="varlist.isreadonly" value={ {item.age}} ng-class="{true:'inactive',false:'active'}[varlist.isreadonly]" placeholder="age">
</td>
<td>
<input type="text" ng-model="item.id" ng-readonly="varlist.isreadonly" value={ {item.id}} ng-class="{true:'inactive',false:'active'}[varlist.isreadonly]" placeholder="id">
</td>
<td>
<input type="text" ng-model="item.interest" ng-readonly="varlist.isreadonly" value={ {item.interest}} ng-class="{true:'inactive',false:'active'}[varlist.isreadonly]" placeholder="interseting">
</td>
<td class="options">
<!-- 编辑 -->
<edit></edit>
<!-- 删除当前行 -->
<delete></delete>
<!-- 放弃所有编辑 -->
<reset></reset>
</td>
</tr>
<tr>
<!-- 增加一行 -->
<td colspan="">
<add></add>
</td>
</tr>
</table>
<script type="text/javascript">
var app = angular.module("mapp", []);
app.controller("mtable", function($scope) {
// 数据源
$scope.items = [{
name: "张三",
age: ,
id: ,
interest: "看书"
}, {
name: "李四",
age: ,
id: ,
interest: "写字"
}, {
name: "王二小",
age: ,
id: ,
interest: "电影"
}, {
name: "隔壁老王",
age: ,
id: ,
interest: "泡妞"
}];
// 为 reset功能而做的准备,ng中数组的复制,注意使用 angular.copy()和不使用的效果
$scope.resetArr = angular.copy($scope.items); // 接收删除事件,使用 splice()而不是slice()
$scope.$on("remove", function(event, data) {
$scope.items.splice(data, );
}); // ng-repeat 重复元素上面定义的ng-controller互不影响
}).controller("row", function($scope) {
$scope.varlist = {
isreadonly: true
}
// 接收edit事件,因为不用jquery,所以使用ng-readonly指令来操作
$scope.$on("edit", function() {
$scope.varlist.isreadonly = false;
});
});
// 下面是四个指令 app.directive("edit", function() {
return {
restrict: "E",
replace: true,
// 这里是在模版中使用ng-click绑定事件,请试试在指令 上绑定事件的方式,有坑
template: "<input type = 'button' value = 'edit' ng-click='edit()'>",
link: function(scope, element, attr) {
scope.edit = function() {
// 传递的true值是给 $scope.varlist.isreadonly 使用的,意思是改变只读状态
scope.$emit("edit", true);
} }
}
}).directive("delete", function() {
return {
restrict: "E",
replace: true,
template: "<input type = 'button' value = 'delete' ng-click='remove($index)'>",
link: function(scope, element, attr) {
scope.remove = function($index) {
scope.$emit("remove", $index); }
}
}
}).directive("reset", function() {
return {
restrict: "E",
replace: true, template: "<input type = 'button' value = 'reset' ng-click='reset($index)'>",
link: function(scope, element) {
//代码很简单,但是你丫就是想不起来使用angualr.copy()
scope.reset = function($index) {
scope.items[$index] = angular.copy(scope.resetArr[$index]);
} }
}
}).directive("add", function() {
return {
restrict: "E",
template: "<button ng-click = 'add()'>增加新行</button>",
replace: true,
link: function(scope) {
scope.add = function() {
// 数据驱动
scope.items.push({});
}
}
}
});
</script>
</body>
</html>

[转]AngularJS 实现 Table的一些操作(示例大于实际)的更多相关文章

  1. AngularJS 实现 Table的一些操作(示例大于实际)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script ty ...

  2. Hudi 数据湖的插入,更新,查询,分析操作示例

    Hudi 数据湖的插入,更新,查询,分析操作示例 作者:Grey 原文地址: 博客园:Hudi 数据湖的插入,更新,查询,分析操作示例 CSDN:Hudi 数据湖的插入,更新,查询,分析操作示例 前置 ...

  3. AngularJS快速入门指南19:示例代码

    本文给出的大部分示例都可以直接运行,通过点击运行按钮来查看结果,同时支持在线编辑代码. <div ng-app=""> <p>Name: <input ...

  4. C#文件的拆分与合并操作示例

    C#文件的拆分与合并操作示例代码. 全局变量定义 ;//文件大小 //拆分.合并的文件数 int count; FileInfo splitFile; string splitFliePath; Fi ...

  5. java-redis集合数据操作示例(三)

    redis系列博文,redis连接管理类的代码请跳转查看<java-redis字符类数据操作示例(一)>. 一.集合类型缓存测试类 public class SetTest { /** * ...

  6. java-redis列表数据操作示例(二)

    接上篇博文<java-redis字符类数据操作示例(一)>,redis连接管理类的代码请跳转查看. 一.列表类型缓存测试类 public class ListTest { /** * 主测 ...

  7. AngularJS基于MVC的复杂操作案例

    AngularJS基于MVC的复杂操作案例 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...

  8. 文件操作示例脚本 tcl

    linux 下,经常会对用到文件操作,下面是一个用 tcl 写的文件操作示例脚本: 其中 set f01 [open "fix.tcl" w] 命令表示 打开或者新建一个文件“fi ...

  9. 聊聊flink Table的groupBy操作

    本文主要研究一下flink Table的groupBy操作 Table.groupBy flink-table_2.11-1.7.0-sources.jar!/org/apache/flink/tab ...

随机推荐

  1. django drf viewsets和routers

    1.定义VIew from django.shortcuts import render from rest_framework.views import APIView from rest_fram ...

  2. django drf 深入ModelSerializer

    serializer用起来稍微麻烦,可以使用ModelSerializer,类似于django里的Form与ModelForm 1.定义ModelSerializer from rest_framew ...

  3. a标签点击时跳出确认框

    在做一些删除等的操作时,在跳转链接前,需要弹出一个确认框确认,避免误点. 方法一: <a  href="http://www.baidu.com" onClick=" ...

  4. python之爬虫(二)爬虫的原理

    在上文中我们说了:爬虫就是请求网站并提取数据的自动化程序.其中请求,提取,自动化是爬虫的关键!下面我们分析爬虫的基本流程 爬虫的基本流程 发起请求通过HTTP库向目标站点发起请求,也就是发送一个Req ...

  5. 深入了解java虚拟机(JVM) 第二章 内存区域---栈空间

    一.栈的含义 用于描述方法执行的动态内存模型,简单来说就是代码中的方法模块.主要用于局部变量表.栈的大小在编译器中就已经分配了空间,在进入一个方法时,这个方法需要栈帧分配多少内存是固定的,运行期间是不 ...

  6. MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled

    初试redis,删除或者修改值的时候报的错,解决方式是运行命令: 127.0.0.1:6379> config set stop-writes-on-bgsave-error no

  7. screen新建窗口,环境变量跟原来不一致。

    昨天为了方便npm安装全局包,我把环境变量重新配置了. 然后,在项目中引用全局包没有出问题.但是后来我在screen里面引用全局包,报错说找不到. 使用#npm list -g命令 发现昨天的全局包都 ...

  8. nginx高性能WEB服务器系列之八--nginx日志分析与切割

    nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...

  9. 2018 noip AFO? 祭

    \(\color{#00ffff}{DAY \ \ -1}\) 一下午,都在说注意事项,也没刷题 学姐说,考前远离rqj,会被%,掉rp(雾 晚上8点20,内心有一丝悸动... 于是去找教练谈话 在教 ...

  10. [原创]Heroku简单部署指南

    目录 1. 设置 1.1 环境依赖 1.2 Heroku 客户端安装 1.3 登录 2. 应用 2.1 创建 2.2 查看日志 2.3 附加组件 2.4 交互式 Shell 2.5 定义 配置变量 2 ...