22 backgrid 0.3.5
33 http://github.com/wyuenho/backgrid
44
5- Copyright (c) 2015 Jimmy Yuen Ho Wong and contributors <wyuenho@gmail.com>
5+ Copyright (c) 2016 Jimmy Yuen Ho Wong and contributors <wyuenho@gmail.com>
66 Licensed under the MIT license.
77*/
88
@@ -281,7 +281,7 @@ _.extend(NumberFormatter.prototype, {
281281 fromRaw : function ( number , model ) {
282282 if ( _ . isNull ( number ) || _ . isUndefined ( number ) ) return '' ;
283283
284- number = number . toFixed ( ~ ~ this . decimals ) ;
284+ number = parseFloat ( number ) . toFixed ( ~ ~ this . decimals ) ;
285285
286286 var parts = number . split ( '.' ) ;
287287 var integerPart = parts [ 0 ] ;
@@ -869,24 +869,30 @@ var Cell = Backgrid.Cell = Backbone.View.extend({
869869 }
870870 } ) ;
871871
872- if ( Backgrid . callByNeed ( column . editable ( ) , column , model ) ) $el . addClass ( "editable" ) ;
873- if ( Backgrid . callByNeed ( column . sortable ( ) , column , model ) ) $el . addClass ( "sortable" ) ;
874- if ( Backgrid . callByNeed ( column . renderable ( ) , column , model ) ) $el . addClass ( "renderable" ) ;
872+ this . updateStateClassesMaybe ( ) ;
873+ } ,
874+
875+ updateStateClassesMaybe : function ( ) {
876+ var model = this . model ;
877+ var column = this . column ;
878+ var $el = this . $el ;
879+ $el . toggleClass ( "editable" , Backgrid . callByNeed ( column . editable ( ) , column , model ) ) ;
880+ $el . toggleClass ( "sortable" , Backgrid . callByNeed ( column . sortable ( ) , column , model ) ) ;
881+ $el . toggleClass ( "renderable" , Backgrid . callByNeed ( column . renderable ( ) , column , model ) ) ;
875882 } ,
876883
877884 /**
878885 Render a text string in a table cell. The text is converted from the
879886 model's raw value for this cell's column.
880887 */
881888 render : function ( ) {
882- this . $el . empty ( ) ;
889+ var $el = this . $el ;
890+ $el . empty ( ) ;
883891 var model = this . model ;
884- this . $el . text ( this . formatter . fromRaw ( model . get ( this . column . get ( "name" ) ) , model ) ) ;
885-
886- this . $el . toggleClass ( "editable" , Backgrid . callByNeed ( this . column . editable ( ) , this . column , this . model ) ) ;
887- this . $el . toggleClass ( "sortable" , Backgrid . callByNeed ( this . column . sortable ( ) , this . column , this . model ) ) ;
888- this . $el . toggleClass ( "renderable" , Backgrid . callByNeed ( this . column . renderable ( ) , this . column , this . model ) ) ;
889-
892+ var columnName = this . column . get ( "name" ) ;
893+ $el . text ( this . formatter . fromRaw ( model . get ( columnName ) , model ) ) ;
894+ $el . addClass ( columnName ) ;
895+ this . updateStateClassesMaybe ( ) ;
890896 this . delegateEvents ( ) ;
891897 return this ;
892898 } ,
@@ -1410,7 +1416,7 @@ var SelectCellEditor = Backgrid.SelectCellEditor = CellEditor.extend({
14101416
14111417 /** @property {function(Object, ?Object=): string } template */
14121418 template : _ . template (
1413- '<option value="<%- value %>" <%= selected ? \'selected="selected"\' : "" %>><%- text %></option>' ,
1419+ '<option value="<%- value %>" <%= selected ? \'selected="selected"\' : "" %>><%- text %></option>' ,
14141420 null ,
14151421 {
14161422 variable : null ,
@@ -2029,7 +2035,9 @@ var EmptyRow = Backgrid.EmptyRow = Backbone.View.extend({
20292035
20302036 var td = document . createElement ( "td" ) ;
20312037 td . setAttribute ( "colspan" , this . columns . length ) ;
2032- td . appendChild ( document . createTextNode ( _ . result ( this , "emptyText" ) ) ) ;
2038+ var span = document . createElement ( "span" ) ;
2039+ span . innerHTML = _ . result ( this , "emptyText" ) ;
2040+ td . appendChild ( span ) ;
20332041
20342042 this . el . className = "empty" ;
20352043 this . el . appendChild ( td ) ;
@@ -2325,15 +2333,19 @@ var Body = Backgrid.Body = Backbone.View.extend({
23252333 this . listenTo ( collection , "reset" , this . refresh ) ;
23262334 this . listenTo ( collection , "backgrid:sort" , this . sort ) ;
23272335 this . listenTo ( collection , "backgrid:edited" , this . moveToNextCell ) ;
2336+
2337+ this . listenTo ( this . columns , "add remove" , this . updateEmptyRow ) ;
23282338 } ,
23292339
23302340 _unshiftEmptyRowMayBe : function ( ) {
23312341 if ( this . rows . length === 0 && this . emptyText != null ) {
2332- this . rows . unshift ( new EmptyRow ( {
2342+ this . emptyRow = new EmptyRow ( {
23332343 emptyText : this . emptyText ,
23342344 columns : this . columns
2335- } ) ) ;
2336- return true ;
2345+ } ) ;
2346+
2347+ this . rows . unshift ( this . emptyRow ) ;
2348+ return true
23372349 }
23382350 } ,
23392351
@@ -2439,6 +2451,17 @@ var Body = Backgrid.Body = Backbone.View.extend({
24392451 return this ;
24402452 } ,
24412453
2454+ /**
2455+ Rerender the EmptyRow which empties the DOM element, creates the td with the
2456+ updated colspan, and appends it back into the DOM
2457+ */
2458+
2459+ updateEmptyRow : function ( ) {
2460+ if ( this . emptyRow != null ) {
2461+ this . emptyRow . render ( ) ;
2462+ }
2463+ } ,
2464+
24422465 /**
24432466 Reinitialize all the rows inside the body and re-render them. Triggers a
24442467 Backbone `backgrid:refresh` event from the collection along with the body
@@ -2607,10 +2630,10 @@ var Body = Backgrid.Body = Backbone.View.extend({
26072630 var i = this . collection . indexOf ( model ) ;
26082631 var j = this . columns . indexOf ( column ) ;
26092632 var cell , renderable , editable , m , n ;
2610-
2633+
26112634 // return if model being edited in a different grid
26122635 if ( j === - 1 ) return this ;
2613-
2636+
26142637 this . rows [ i ] . cells [ j ] . exitEditMode ( ) ;
26152638
26162639 if ( command . moveUp ( ) || command . moveDown ( ) || command . moveLeft ( ) ||
@@ -2904,4 +2927,4 @@ var Grid = Backgrid.Grid = Backbone.View.extend({
29042927
29052928} ) ;
29062929 return Backgrid ;
2907- } ) ) ;
2930+ } ) ) ;
0 commit comments