*Project* The Anubis Project *Title* Generic table page. *Copyright* Copyright (c) Alain Prouté 2004. *Author* Alain Prouté *Author* Olivier Duvernois ---------------------------------------------------------------------------------------- read tools/basis.anubis read CXM_common.anubis read CXM_making_a_web_site.anubis The purpose is to print on a html browser a table from a List($Data) using the function 'generic_table()' describe below. Note : the explanations are only given for HTML_Item and its components. But they are also available for HTML_Form and HTML_Element and their components. A table may have the following look : +-----+----------+--------+-----------------------+ ............... | | | | name 3 | | num | name1 | name2 |-----------+-----------+ columns_name | | | | name 31 | name 32 | +-----+----------+--------+-----------+-----------+ ............... | 1 | data11 | data12 | data131 | data132 | line 1 with background color a +-----+----------+--------+-----------+-----------+ ............... | 2 | data21 | data22 | data231 | data232 | line 2 with background color b +-----+----------+--------+-----------+-----------+ ............... | 3 | data31 | data32 | data331 | data332 | line 3 with background color a +-----+----------+--------+-----------+-----------+ ............... | n | datan1 | datan2 | datan31 | datan32 | line n with background color ? +-----+----------+--------+-----------+-----------+ ............... |total| total1 | | | total32 | total line +-----+----------+--------+-----------+-----------+ ............... For the total-line, assuming that datax1 to dataxn and datax32 to datan32 are numbers (Int, Float or Maybe(Float)). The columns name are just a List(Item_Row). The column 'num' is in the case you want to enumerate your data. The existence of this column depends on the line function. Lines are given by the function : (RGB color,Int num,$Data d) -> Item_Row where : - (RGB)color is the color of the background of the row (the 'a color' or 'b color'); - (Int)num the number of the data (to enumerate). So, this function must be written something like : (RGB color, Int num,$Data d) |-> row([background_color(color)], // and of course possibly other row-options [ cell([], (Int -> $HTML)(num) ) . ($Data -> List(Cell))d // how data is printed in cells ]). But, for the above convenient function with no enumeration, you can only write : (RGB color, $Data d) |-> row([background_color(color)], // and of course possibly other row-options ($Data -> List(Cell))d // how data is printed in cells ). If you want to sum by column your data, use the following type : public type Total_Line($Data,$Upplet,$Row): no_total, total ( ($Upplet,$Data) -> $Upplet sum_functions, $Upplet -> $Row print_total_line, $Upplet initial_value ). $Row is for HTML_Row($HTML). $Upplet represents the components of $Data that will be sum. Example : with the above sheme table, $Data is something like : type $Data data ( Data1 d1, Data2 d2 Data3 d3 ). and type Data3: data3 ( Data31 d31, Data32 d32 ). So $Upplet will be (Data1,Data32) : sums are wanted for those 2 datum The function ($Upplet,$Data) -> $Upplet will be written like : ($Upplet u,$Data d) -> if u is (u1,u2) then (u1+d1(d), u2+d32(d3(d))) (if of course (Data1 + Data1) and (Data32 + Data32) are defined). - Several columns - ------------------- If you want to print your data on sevaral columns (i.e. considering the above table scheme as a column), you must specify the number of columns. You will obtain : Here is a List($Data) : l = [a,b,c,d,e,f,g,h,i,j,k,l,m]; and f : (RGB,Int,$Data) -> Item_Row You want to print this list on 3 columns. The result will be : +-----------+-----------+-----------+ | col.names | col.names | col.names | +-----------+-----------+-----------+ | f(a) | f(f) | f(k) | +-----------+-----------+-----------+ | f(b) | f(g) | f(l) | +-----------+-----------+-----------+ Each column is a table as defined | f(c) | f(h) | f(m) | above. +-----------+-----------+-----------+ | f(d) | f(i) | | +-----------+-----------+-----------+ | f(e) | f(j) | | +-----------+-----------+-----------+ If several columns are required, it's also asked for spaces between two colums. So use the following type : public type HowManyColumns: _1, several (Int col_nb, Int spacer). - Now the Generic table definition - ------------------------------------ Here is the most customizable generic table. Below, they are some convenience functions. public define HTML_Off_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_Off_Form)) columns_name, HowManyColumns number_of_columns, (RGB,Int,$Data) -> HTML_Row(HTML_Off_Form) line_format, RGB a_color, RGB b_color, Total_Line($Data,$Upplet,HTML_Row(HTML_Off_Form)) total_line ). public define HTML_In_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_In_Form)) columns_name, HowManyColumns number_of_columns, (RGB,Int,$Data) -> HTML_Row(HTML_In_Form) line_format, RGB a_color, RGB b_color, Total_Line($Data,$Upplet,HTML_Row(HTML_In_Form)) total_line ). Note : List(Table_Option) : if you choose 'nude' (i.e. border(0,0,0)), don't forget a horizontal spacer between the cells contain in the 'line_format' row. If you don't put any, each data will be closer to the next one. - Convenience functions - ------------------------- 1/ Table without multicolumns, enumeration and total-line: public define HTML_Off_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_Off_Form)) columns_name, (RGB,$Data) -> HTML_Row(HTML_Off_Form) line_format, RGB a_color, RGB b_color ). public define HTML_In_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_In_Form)) columns_name, (RGB,$Data) -> HTML_Row(HTML_In_Form) line_format, RGB a_color, RGB b_color ). 2/ Table with total-line and without multicolumns, enumeration. public define HTML_Off_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_Off_Form)) columns_name, (RGB,$Data) -> HTML_Row(HTML_Off_Form) line_format, RGB a_color, RGB b_color, Total_Line($Data,$Upplet,HTML_Row(HTML_Off_Form)) total_line ). public define HTML_In_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_In_Form)) columns_name, (RGB,$Data) -> HTML_Row(HTML_In_Form) line_format, RGB a_color, RGB b_color, Total_Line($Data,$Upplet,HTML_Row(HTML_In_Form)) total_line ). 3/ Table with multicolumns, enumeration, but without total public define HTML_Off_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_Off_Form)) columns_name, HowManyColumns number_of_columns, (RGB,Int,$Data) -> HTML_Row(HTML_Off_Form) line_format, RGB a_color, RGB b_color ). public define HTML_In_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_In_Form)) columns_name, HowManyColumns number_of_columns, (RGB,Int,$Data) -> HTML_Row(HTML_In_Form) line_format, RGB a_color, RGB b_color ). 4/ Table with multicolumns and without enumeration & total public define HTML_Off_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_Off_Form)) columns_name, HowManyColumns number_of_columns, (RGB,$Data) -> HTML_Row(HTML_Off_Form) line_format, RGB a_color, RGB b_color ). public define HTML_In_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_In_Form)) columns_name, HowManyColumns number_of_columns, (RGB,$Data) -> HTML_Row(HTML_In_Form) line_format, RGB a_color, RGB b_color ). --- That's all for public part. ------------------------------------------------------------------------- When the datum $Data must be presented on several columns, the initial list must be re-composed : sot the type Print_Table. type Print_Table($Data): print_table ( $Data data, Int num ). *** Transform List($Data) into List(Print_Table($Data)) define (List(Print_Table($Data)),List($Data)) get_n_elements ( List($Data) l, List(Print_Table($Data)) result, Int n, Int ct, // counter Int num ) = if l is { [] then (reverse(result),[]), [h . t] then if ct = n then (reverse([print_table(h,num+1) .result]),t) else get_n_elements(t,[print_table(h,num+1) . result],n,ct+1,num+1) }. define List(List(Print_Table($Data))) short_lists ( List($Data) l, Int nb, // number of element of short list Int ct // counter for numbering data (initialized at 0) ) = if get_n_elements(l,[],nb,1,ct) is (result,unused) then if unused is { [] then [result], [_ . _] then [result . short_lists(unused,nb,ct+nb)] }. - Generic row - --------------- define (List(HTML_Row($HTML)),$Upplet) generic_rows ( List(Print_Table($Data)) lpt, (RGB,Int,$Data) -> HTML_Row($HTML) line_format, RGB a_color, RGB b_color, ($Upplet,$Data) -> $Upplet do_sum, List(HTML_Row($HTML)) rows, $Upplet sum ) = if lpt is { [] then (reverse(rows),sum), [h . t] then generic_rows(t,line_format,b_color,a_color,do_sum, [line_format(a_color,num(h),data(h)) . rows],do_sum(sum,data(h))) }. - HTML_In_Form - ---------------- define List(HTML_Cell(HTML_In_Form)) generic_cells ( List(List(Print_Table($Data))) print_data, List(Table_Option) lto, List(HTML_Row(HTML_In_Form)) names, (RGB,Int,$Data) -> HTML_Row(HTML_In_Form) line_format, RGB a_color, RGB b_color, ($Upplet,$Data) -> $Upplet do_sum, $Upplet -> HTML_Row(HTML_In_Form) total_line, $Upplet value, Int spacer ) = if print_data is { [] then (List(HTML_Cell(HTML_In_Form))) [], [h . t] then if generic_rows(h,line_format,a_color,b_color,do_sum,(List(HTML_Row(HTML_In_Form)))[],value) is (rows,new_value) then if t is [] then [cell([top],table(lto,names+rows+[total_line(new_value)]))] else [ cell([top],table(lto,append(names,rows))) . if spacer = 0 then generic_cells (t,lto,names,line_format,a_color,b_color,do_sum,total_line,new_value,spacer) else [cell([width(spacer)],text([],"")) . generic_cells (t,lto,names,line_format,a_color,b_color,do_sum,total_line,new_value,spacer)] ] }. public define Int Int x (mod Int y) = if x / y is { failure then 0, success(result) then if result is (q, r) then r }. public define HTML_In_Form generic_table ( List($Data) l, List(Table_Option) lto, List(HTML_Row(HTML_In_Form)) names, HowManyColumns hm_col, (RGB,Int,$Data) -> HTML_Row(HTML_In_Form) line_format, RGB a_color, RGB b_color, Total_Line($Data,$Upplet,HTML_Row(HTML_In_Form)) total_line ) = table([], if l is [] then [] else with nbl = length(l), [ row([], if total_line is { no_total then with col_nb = if hm_col is { _1 then 1, several(n,_) then n }, with spacer = if hm_col is { _1 then 0, several(n,sp) then sp }, generic_cells (short_lists(l,nbl\col_nb + if (nbl (mod col_nb)) > 0 then 1 else 0,0), lto,names,line_format,a_color,b_color, (One u,$Data d) |-> unique,(One _) |-> row([],[]),unique,spacer), total(sum_fct,total_line,init) then with col_nb = if hm_col is { _1 then 1, several(n,_) then n }, with spacer = if hm_col is { _1 then 0, several(n,sp) then sp }, generic_cells ( short_lists(l,nbl\col_nb + if (nbl (mod col_nb)) > 0 then 1 else 0,0), lto,names,line_format,a_color,b_color, sum_fct,total_line,init,spacer ) }) ]). - HTML_Off_Form - ---------------- define List(HTML_Cell(HTML_Off_Form)) generic_cells ( List(List(Print_Table($Data))) print_data, List(Table_Option) lto, List(HTML_Row(HTML_Off_Form)) names, (RGB,Int,$Data) -> HTML_Row(HTML_Off_Form) line_format, RGB a_color, RGB b_color, ($Upplet,$Data) -> $Upplet do_sum, $Upplet -> HTML_Row(HTML_Off_Form) total_line, $Upplet value, Int spacer ) = if print_data is { [] then (List(HTML_Cell(HTML_Off_Form))) [], [h . t] then if generic_rows(h,line_format,a_color,b_color,do_sum,(List(HTML_Row(HTML_Off_Form)))[],value) is (rows,new_value) then if t is [] then [cell([top],table(lto,(names+rows+[total_line(new_value)])))] else [ cell([top],table(lto,append(names,rows))) . if spacer =0 then generic_cells (t,lto,names,line_format,a_color,b_color,do_sum,total_line,new_value,spacer) else [cell([width(spacer)],text([],"")) . generic_cells (t,lto,names,line_format,a_color,b_color,do_sum,total_line,new_value,spacer)] ] }. public define HTML_Off_Form generic_table ( List($Data) l, List(Table_Option) lto, List(HTML_Row(HTML_Off_Form)) names, HowManyColumns hm_col, (RGB,Int,$Data) -> HTML_Row(HTML_Off_Form) line_format, RGB a_color, RGB b_color, Total_Line($Data,$Upplet,HTML_Row(HTML_Off_Form)) total_line ) = table([], with nbl = length(l), [ row([], if total_line is { no_total then with col_nb = if hm_col is { _1 then 1, several(n,_) then n }, with spacer = if hm_col is { _1 then 0, several(n,sp) then sp }, generic_cells ( short_lists(l,nbl\col_nb + if (nbl (mod col_nb)) > 0 then 1 else 0,0), lto,names,line_format,a_color,b_color, (One u,$Data d) |-> unique,(One _) |-> row([],[]),unique,spacer ), total(sum_fct,total_line,init) then with col_nb = if hm_col is { _1 then 1, several(n,_) then n }, with spacer = if hm_col is { _1 then 0, several(n,sp) then sp }, generic_cells ( short_lists(l,nbl\col_nb + if (nbl (mod col_nb)) > 0 then 1 else 0,0), lto,names,line_format,a_color,b_color, sum_fct,total_line,init,spacer ) }) ]). - Convenience functions : public define HTML_Off_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_Off_Form)) columns_name, (RGB,$Data) -> HTML_Row(HTML_Off_Form) line_format, RGB a_color, RGB b_color ) = generic_table ( (List($Data)) data, (List(Table_Option)) lto, (List(HTML_Row(HTML_Off_Form))) columns_name, (HowManyColumns) _1, ((RGB,Int,$Data) -> HTML_Row(HTML_Off_Form)) (RGB r,Int n,$Data d) |-> line_format(r,d), (RGB) a_color, (RGB) b_color, (Total_Line($Data,$Data,HTML_Row(HTML_Off_Form))) no_total ). public define HTML_In_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_In_Form)) columns_name, (RGB,$Data) -> HTML_Row(HTML_In_Form) line_format, RGB a_color, RGB b_color ) = generic_table ( (List($Data)) data, (List(Table_Option)) lto, (List(HTML_Row(HTML_In_Form))) columns_name, (HowManyColumns) _1, ((RGB,Int,$Data) -> HTML_Row(HTML_In_Form)) (RGB r,Int n,$Data d) |-> line_format(r,d), (RGB) a_color, (RGB) b_color, (Total_Line($Data,$Data,HTML_Row(HTML_In_Form))) no_total ). 2/ Table with total-line and without multicolumns, enumeration. public define HTML_Off_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_Off_Form)) columns_name, (RGB,$Data) -> HTML_Row(HTML_Off_Form) line_format, RGB a_color, RGB b_color, Total_Line($Data,$Upplet,HTML_Row(HTML_Off_Form)) total_line ) = generic_table ( (List($Data)) data, (List(Table_Option)) lto, (List(HTML_Row(HTML_Off_Form))) columns_name, (HowManyColumns) _1, ((RGB,Int,$Data) -> HTML_Row(HTML_Off_Form)) (RGB r,Int n,$Data d) |-> line_format(r,d), (RGB) a_color, (RGB) b_color, (Total_Line($Data,$Upplet,(HTML_Row(HTML_Off_Form)))) total_line ). public define HTML_In_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_In_Form)) columns_name, (RGB,$Data) -> HTML_Row(HTML_In_Form) line_format, RGB a_color, RGB b_color, Total_Line($Data,$Upplet,HTML_Row(HTML_In_Form)) total_line ) = generic_table ( (List($Data)) data, (List(Table_Option)) lto, (List(HTML_Row(HTML_In_Form))) columns_name, (HowManyColumns) _1, ((RGB,Int,$Data) -> HTML_Row(HTML_In_Form)) (RGB r,Int n,$Data d) |-> line_format(r,d), (RGB) a_color, (RGB) b_color, (Total_Line($Data,$Upplet,(HTML_Row(HTML_In_Form)))) total_line ). 3/ Table with multicolumns, enumeration, but without total public define HTML_Off_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_Off_Form)) columns_name, HowManyColumns number_of_columns, (RGB,Int,$Data) -> HTML_Row(HTML_Off_Form) line_format, RGB a_color, RGB b_color ) = generic_table ( (List($Data)) data, (List(Table_Option)) lto, (List(HTML_Row(HTML_Off_Form))) columns_name, (HowManyColumns) number_of_columns, ((RGB,Int,$Data) -> HTML_Row(HTML_Off_Form)) (RGB r,Int n,$Data d) |-> line_format(r,n,d), (RGB) a_color, (RGB) b_color, (Total_Line($Data,$Data,(HTML_Row(HTML_Off_Form)))) no_total ). public define HTML_In_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_In_Form)) columns_name, HowManyColumns number_of_columns, (RGB,Int,$Data) -> HTML_Row(HTML_In_Form) line_format, RGB a_color, RGB b_color ) = generic_table ( (List($Data)) data, (List(Table_Option)) lto, (List(HTML_Row(HTML_In_Form))) columns_name, (HowManyColumns) number_of_columns, ((RGB,Int,$Data) -> HTML_Row(HTML_In_Form)) (RGB r,Int n,$Data d) |-> line_format(r,n,d), (RGB) a_color, (RGB) b_color, (Total_Line($Data,$Data,(HTML_Row(HTML_In_Form)))) no_total ). 4/ Table with multicolumns and without enumeration & total public define HTML_Off_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_Off_Form)) columns_name, HowManyColumns number_of_columns, (RGB,$Data) -> HTML_Row(HTML_Off_Form) line_format, RGB a_color, RGB b_color ) = generic_table ( (List($Data)) data, (List(Table_Option)) lto, (List(HTML_Row(HTML_Off_Form))) columns_name, (HowManyColumns) number_of_columns, ((RGB,Int,$Data) -> HTML_Row(HTML_Off_Form)) (RGB r,Int n,$Data d) |-> line_format(r,d), (RGB) a_color, (RGB) b_color, (Total_Line($Data,$Data,(HTML_Row(HTML_Off_Form)))) no_total ). public define HTML_In_Form generic_table ( List($Data) data, List(Table_Option) lto, List(HTML_Row(HTML_In_Form)) columns_name, HowManyColumns number_of_columns, (RGB,$Data) -> HTML_Row(HTML_In_Form) line_format, RGB a_color, RGB b_color ) = generic_table ( (List($Data)) data, (List(Table_Option)) lto, (List(HTML_Row(HTML_In_Form))) columns_name, (HowManyColumns) number_of_columns, ((RGB,Int,$Data) -> HTML_Row(HTML_In_Form)) (RGB r,Int n,$Data d) |-> line_format(r,d), (RGB) a_color, (RGB) b_color, (Total_Line($Data,$Data,(HTML_Row(HTML_In_Form)))) no_total ).