json export in grid, thanks Alan
This commit is contained in:
@@ -1 +1 @@
|
|||||||
Version 2.4.1-alpha.2+timestamp.2013.01.15.22.22.06
|
Version 2.4.1-alpha.2+timestamp.2013.01.16.09.40.52
|
||||||
|
|||||||
+23
-24
@@ -1991,6 +1991,7 @@ class SQLFORM(FORM):
|
|||||||
csv=(ExporterCSV, 'CSV'),
|
csv=(ExporterCSV, 'CSV'),
|
||||||
xml=(ExporterXML, 'XML'),
|
xml=(ExporterXML, 'XML'),
|
||||||
html=(ExporterHTML, 'HTML'),
|
html=(ExporterHTML, 'HTML'),
|
||||||
|
json=(ExporterJSON, 'JSON'),
|
||||||
tsv_with_hidden_cols=
|
tsv_with_hidden_cols=
|
||||||
(ExporterTSV, 'TSV (Excel compatible, hidden cols)'),
|
(ExporterTSV, 'TSV (Excel compatible, hidden cols)'),
|
||||||
tsv=(ExporterTSV, 'TSV (Excel compatible)'))
|
tsv=(ExporterTSV, 'TSV (Excel compatible)'))
|
||||||
@@ -2938,11 +2939,10 @@ class ExporterCSV(ExportClass):
|
|||||||
|
|
||||||
def export(self):
|
def export(self):
|
||||||
if self.rows:
|
if self.rows:
|
||||||
return str(self.rows)
|
return self.rows.as_csv()
|
||||||
else:
|
else:
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
|
||||||
class ExporterHTML(ExportClass):
|
class ExporterHTML(ExportClass):
|
||||||
label = 'HTML'
|
label = 'HTML'
|
||||||
file_ext = "html"
|
file_ext = "html"
|
||||||
@@ -2952,18 +2952,10 @@ class ExporterHTML(ExportClass):
|
|||||||
ExportClass.__init__(self, rows)
|
ExportClass.__init__(self, rows)
|
||||||
|
|
||||||
def export(self):
|
def export(self):
|
||||||
out = cStringIO.StringIO()
|
|
||||||
out.write('<html>\n<body>\n<table>\n')
|
|
||||||
if self.rows:
|
if self.rows:
|
||||||
colnames = [a.split('.') for a in self.rows.colnames]
|
return self.rows.xml()
|
||||||
for row in self.rows.records:
|
else:
|
||||||
out.write('<tr>\n')
|
return '<html>\n<body>\n<table>\n</table>\n</body>\n</html>'
|
||||||
for col in colnames:
|
|
||||||
out.write('<td>' + str(row[col[0]][col[1]]) + '</td>\n')
|
|
||||||
out.write('</tr>\n')
|
|
||||||
out.write('</table>\n</body>\n</html>')
|
|
||||||
return str(out.getvalue())
|
|
||||||
|
|
||||||
|
|
||||||
class ExporterXML(ExportClass):
|
class ExporterXML(ExportClass):
|
||||||
label = 'XML'
|
label = 'XML'
|
||||||
@@ -2974,15 +2966,22 @@ class ExporterXML(ExportClass):
|
|||||||
ExportClass.__init__(self, rows)
|
ExportClass.__init__(self, rows)
|
||||||
|
|
||||||
def export(self):
|
def export(self):
|
||||||
out = cStringIO.StringIO()
|
|
||||||
out.write('<rows>\n')
|
|
||||||
if self.rows:
|
if self.rows:
|
||||||
colnames = [a.split('.') for a in self.rows.colnames]
|
return self.rows.as_xml()
|
||||||
for row in self.rows.records:
|
else:
|
||||||
out.write('<row>\n')
|
return '<rows></rows>'
|
||||||
for col in colnames:
|
|
||||||
out.write(
|
class ExporterJSON(ExportClass):
|
||||||
'<%s>' % col + str(row[col[0]][col[1]]) + '</%s>\n' % col)
|
label = 'JSON'
|
||||||
out.write('</row>\n')
|
file_ext = "json"
|
||||||
out.write('</rows>')
|
content_type = "application/json"
|
||||||
return str(out.getvalue())
|
|
||||||
|
def __init__(self, rows):
|
||||||
|
ExportClass.__init__(self, rows)
|
||||||
|
|
||||||
|
def export(self):
|
||||||
|
if self.rows:
|
||||||
|
return self.rows.as_json()
|
||||||
|
else:
|
||||||
|
return 'null'
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user