解决angular ui-grid 中添加input date修改日期
需求:在angular ui-grid列表中添加一个日期组件来修改时间。
在angular ui-grid添加了一个html5 date input,后端返回的数据是YYYY-MM-DD,比如:2018-06-21
{ displayName: '起飞时间', name: '起飞时间', cellTemplate: '<div ng-hide="row.entity.isShowSave">{{row.entity.airwayBillCheckTime}}</div>'+ '<div ng-show="row.entity.isShowSave"><input class="form-control" style="width: 120px" type="date" ng-model="row.entity.airwayBillCheckTime">, minWidth: '120' },
因为返回的是一个日期格式数据,不是一个Date对象,会报错。
如果实在普通的页面,只需要将返回的数据变为Date对象就好,如:new Date('2018-06-21')
但我们加的日期是在angular ui-grid列表页中,ui-grid插件只能接受后端封装好的格式,而后端返回的是日期格式字符串,需要在显示之前处理。
那么我想到的有两种思路:
1:在插件初始化接受数据之后循环转换为Date对象。
context.gridOptions = { enablePinning: false, selectAll: true, url:"xxxxx.html", columnDefs: Core.Const.Web, params:{pageSize:'20'}, $scope:$scope, getDataFinish: function(){ //todo 循环格式化数据(未验证) } };
2.在ui-grid模板html中添加自定义指令处理。
angular.module('app') .directive("formatDate", function(){ return { require: 'ngModel', link: function(scope, elem, attr, modelCtrl) { modelCtrl.$formatters.push(function(modelValue){ return new Date(modelValue); }) } } })
//在input标签中加入format-date { displayName: '起飞时间', name: '起飞时间', cellTemplate: '<div ng-hide="row.entity.isShowSave">{{row.entity.airwayBillCheckTime}}</div>'+ '<div ng-show="row.entity.isShowSave"><input class="form-control" style="width: 120px" type="date" ng-model="row.entity.airwayBillCheckTime" format-date>, minWidth: '120' },
方案二觉得合理,亲测可用,解决了在angular ui-gri使用日期组件修改提交日期。
彩蛋:附加一个Date对象日期格式化
1 function date(format, timestamp) { 2 // discuss at: http://phpjs.org/functions/date/ 3 // original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com) 4 // original by: gettimeofday 5 // parts by: Peter-Paul Koch (http://www.quirksmode.org/js/beat.html) 6 // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 7 // improved by: MeEtc (http://yass.meetcweb.com) 8 // improved by: Brad Touesnard 9 // improved by: Tim Wiel 10 // improved by: Bryan Elliott 11 // improved by: David Randall 12 // improved by: Theriault 13 // improved by: Theriault 14 // improved by: Brett Zamir (http://brett-zamir.me) 15 // improved by: Theriault 16 // improved by: Thomas Beaucourt (http://www.webapp.fr) 17 // improved by: JT 18 // improved by: Theriault 19 // improved by: Rafał Kukawski (http://blog.kukawski.pl) 20 // improved by: Theriault 21 // input by: Brett Zamir (http://brett-zamir.me) 22 // input by: majak 23 // input by: Alex 24 // input by: Martin 25 // input by: Alex Wilson 26 // input by: Haravikk 27 // bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 28 // bugfixed by: majak 29 // bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 30 // bugfixed by: Brett Zamir (http://brett-zamir.me) 31 // bugfixed by: omid (http://phpjs.org/functions/380:380#comment_137122) 32 // bugfixed by: Chris (http://www.devotis.nl/) 33 // note: Uses global: php_js to store the default timezone 34 // note: Although the function potentially allows timezone info (see notes), it currently does not set 35 // note: per a timezone specified by date_default_timezone_set(). Implementers might use 36 // note: this.php_js.currentTimezoneOffset and this.php_js.currentTimezoneDST set by that function 37 // note: in order to adjust the dates in this function (or our other date functions!) accordingly 38 // example 1: date('H:m:s \\m \\i\\s \\m\\o\\n\\t\\h', 1062402400); 39 // returns 1: '09:09:40 m is month' 40 // example 2: date('F j, Y, g:i a', 1062462400); 41 // returns 2: 'September 2, 2003, 2:26 am' 42 // example 3: date('Y W o', 1062462400); 43 // returns 3: '2003 36 2003' 44 // example 4: x = date('Y m d', (new Date()).getTime()/1000); 45 // example 4: (x+'').length == 10 // 2009 01 09 46 // returns 4: true 47 // example 5: date('W', 1104534000); 48 // returns 5: '53' 49 // example 6: date('B t', 1104534000); 50 // returns 6: '999 31' 51 // example 7: date('W U', 1293750000.82); // 2010-12-31 52 // returns 7: '52 1293750000' 53 // example 8: date('W', 1293836400); // 2011-01-01 54 // returns 8: '52' 55 // example 9: date('W Y-m-d', 1293974054); // 2011-01-02 56 // returns 9: '52 2011-01-02' 57 58 var that = this; 59 var jsdate, f; 60 // Keep this here (works, but for code commented-out below for file size reasons) 61 // var tal= []; 62 var txt_words = [ 63 'Sun', 'Mon', 'Tues', 'Wednes', 'Thurs', 'Fri', 'Satur', 64 'January', 'February', 'March', 'April', 'May', 'June', 65 'July', 'August', 'September', 'October', 'November', 'December' 66 ]; 67 // trailing backslash -> (dropped) 68 // a backslash followed by any character (including backslash) -> the character 69 // empty string -> empty string 70 var formatChr = /\\?(.?)/gi; 71 var formatChrCb = function (t, s) { 72 return f[t] ? f[t]() : s; 73 }; 74 var _pad = function (n, c) { 75 n = String(n); 76 while (n.length < c) { 77 n = '0' + n; 78 } 79 return n; 80 }; 81 f = { 82 // Day 83 d: function () { // Day of month w/leading 0; 01..31 84 return _pad(f.j(), 2); 85 }, 86 D: function () { // Shorthand day name; Mon...Sun 87 return f.l() 88 .slice(0, 3); 89 }, 90 j: function () { // Day of month; 1..31 91 return jsdate.getDate(); 92 }, 93 l: function () { // Full day name; Monday...Sunday 94 return txt_words[f.w()] + 'day'; 95 }, 96 N: function () { // ISO-8601 day of week; 1[Mon]..7[Sun] 97 return f.w() || 7; 98 }, 99 S: function () { // Ordinal suffix for day of month; st, nd, rd, th 100 var j = f.j(); 101 var i = j % 10; 102 if (i <= 3 && parseInt((j % 100) / 10, 10) == 1) { 103 i = 0; 104 } 105 return ['st', 'nd', 'rd'][i - 1] || 'th'; 106 }, 107 w: function () { // Day of week; 0[Sun]..6[Sat] 108 return jsdate.getDay(); 109 }, 110 z: function () { // Day of year; 0..365 111 var a = new Date(f.Y(), f.n() - 1, f.j()); 112 var b = new Date(f.Y(), 0, 1); 113 return Math.round((a - b) / 864e5); 114 }, 115 116 // Week 117 W: function () { // ISO-8601 week number 118 var a = new Date(f.Y(), f.n() - 1, f.j() - f.N() + 3); 119 var b = new Date(a.getFullYear(), 0, 4); 120 return _pad(1 + Math.round((a - b) / 864e5 / 7), 2); 121 }, 122 123 // Month 124 F: function () { // Full month name; January...December 125 return txt_words[6 + f.n()]; 126 }, 127 m: function () { // Month w/leading 0; 01...12 128 return _pad(f.n(), 2); 129 }, 130 M: function () { // Shorthand month name; Jan...Dec 131 return f.F() 132 .slice(0, 3); 133 }, 134 n: function () { // Month; 1...12 135 return jsdate.getMonth() + 1; 136 }, 137 t: function () { // Days in month; 28...31 138 return (new Date(f.Y(), f.n(), 0)) 139 .getDate(); 140 }, 141 142 // Year 143 L: function () { // Is leap year?; 0 or 1 144 var j = f.Y(); 145 return j % 4 === 0 & j % 100 !== 0 | j % 400 === 0; 146 }, 147 o: function () { // ISO-8601 year 148 var n = f.n(); 149 var W = f.W(); 150 var Y = f.Y(); 151 return Y + (n === 12 && W < 9 ? 1 : n === 1 && W > 9 ? -1 : 0); 152 }, 153 Y: function () { // Full year; e.g. 1980...2010 154 return jsdate.getFullYear(); 155 }, 156 y: function () { // Last two digits of year; 00...99 157 return f.Y() 158 .toString() 159 .slice(-2); 160 }, 161 162 // Time 163 a: function () { // am or pm 164 return jsdate.getHours() > 11 ? 'pm' : 'am'; 165 }, 166 A: function () { // AM or PM 167 return f.a() 168 .toUpperCase(); 169 }, 170 B: function () { // Swatch Internet time; 000..999 171 var H = jsdate.getUTCHours() * 36e2; 172 // Hours 173 var i = jsdate.getUTCMinutes() * 60; 174 // Minutes 175 var s = jsdate.getUTCSeconds(); // Seconds 176 return _pad(Math.floor((H + i + s + 36e2) / 86.4) % 1e3, 3); 177 }, 178 g: function () { // 12-Hours; 1..12 179 return f.G() % 12 || 12; 180 }, 181 G: function () { // 24-Hours; 0..23 182 return jsdate.getHours(); 183 }, 184 h: function () { // 12-Hours w/leading 0; 01..12 185 return _pad(f.g(), 2); 186 }, 187 H: function () { // 24-Hours w/leading 0; 00..23 188 return _pad(f.G(), 2); 189 }, 190 i: function () { // Minutes w/leading 0; 00..59 191 return _pad(jsdate.getMinutes(), 2); 192 }, 193 s: function () { // Seconds w/leading 0; 00..59 194 return _pad(jsdate.getSeconds(), 2); 195 }, 196 u: function () { // Microseconds; 000000-999000 197 return _pad(jsdate.getMilliseconds() * 1000, 6); 198 }, 199 200 // Timezone 201 e: function () { // Timezone identifier; e.g. Atlantic/Azores, ... 202 // The following works, but requires inclusion of the very large 203 // timezone_abbreviations_list() function. 204 /* return that.date_default_timezone_get(); 205 */ 206 throw 'Not supported (see source code of date() for timezone on how to add support)'; 207 }, 208 I: function () { // DST observed?; 0 or 1 209 // Compares Jan 1 minus Jan 1 UTC to Jul 1 minus Jul 1 UTC. 210 // If they are not equal, then DST is observed. 211 var a = new Date(f.Y(), 0); 212 // Jan 1 213 var c = Date.UTC(f.Y(), 0); 214 // Jan 1 UTC 215 var b = new Date(f.Y(), 6); 216 // Jul 1 217 var d = Date.UTC(f.Y(), 6); // Jul 1 UTC 218 return ((a - c) !== (b - d)) ? 1 : 0; 219 }, 220 O: function () { // Difference to GMT in hour format; e.g. +0200 221 var tzo = jsdate.getTimezoneOffset(); 222 var a = Math.abs(tzo); 223 return (tzo > 0 ? '-' : '+') + _pad(Math.floor(a / 60) * 100 + a % 60, 4); 224 }, 225 P: function () { // Difference to GMT w/colon; e.g. +02:00 226 var O = f.O(); 227 return (O.substr(0, 3) + ':' + O.substr(3, 2)); 228 }, 229 T: function () { // Timezone abbreviation; e.g. EST, MDT, ... 230 // The following works, but requires inclusion of the very 231 // large timezone_abbreviations_list() function. 232 /* var abbr, i, os, _default; 233 if (!tal.length) { 234 tal = that.timezone_abbreviations_list(); 235 } 236 if (that.php_js && that.php_js.default_timezone) { 237 _default = that.php_js.default_timezone; 238 for (abbr in tal) { 239 for (i = 0; i < tal[abbr].length; i++) { 240 if (tal[abbr][i].timezone_id === _default) { 241 return abbr.toUpperCase(); 242 } 243 } 244 } 245 } 246 for (abbr in tal) { 247 for (i = 0; i < tal[abbr].length; i++) { 248 os = -jsdate.getTimezoneOffset() * 60; 249 if (tal[abbr][i].offset === os) { 250 return abbr.toUpperCase(); 251 } 252 } 253 } 254 */ 255 return 'UTC'; 256 }, 257 Z: function () { // Timezone offset in seconds (-43200...50400) 258 return -jsdate.getTimezoneOffset() * 60; 259 }, 260 261 // Full Date/Time 262 c: function () { // ISO-8601 date. 263 return 'Y-m-d\\TH:i:sP'.replace(formatChr, formatChrCb); 264 }, 265 r: function () { // RFC 2822 266 return 'D, d M Y H:i:s O'.replace(formatChr, formatChrCb); 267 }, 268 U: function () { // Seconds since UNIX epoch 269 return jsdate / 1000 | 0; 270 } 271 }; 272 this.date = function (format, timestamp) { 273 that = this; 274 jsdate = (timestamp === undefined ? new Date() : // Not provided 275 (timestamp instanceof Date) ? new Date(timestamp) : // JS Date() 276 new Date(timestamp * 1000) // UNIX timestamp (auto-convert to int) 277 ); 278 return format.replace(formatChr, formatChrCb); 279 }; 280 return this.date(format, timestamp); 281 }
-----原创文章,©版权所有,转载请注明标明出处:http://www.cnblogs.com/doinbean