1. 程式人生 > >依賴於angular的table組件

依賴於angular的table組件

down 拖拽排序 ces onmouseup app 寬度調整 還需 記錄 ble

組件實現了以下功能

1. 列寬可動態拖動

2. 列數據排序

3. 列過濾

4. 列位置自由調整

除了需要引入angular.js(我用的是1.4.6版本),還需要引用一個angular衍生出來的插件ngdraggable.js

插件地址:https://github.com/fatlinesofcode/ngDraggable/blob/master/example.html

為了方便,我直接全部放在頁面上展示吧

<body ng-app="myApp">
<div> 
    <div ng-controller="myCon">
        <table 
id="myTab" cellspacing="0" cellpadding="2" border="1" style="text-align:center;table-layout: fixed;"> <thead> <tr> <th width="50">序號</th> <th width="{{head.width}}" ng-repeat="head in headList">{{head.value}}</
th> </tr> <tr> <th width="50"></th> <th width="{{head.width}}" ng-repeat="head in headList"><input style="width:80px;" type="text" ng-model="filterInput[head.code]" ng-change="filtrate(filterInput[head.code],head.code)"
/></th> </tr> </thead> <tbody> <tr ng-repeat="data in tableData"> <td width="50">{{$index}}</td> <td width="{{head.width}}" ng-repeat="head in headList" repeat-finish>{{data[head.code]}}</td> </tr> </tbody> </table> <p> <button ng-click="sort(‘name‘)">姓名排序</button> <button ng-click="sort(‘sex‘)">性別排序</button> <button ng-click="sort(‘age‘)">年齡排序</button> <button ng-click="sort(‘from‘)">籍貫排序</button> <button ng-click="sort(‘like‘)">愛好排序</button> </p> <br /> <br /> <p>下面可以拖拽排序(table列排序)</p> <ul> <li ng-repeat="head in headList" ng-drop="true" ng-drop-success="onDropComplete($index, $data,$event)" style="cursor:pointer;"> <p ng-drag="true" ng-drag-data="head">{{head.value}}</p> </li> </ul> </div> </div> <script src="angular.js"></script> <script src="ngDraggable.js"></script> <script> var headList = [ {code:"name",value:"姓名",width:"150"}, {code:"sex",value:"性別",width:"150"}, {code:"age",value:"年齡",width:"150"}, {code:"from",value:"籍貫",width:"150"}, {code:"like",value:"愛好",width:"200"} ] var tableData = [ {name:"老王",age:25,sex:"",from:"隔壁",like:"FQ"}, {name:"小明",age:18,sex:"",from:"山東",like:"遊泳、爬山"}, {name:"吳樊",age:3,sex:"",from:"廣東",like:"看書"}, {name:"科比",age:45,sex:"",from:"美國",like:"打籃球"}, {name:"紀鮁強",age:25,sex:"不詳",from:"火星",like:"聊天"}, {name:"敏怡",age:23,sex:"",from:"廣東",like:"看書"}, {name:"貂蟬",age:25,sex:"",from:"洛陽",like:"彈琴"}, {name:"姜子牙",age:85,sex:"",from:"河南",like:"釣魚"}, {name:"小丸子",age:24,sex:"",from:"日本",like:"吃東西"} ] var sortData = {name : "+",sex : "+",age : "+",from : "+",like : "+"} var app = angular.module(myApp, [ngDraggable]); app.controller(myCon,["$scope","$filter",function($scope,$filter){ var table = { init : function(){ //初始化 $scope.headList = headList; $scope.tableData = tableData; $scope.sortData = sortData; $scope.filterInput = {}; $scope.stretch = this.stretch; $scope.onDropComplete = this.onDropComplete; $scope.filtrate = this.filtrate; $scope.sort = this.sort; }, onDropComplete : function (index, obj, evt) { //拖拽排序方法 var otherObj = $scope.headList[index]; var otherIndex = $scope.headList.indexOf(obj); $scope.headList.splice(otherIndex,1); $scope.headList.splice(index,0,obj); }, filtrate : function(value,code){ //列過濾方法 $scope.tableData = tableData; switch(code){ case name : $scope.tableData = $filter(filter)($scope.tableData,{name : value}); break; case sex : $scope.tableData = $filter(filter)($scope.tableData,{sex : value}); break; case age : $scope.tableData = $filter(filter)($scope.tableData,{age : value}); break; case from : $scope.tableData = $filter(filter)($scope.tableData,{from : value}); break; case like : $scope.tableData = $filter(filter)($scope.tableData,{like : value}); break; } }, sort : function(row){ //列排序方法 var sort = $scope.sortData[row]; var val = sort + row; $scope.tableData = $filter(orderBy)($scope.tableData,val); if(sort == "+"){ $scope.sortData[row] = "-" }else{ $scope.sortData[row] = "+" } }, stretch : function(){ //列寬度調整方法 var myTAbId = document.getElementById("myTab"); var tTD; //用來存儲當前更改寬度的Table Cell,避免快速移動鼠標的問題 var wData = {}; var unset = myTAbId.rows[0].cells.length - $scope.headList.length;//不在headList遍歷的列數 for (var j = 0; j < myTAbId.rows[0].cells.length; j++) { myTAbId.rows[0].cells[j].index = j; if(unset - 1<j){ myTAbId.rows[0].cells[j].onmousedown = function (event) { //記錄單元格 tTD = this; if (event.offsetX > tTD.offsetWidth - 10) { tTD.mouseDown = true; tTD.oldX = event.clientX; tTD.oldWidth = tTD.offsetWidth; } }; myTAbId.rows[0].cells[j].onmouseup = function (event) { //結束寬度調整 if (tTD == undefined) tTD = this; tTD.mouseDown = false; tTD.style.cursor = default; $scope.headList[this.index-unset].width = tTD.width; }; myTAbId.rows[0].cells[j].onmousemove = function (event) { //更改鼠標樣式 if (event.offsetX > this.offsetWidth - 10) this.style.cursor = col-resize; else this.style.cursor = default; //取出暫存的Table Cell if (tTD == undefined) tTD = this; //調整寬度 if (tTD.mouseDown != null && tTD.mouseDown == true) { tTD.style.cursor = default; if (tTD.oldWidth + (event.clientX - tTD.oldX)>0) tTD.width = tTD.oldWidth + (event.clientX - tTD.oldX); //調整列寬 // tTD.style.width = tTD.width; tTD.style.cursor = col-resize; //調整該列中的每個Cell myTAbId = tTD; while (myTAbId.tagName != TABLE) myTAbId = myTAbId.parentElement; for (var k = 0; k < myTAbId.rows.length; k++) { myTAbId.rows[k].cells[tTD.cellIndex].width = tTD.width; } } }; } } } } table.init(); }]); app.directive(repeatFinish,[function(){ return { link: function($scope,$element,$attr){ if($scope.$last == true){ $scope.stretch(); } } }; }]); </script> </body>

小生剛入門angular不久,所以有什麽寫的不好的地方,請各位大神指出

依賴於angular的table組件