1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
//script function MessageCenterController($scope, $timeout, $filter) { 'ngInject'; $scope.tabs = [ { heading: "通知", messages: [ { type: 'invitation', from: { name: 'Janny' }, brief: 'say hello', id: '0001' },{ type: 'task', from: { name: 'Hokinson' }, brief: 'say hello', id: '0002' },{ type: 'invitation', from: { name: 'Gray' }, brief: 'say hello', id: '0003' },{ type: 'invitation', from: { name: 'Dammy' }, brief: 'say hello', id: '0004' },{ type: 'invitation', from: { name: 'Jasmin' }, brief: 'say hello', id: '0005' },{ type: 'invitation', from: { name: 'Tommy' }, brief: 'say hello', id: '0006' },{ type: 'invitation', from: { name: 'John' }, brief: 'say hello', id: '0007' },{ type: 'invitation', from: { name: 'lulu' }, brief: 'say hello', id: '0008' },{ type: 'invitation', from: { name: 'May' }, brief: 'say hello', id: '0009' },{ type: 'invitation', from: { name: 'Amy' }, brief: 'say hello', id: '0010' },{ type: 'invitation', from: { name: 'Kim' }, brief: 'say hello', id: '0011' },{ type: 'invitation', from: { name: 'Lily' }, brief: 'say hello', id: '0012' },{ type: 'invitation', from: { name: 'Ann' }, brief: 'say hello', id: '0013' } ] },{ heading: "待处理", messages: [] } ]; $scope.messageTransform = function($event, message, idx) { let index, indexInSelected; switch (idx) { case 0: index = findMessageIndex(message.id, $scope.tabs[0].messages); $scope.tabs[0].messages.splice(index,1); indexInSelected = findMessageIndex(message.id, selectedMessages); if(indexInSelected >= 0) { selectedMessages.splice(index,1); } $scope.tabs[1].messages.unshift(message); break; case 1: indexInSelected = findMessageIndex(message.id, selectedPendings); if(indexInSelected >= 0) { selectedPendings.splice(index,1); } index = findMessageIndex(message.id, $scope.tabs[1].messages); $scope.tabs[1].messages.splice(index,1); $scope.tabs[0].messages.unshift(message); break; } }; var selectedMessages = []; var selectedPendings = []; $scope.updateChecked = function($event, item, idx) { let target = angular.element($event.target).siblings('input'); let action = target.attr('checked') ? "remove" : "add"; $scope.updateSelected(action, item, idx); }; $scope.updateSelected = function(action, selection ,idx) { let result, index; switch (idx) { case 0: if(action === "add") { if(selection.cOnstructor=== Array) { for(let i=0;i<$scope.tabs[0].messages.length;i++) { result = $filter('filter')(selectedMessages, {id: $scope.tabs[0].messages[i].id}); if(result.length == 0) { selectedMessages.push($scope.tabs[0].messages[i]); result = undefined; } } } else { selectedMessages.push(selection); } } else if(action === "remove") { if(selection.cOnstructor=== Array) { selectedMessages.splice(0, selectedMessages.length); } else { for(let i=0;i if(selectedMessages[i].id == selection.id) { index = i; break; } } selectedMessages.splice(index,1); } } break; case 1: if(action === "add") { if(selection.cOnstructor=== Array) { for(let i=0;i<$scope.tabs[1].messages.length;i++) { result = $filter('filter')(selectedPendings, {id: $scope.tabs[1].messages[i].id}); if(result.length == 0) { selectedPendings.push($scope.tabs[1].messages[i]); result = undefined; } } } else { selectedPendings.push(selection); } } else if(action === "remove") { if(selection.cOnstructor=== Array) { selectedPendings.splice(0, selectedPendings.length); } else { for(let i=0;i if(selectedPendings[i].id == selection.id) { index = i; break; } } selectedPendings.splice(index,1); } } break; } }; $scope.isSelected = function(id, idx) { let result; switch (idx) { case 0: result = $filter('filter')(selectedMessages, {id: id}); if(result.length === 0) { return false; } else { return true; } break; case 1: result = $filter('filter')(selectedPendings, {id: id}); if(result.length === 0) { return false; } else { return true; } break; } }; $scope.isAllSelected = function(idx) { switch (idx) { case 0: if(selectedMessages.length == $scope.tabs[0].messages.length && selectedMessages.length != 0) return true; else return false; break; case 1: if(selectedPendings.length == $scope.tabs[1].messages.length && selectedPendings.length != 0) return true; else return false; break; } }; function findMessageIndex(id, messages) { for(let i=0; i if(messages[i].id === id) { return i; } } return -1; } } |
整体的功能是这样的:
有左右两个tab页,左边的是“通知”,右边的是“待处理”(即稍后处理的意思)。
通知里的列表项目右上角有个按钮,点击之后该条项目会被移到待处理里;待处理里也同理可以将消息转回通知里。
列表下方有一个全选按钮,能实现全选操作。
问题1:只要鼠标进入li.message-item范围内就不停地触发判断ng-checked,该怎么解决呢?
问题2:触发messageTransform()后,列表项数量马上发生了变化,但是整个列表的高度卡顿了一下才变化。
问题3:触发messageTransform()时,有时候会对其他列表项的ng-checked造成影响。
初学angularjs,函数可能写得惨不忍睹,希望大家多多指教!