<!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: 100%;
width: 100%;
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="5">
<add></add>
</td>
</tr>
</table>
<script type="text/javascript">
var app = angular.module("mapp", []);
app.controller("mtable", function($scope) {
// 数据源
$scope.items = [{
name: "张三",
age: 20,
id: 111,
interest: "看书"
}, {
name: "李四",
age: 22,
id: 222,
interest: "写字"
}, {
name: "王二小",
age: 23,
id: 333,
interest: "电影"
}, {
name: "隔壁老王",
age: 5,
id: 444,
interest: "泡妞"
}];
// 为 reset功能而做的准备,ng中数组的复制,注意使用 angular.copy()和不使用的效果
$scope.resetArr = angular.copy($scope.items); // 接收删除事件,使用 splice()而不是slice()
$scope.$on("remove", function(event, data) {
$scope.items.splice(data, 1);
}); // 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的一些操作(示例大于实际)

    本文转自:http://www.cnblogs.com/lin-js/p/linJS.html <!DOCTYPE html> <html> <head> < ...

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

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

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

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

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

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

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

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

  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. 201621123012 《Java程序设计》第7周学习总结

    1. 本周学习总结 1.1 思维导图:Java图形界面总结 答: 1.2 可选:使用常规方法总结其他上课内容. 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. ...

  2. 【转载】Java资源大全中文版

    Java资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-java 就是 akullpp 发起维护的 Java 资源列表,内容 ...

  3. javascript前端导出csv表格

    使用场景 后台统计经常要展示各种各样的表格数据,几乎每个表格展示都会伴随着数据的导出. 之前的解决方案都是通过发起一个相同查询参数(querystring)的导出请求(action=export),由 ...

  4. asp手动给combox赋值

    ASPxComboBox cbSex = ASPxGridView1.FindEditFormTemplateControl("cmbUSER_SEX") as ASPxCombo ...

  5. Georgia and Bob POJ - 1704 阶梯Nim

    $ \color{#0066ff}{ 题目描述 }$ Georgia and Bob decide to play a self-invented game. They draw a row of g ...

  6. Axure 原型图

    Axure RP 9最新版软件及汉化包下载 https://www.axure.com.cn/78629/   点击下载 lang语言包放在根目录即可 Axure各种版本注册码 | 最新Axure R ...

  7. Windows Server 2008 R2中IIS7.5配置完网站权限不足问题的解决办法:

    Windows Server 2008 R2中IIS7.5配置完网站权限不足问题的解决办法:常见问题:HTTP 错误 500.0 - Internal Server Error无法显示页面,因为发生内 ...

  8. 洛谷 P4001 [ICPC-Beijing 2006]狼抓兔子

    题目描述 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形: ...

  9. Windows10 下安装配置IIS + MySQL5.7.19 + nginx1.12.1 + php7.1.7

    环境: VMWare Workstation Player12 Windows10 Pro x64 一.安装系统 vmware 会采用 fast install 方式很快装完,无需配置什么. 二.配置 ...

  10. Covering(矩阵快速幂)

    Bob's school has a big playground, boys and girls always play games here after school.  To protect b ...