本文转自: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. 关于.NET C#调用Sqlite的总结一

    --前记 由于自己的密码太多又不容易记住,经常性的会忘记.所以想找个管理软件管理下,可用网上下载的担心不安全.于是自己开始动手写个属于自己的密码管理软件. 因为自己一直做的是WEB开发,对WPF懂的不 ...

  2. [Asp.net Mvc]为js,css静态文件添加版本号

    方式一: 思路 string version = ViewBag.Version; @Scripts.RenderFormat("<script type=\"text/ja ...

  3. NOI2014起床困难综合症

    Description 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚持与起床困难综合症作斗争.通过研究相关文献,他找 ...

  4. Shell中反引号和$()的区别

    Shell中可以用来实现变量代换的命令有两种,一种是由反引号括起来的一条命令另一种是由$()括起来一条命令,shell先执行这条命令,然后见输出结果立刻代换到当前命令行中. 例如定义一个变量存放dat ...

  5. Archlinux下virtualbox报错'/sbin/rcvboxdrv setup'

    因为刚刚换成archlinux系统,安装virtualbox的时候报错了.如下图: 可是怎么解决呢?我看了很多资料大多数是ubuntu的,没有archlinux的. 但是原理都差不多我借着也就研究出来 ...

  6. 01Trie树 CF923C Perfect Security

    CF923C Perfect Security 上下各n个数,求一种排列p,使上面的数i异或pi成为新的数i,求方案另字典序最小,输出该结果 01Trie树. 记录每个节点经过多少次. 每一次查询的时 ...

  7. [inside hotspot] 汇编模板解释器(Template Interpreter)和字节码执行

    [inside hotspot] 汇编模板解释器(Template Interpreter)和字节码执行 1.模板解释器 hotspot解释器模块(hotspot\src\share\vm\inter ...

  8. c++11 enable_shared_from_this

    本质的原因:raw data和引用计数的管理块,可能是分开的 使用场景: 需要在对象中得到shared ptr,  错误的代码:直接构造新的shared_ptr<A>对象,两个shared ...

  9. char * 与char []探究理解

    问题引入 以前一直认为二者是一样的,今天突然发现他们还是有很大的不同的.例如char *a = "abc"和char b[] = "abc",当我使用strca ...

  10. 微信智能机器人助手,基于hook技术,自动聊天机器人

    下载地址: 链接:https://pan.baidu.com/s/1N5uQ3gaG2IZu7f6EGUmBxA 提取码:md7z 复制这段内容后打开百度网盘手机App,操作更方便哦 微信智能助手说明 ...