1
1
require 'daru/data_tables'
2
2
require 'daru'
3
3
require 'securerandom'
4
+ require 'erb'
4
5
5
6
module Daru
6
7
module View
@@ -16,16 +17,21 @@ def init_table(data=[], options={})
16
17
# TODO : create data array from the df and vector data. So that
17
18
# we can directly use the array.(No need to create df or vector and
18
19
# generate the html table using to_html)
19
- if data . is_a? ( Array )
20
- data_name = 'series_data' + SecureRandom . uuid
21
- data =
22
- if data . all? { |e | e . class ==Array }
23
- Daru ::DataFrame . rows ( data , name : data_name )
24
- else
25
- Daru ::Vector . new ( data , name : data_name )
26
- end
20
+ # if data.is_a?(Array)
21
+ # data_name = 'series_data'+ SecureRandom.uuid
22
+ # data =
23
+ # if data.all? { |e| e.class==Array }
24
+ # Daru::DataFrame.rows(data, name: data_name)
25
+ # else
26
+ # Daru::Vector.new(data, name: data_name)
27
+ # end
28
+ # end
29
+ options [ :data ] = to_data_array ( data )
30
+ row_number = 0
31
+ options [ :data ] . each do |array |
32
+ array . unshift ( row_number )
33
+ row_number += 1
27
34
end
28
- # options[:data] = data_in_array unless data_in_array.empty?
29
35
@table = Daru ::DataTables ::DataTable . new ( options )
30
36
@data = data
31
37
@table
@@ -40,12 +46,12 @@ def generate_body(table)
40
46
class : 'display' ,
41
47
cellspacing : '0' ,
42
48
width : '100%' ,
43
- table_html : @data . to_html_thead + @data . to_html_tbody
49
+ table_html : extract_table
44
50
}
45
- html_options = {
51
+ html_options = {
46
52
table_options : table_opts
47
53
}
48
- table . to_html ( @data . name , html_options )
54
+ table . to_html ( 'series_data' + SecureRandom . uuid , html_options )
49
55
end
50
56
51
57
def export_html_file ( table , path = './table.html' )
@@ -73,17 +79,37 @@ def to_data_array(data_set)
73
79
case
74
80
when data_set . is_a? ( Daru ::DataFrame )
75
81
return ArgumentError unless data_set . index . is_a? ( Daru ::Index )
76
- data_set . access_row_tuples_by_indexs ( *data_set . index . to_a )
82
+ rows = data_set . access_row_tuples_by_indexs ( *data_set . index . to_a )
83
+ convert_to_array_of_array ( rows )
77
84
when data_set . is_a? ( Daru ::Vector )
78
85
rows = [ ]
79
86
data_set . to_a . each { |a | rows << [ a ] }
80
87
rows
81
88
when data_set . is_a? ( Array )
82
- data_set
89
+ convert_to_array_of_array ( data_set )
83
90
else
84
91
raise ArgumentError # TODO: error msg
85
92
end
86
93
end
94
+
95
+ def convert_to_array_of_array ( rows )
96
+ if rows . all? { |row | row . class ==Array }
97
+ rows
98
+ else
99
+ tuples = [ ]
100
+ rows . each { |row | tuples << [ row ] }
101
+ tuples
102
+ end
103
+ end
104
+
105
+ def extract_table
106
+ return @data . to_html_thead unless @data . is_a? ( Array )
107
+ path = File . expand_path (
108
+ '../templates/datatables/thead.erb' , __dir__
109
+ )
110
+ template = File . read ( path )
111
+ ERB . new ( template ) . result ( binding )
112
+ end
87
113
end
88
114
end
89
115
end
0 commit comments