本文转自: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. Huawei .V3 Extention audio Play

    相关dll下载 链接 using System;using System.IO;using System.Threading;using Alvas.Audio;using NAudio.Wave; ...

  2. CLR via C# 读书笔记-21.托管堆和垃圾回收

    前言 近段时间工作需要用到了这块知识,遂加急补了一下基础,CLR中这一章节反复看了好多遍,得知一二,便记录下来,给自己做一个学习记录,也希望不对地方能够得到补充指点. 1,.托管代码和非托管代码的区别 ...

  3. Qt学习(二):项目学习

    1. 概述 Qt做UI设计后,通常会生成Form Files文件夹下的ui文件.Generated File文件夹下的ui_xxx等头文件和源文件.Header Files文件夹下的头文件.Sourc ...

  4. 原创:各种normalize函数实现的性能和精度大比拼

    ///////////////////////////////////////////////////////////////////////// // // Performance benchmar ...

  5. Android通知栏介绍与适配总结(上篇)

    此文已由作者黎星授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 由于历史原因,Android在发布之初对通知栏Notification的设计相当简单,而如今面对各式各样的通知 ...

  6. SDOI2010粟粟的书架

    题目传送:https://www.luogu.org/problemnew/show/P2468 这是一个二合一的题目,前50% \(n!=1\)的分数中,我们考虑用动态规划来做. 设\(sum[i] ...

  7. read_csv 函数

    转载自 https://www.cnblogs.com/datablog/p/6127000.html pandas.read_csv参数整理 读取CSV(逗号分割)文件到DataFrame也支持文件 ...

  8. Objective-C适用C数学函数 <math.h>

    在实际工作中有些程序不可避免的需要使用数学函数进行计算,比如地图程序的地理坐标到地图坐标的变换.Objective-C做为ANSI C的扩展,使用C标准库头文件<math.h>中定义的数学 ...

  9. native2ascii -- 编码转化工具

    参考文档 http://blog.chinaunix.net/uid-692788-id-2681133.html 功能说明 Java 编译器和其它 Java 工具只能处理含有 Latin-1 和/或 ...

  10. CoreImage 图片处理

    CoreImage 是苹果 iOS5新增的一个 OC 框架,提供了强大的图像处理功能, 用于对基于像素的图像进行操作与分析, 提供了很多滤镜(Filter),形成强大的自定义效果 CIImage 类 ...