@@ -17,41 +17,34 @@ var List = React.createClass({
17
17
} ,
18
18
19
19
render : function ( ) {
20
- var contents = [ this . renderItems ( ) ] ;
20
+ var cols = this . normalizeColumns ( ) ,
21
+ contents = [ this . renderItems ( cols ) ]
22
+ ;
21
23
22
24
if ( this . getSetting ( 'header' ) )
23
- contents . unshift ( this . renderHeader ( ) ) ;
25
+ contents . unshift ( this . renderHeader ( cols ) ) ;
24
26
25
27
return $ . table ( { className : "jsonTable" } , contents ) ;
26
28
} ,
27
29
28
- renderHeader : function ( ) {
30
+ renderHeader : function ( cols ) {
29
31
var me = this ,
30
- cells = this . props . fields . map ( function ( field ) {
31
- var name ;
32
- if ( typeof field == 'string' ) {
33
- name = field ;
34
- }
35
- else {
36
- name = field . name ;
37
- }
38
-
32
+ cells = cols . map ( function ( col ) {
39
33
return $ . th (
40
- { className : 'jsonColumn' , key : name , onClick : me . onClickHeader , "data-name " : name } ,
41
- name
34
+ { className : 'jsonColumn' , key : col . key , onClick : me . onClickHeader , "data-key " : col . key } ,
35
+ col . label
42
36
) ;
43
37
} )
44
38
;
45
39
46
40
return $ . thead ( { key : 'th' } ,
47
- $ . tr ( { className : 'jsonHeader' , key : 'h' , "data-name " : 'header' } , cells )
41
+ $ . tr ( { className : 'jsonHeader' , key : 'h' , "data-key " : 'header' } , cells )
48
42
) ;
49
43
} ,
50
44
51
- renderItems : function ( ) {
45
+ renderItems : function ( cols ) {
52
46
var me = this ,
53
47
items = this . props . items ,
54
- fields = this . normalizeFields ( ) ,
55
48
i = 1
56
49
;
57
50
@@ -62,8 +55,8 @@ var List = React.createClass({
62
55
return React . createElement ( Item , {
63
56
key : me . getKey ( item ) ,
64
57
item : item ,
65
- fields : fields ,
66
- i : i ,
58
+ columns : cols ,
59
+ i : i ++ ,
67
60
onClickItem : me . onClickItem ,
68
61
onClickCell : me . onClickCell
69
62
} ) ;
@@ -72,27 +65,53 @@ var List = React.createClass({
72
65
return $ . tbody ( { } , rows ) ;
73
66
} ,
74
67
75
- normalizeFields : function ( ) {
76
- var getItemField = function ( item , field ) {
77
- return item [ field ] ;
78
- } ;
68
+ getItemField : function ( item , field ) {
69
+ return item [ field ] ;
70
+ } ,
79
71
80
- return this . props . fields . map ( function ( field ) {
81
- var name , content ;
82
- if ( typeof field == 'string' ) {
72
+ normalizeColumns : function ( ) {
73
+ var getItemField = this . getItemField ,
74
+ cols = this . props . columns ,
75
+ items = this . props . items
76
+ ;
77
+
78
+ if ( ! cols ) {
79
+ if ( ! items || ! items . length )
80
+ return [ ] ;
81
+
82
+ return Object . keys ( items [ 0 ] ) . map ( function ( key ) {
83
+ return { key : key , label : key , cell : getItemField } ;
84
+ } ) ;
85
+ }
86
+
87
+ return cols . map ( function ( col ) {
88
+ var key ;
89
+ if ( typeof col == 'string' ) {
83
90
return {
84
- name : field ,
85
- content : getItemField
91
+ key : col ,
92
+ label : col ,
93
+ cell : getItemField
86
94
} ;
87
95
}
88
96
89
- if ( typeof field == 'object' ) {
90
- return field ;
97
+ if ( typeof col == 'object' ) {
98
+ key = col . key || col . label ;
99
+
100
+ // This is about get default column definition
101
+ // we use label as key if not defined
102
+ // we use key as label if not defined
103
+ // we use getItemField as cell function if not defined
104
+ return {
105
+ key : key ,
106
+ label : col . label || key ,
107
+ cell : col . cell || getItemField
108
+ } ;
91
109
}
92
110
93
111
return {
112
+ key : 'unknown' ,
94
113
name :'unknown' ,
95
- content : 'Unknown'
114
+ cell : 'Unknown'
96
115
} ;
97
116
} ) ;
98
117
} ,
@@ -113,10 +132,6 @@ var List = React.createClass({
113
132
return true ;
114
133
} ,
115
134
116
- componentDidUpdate : function ( ) {
117
- console . log ( 'updated' ) ;
118
- } ,
119
-
120
135
onClickItem : function ( e , item ) {
121
136
if ( this . props . onClickItem ) {
122
137
this . props . onClickItem ( e , item ) ;
@@ -125,32 +140,32 @@ var List = React.createClass({
125
140
126
141
onClickHeader : function ( e ) {
127
142
if ( this . props . onClickHeader ) {
128
- this . props . onClickHeader ( e , e . target . dataset . name ) ;
143
+ this . props . onClickHeader ( e , e . target . dataset . key ) ;
129
144
}
130
145
} ,
131
146
132
- onClickCell : function ( e , field , item ) {
147
+ onClickCell : function ( e , key , item ) {
133
148
if ( this . props . onClickCell ) {
134
- this . props . onClickCell ( e , field , item ) ;
149
+ this . props . onClickCell ( e , key , item ) ;
135
150
}
136
151
}
137
152
} ) ;
138
153
139
154
var Item = React . createClass ( {
140
155
render : function ( ) {
141
156
var me = this ,
142
- cells = this . props . fields . map ( function ( field ) {
143
- var content = field . content ,
144
- name = field . name
157
+ cells = this . props . columns . map ( function ( col ) {
158
+ var content = col . cell ,
159
+ key = col . key
145
160
;
146
161
147
162
if ( typeof content == 'function' )
148
- content = content ( me . props . item , name ) ;
163
+ content = content ( me . props . item , key ) ;
149
164
150
165
return $ . td ( {
151
166
className : 'jsonCell' ,
152
- key : name ,
153
- "data-name " : name ,
167
+ key : key ,
168
+ "data-key " : key ,
154
169
onClick : me . onClickCell
155
170
} , content ) ;
156
171
} )
@@ -169,7 +184,7 @@ var Item = React.createClass({
169
184
} ,
170
185
171
186
onClickCell : function ( e ) {
172
- this . props . onClickCell ( e , e . target . dataset . name , this . props . item ) ;
187
+ this . props . onClickCell ( e , e . target . dataset . key , this . props . item ) ;
173
188
} ,
174
189
175
190
onClickItem : function ( e ) {
0 commit comments