1 #include "../../lv_examples.h"
2 #if LV_USE_TABLE && LV_BUILD_EXAMPLES
3 
draw_part_event_cb(lv_event_t * e)4 static void draw_part_event_cb(lv_event_t * e)
5 {
6     lv_obj_t * obj = lv_event_get_target(e);
7     lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e);
8     /*If the cells are drawn...*/
9     if(dsc->part == LV_PART_ITEMS) {
10         uint32_t row = dsc->id /  lv_table_get_col_cnt(obj);
11         uint32_t col = dsc->id - row * lv_table_get_col_cnt(obj);
12 
13         /*Make the texts in the first cell center aligned*/
14         if(row == 0) {
15             dsc->label_dsc->align = LV_TEXT_ALIGN_CENTER;
16             dsc->rect_dsc->bg_color = lv_color_mix(lv_palette_main(LV_PALETTE_BLUE), dsc->rect_dsc->bg_color, LV_OPA_20);
17             dsc->rect_dsc->bg_opa = LV_OPA_COVER;
18         }
19         /*In the first column align the texts to the right*/
20         else if(col == 0) {
21             dsc->label_dsc->align = LV_TEXT_ALIGN_RIGHT;
22         }
23 
24         /*MAke every 2nd row grayish*/
25         if((row != 0 && row % 2) == 0) {
26             dsc->rect_dsc->bg_color = lv_color_mix(lv_palette_main(LV_PALETTE_GREY), dsc->rect_dsc->bg_color, LV_OPA_10);
27             dsc->rect_dsc->bg_opa = LV_OPA_COVER;
28         }
29     }
30 }
31 
lv_example_table_1(void)32 void lv_example_table_1(void)
33 {
34     lv_obj_t * table = lv_table_create(lv_scr_act());
35 
36     /*Fill the first column*/
37     lv_table_set_cell_value(table, 0, 0, "Name");
38     lv_table_set_cell_value(table, 1, 0, "Apple");
39     lv_table_set_cell_value(table, 2, 0, "Banana");
40     lv_table_set_cell_value(table, 3, 0, "Lemon");
41     lv_table_set_cell_value(table, 4, 0, "Grape");
42     lv_table_set_cell_value(table, 5, 0, "Melon");
43     lv_table_set_cell_value(table, 6, 0, "Peach");
44     lv_table_set_cell_value(table, 7, 0, "Nuts");
45 
46     /*Fill the second column*/
47     lv_table_set_cell_value(table, 0, 1, "Price");
48     lv_table_set_cell_value(table, 1, 1, "$7");
49     lv_table_set_cell_value(table, 2, 1, "$4");
50     lv_table_set_cell_value(table, 3, 1, "$6");
51     lv_table_set_cell_value(table, 4, 1, "$2");
52     lv_table_set_cell_value(table, 5, 1, "$5");
53     lv_table_set_cell_value(table, 6, 1, "$1");
54     lv_table_set_cell_value(table, 7, 1, "$9");
55 
56     /*Set a smaller height to the table. It'll make it scrollable*/
57     lv_obj_set_height(table, 200);
58     lv_obj_center(table);
59 
60     /*Add an event callback to to apply some custom drawing*/
61     lv_obj_add_event_cb(table, draw_part_event_cb, LV_EVENT_DRAW_PART_BEGIN, NULL);
62 }
63 
64 #endif
65