Skip to content

Commit 2441a21

Browse files
committed
Use javascript Array rather than html table in DataTables
1 parent 3a36663 commit 2441a21

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

lib/daru/view/adapters/datatables.rb

+40-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'daru/data_tables'
22
require 'daru'
33
require 'securerandom'
4+
require 'erb'
45

56
module Daru
67
module View
@@ -16,16 +17,21 @@ def init_table(data=[], options={})
1617
# TODO : create data array from the df and vector data. So that
1718
# we can directly use the array.(No need to create df or vector and
1819
# 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
2734
end
28-
# options[:data] = data_in_array unless data_in_array.empty?
2935
@table = Daru::DataTables::DataTable.new(options)
3036
@data = data
3137
@table
@@ -40,12 +46,12 @@ def generate_body(table)
4046
class: 'display',
4147
cellspacing: '0',
4248
width: '100%',
43-
table_html: @data.to_html_thead + @data.to_html_tbody
49+
table_html: extract_table
4450
}
45-
html_options ={
51+
html_options = {
4652
table_options: table_opts
4753
}
48-
table.to_html(@data.name, html_options)
54+
table.to_html('series_data'+ SecureRandom.uuid, html_options)
4955
end
5056

5157
def export_html_file(table, path='./table.html')
@@ -73,17 +79,37 @@ def to_data_array(data_set)
7379
case
7480
when data_set.is_a?(Daru::DataFrame)
7581
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)
7784
when data_set.is_a?(Daru::Vector)
7885
rows = []
7986
data_set.to_a.each { |a| rows << [a] }
8087
rows
8188
when data_set.is_a?(Array)
82-
data_set
89+
convert_to_array_of_array(data_set)
8390
else
8491
raise ArgumentError # TODO: error msg
8592
end
8693
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
87113
end
88114
end
89115
end

0 commit comments

Comments
 (0)