How to add or remove a table cell?

Description

What MCU/Processor/Board and compiler are you using?

STM32H7

What LVGL version are you using?

LVGL 7.11

What do you want to achieve?

To add or remove a table cell

What have you tried so far?

I have checked the API and found nothing about it.

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

/*You code here*/

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.

Hi , Sir,
I am learning the TABLE widget. I need to update the table cell frequently, to add , insert or delete a certain row of the table. I have checked the API and found nothing about it. Does it support that function?

Best Regards,
James

Hi @JamesJJ,

I can confirm the API does not provide those functions. You would need to manage this within your own code.

Here is an alphabetical sorted process list implementation from one of my old V7 projects to give you some idea of how you could go about managing the table implementation.

extern shared_memp_t		shmem_p;
extern config_dat_p_t		confp;

static lv_obj_t 			*tsk_table0;
static lv_obj_t 			*tsk_table1;
static lv_obj_t 			*cpu0_label;
static lv_obj_t 			*cpu1_label;

#define NUM_COLUMNS		5

static int compare_proc_name( void *a, void *b ) {

	OurTaskStatus_t		*taska = (OurTaskStatus_t*)a;
	OurTaskStatus_t		*taskb = (OurTaskStatus_t*)b;

	return( strcmp( taska->pcTaskName, taskb->pcTaskName ));

}

static void update_task_tables( uint8_t cpu_id, lv_obj_t *tsk_table ) {

	TaskStatus_t		ProcessStatusOS[MAX_TASKS];
	OurTaskStatus_t		*ProcessStatus;
    uint32_t			task_idx, row_idx, col_idx;
	char     			buf[32];

	ProcessStatus = shmem_p->cpud[cpu_id].ProcessStatus;
    if( cpu_id == 0 ) {
    	shmem_p->cpud[cpu_id].task_cnt = uxTaskGetNumberOfTasks();
    	uxTaskGetSystemState( ProcessStatusOS, shmem_p->cpud[cpu_id].task_cnt, NULL );		// Only get info if local CPU!
        for( int i = 0; i < shmem_p->cpud[cpu_id].task_cnt; i++ ) {
        	strcpy( (ProcessStatus+i)->pcTaskName, ProcessStatusOS[i].pcTaskName );
        	(ProcessStatus+i)->eCurrentState = ProcessStatusOS[i].eCurrentState;
        	(ProcessStatus+i)->uxCurrentPriority = ProcessStatusOS[i].uxCurrentPriority;
        	(ProcessStatus+i)->usStackHighWaterMark = ProcessStatusOS[i].usStackHighWaterMark;
        }
    }
    qsort( ProcessStatus, shmem_p->cpud[cpu_id].task_cnt, sizeof(OurTaskStatus_t), (void*)compare_proc_name ); // Sort Alphabetically by name

    lv_table_set_row_cnt(tsk_table, shmem_p->cpud[cpu_id].task_cnt + 1);
	for( row_idx = 0; row_idx < (shmem_p->cpud[cpu_id].task_cnt + 1); row_idx++ ) {
		for( col_idx = 0; col_idx < NUM_COLUMNS; col_idx++ ) lv_table_set_cell_type(tsk_table, row_idx, col_idx, 2);
	}

    lv_table_set_cell_value( tsk_table, 0, 0, "Task" );
    lv_table_set_cell_value( tsk_table, 0, 1, "Priority" );
    lv_table_set_cell_value( tsk_table, 0, 2, "State" );
    lv_table_set_cell_value( tsk_table, 0, 3, "Lowest Stack" );

    for( task_idx = 0; task_idx < shmem_p->cpud[cpu_id].task_cnt; task_idx++ ) {
    	row_idx =  task_idx+1;
    	lv_table_set_cell_value(tsk_table, row_idx, 0, (ProcessStatus[task_idx].pcTaskName));
		sprintf(buf, "%lu", ProcessStatus[task_idx].uxCurrentPriority);
		lv_table_set_cell_value(tsk_table, row_idx, 1, buf);
		switch (ProcessStatus[task_idx].eCurrentState) {
			case eReady:
				lv_table_set_cell_value(tsk_table, row_idx, 2, "Ready");
				break;

			case eBlocked:
				lv_table_set_cell_value(tsk_table, row_idx, 2, "Blocked");
				break;

			case eDeleted:
				lv_table_set_cell_value(tsk_table, row_idx, 2, "Deleted");
				break;

			case eSuspended:
				lv_table_set_cell_value(tsk_table, row_idx, 2, "Suspended");
				break;

			case eRunning:
				lv_table_set_cell_value(tsk_table, row_idx, 2, "Running");
				break;

			default:
				lv_table_set_cell_value(tsk_table, row_idx, 2, "Unknown");
				break;
		}
		sprintf(buf, "%d", (ProcessStatus[task_idx].usStackHighWaterMark * 4));
		lv_table_set_cell_value(tsk_table, row_idx, 3, buf);

    }
}

static void* update_task_list( void ) {

    char					s_buf[80];

	sprintf( s_buf, "CPU0 Task List:(%lu items)",shmem_p->cpud[0].task_cnt );
	lv_label_set_text(cpu0_label, s_buf);
	update_task_tables( 0, tsk_table0 );			// Update CPU 0 Task table

	sprintf( s_buf, "CPU1 Task List:(%lu items)",shmem_p->cpud[1].task_cnt );
	lv_label_set_text(cpu1_label, s_buf);
	update_task_tables( 1, tsk_table1 );		// Update CPU 1 Task table 
    return( NULL );
}

void sysinfo_create( lv_obj_t * parent ) {

    uint32_t				col_idx;
    static lv_obj_t			*page1, *page2;


	cpu0_label = lv_label_create(parent, NULL);
	cpu1_label = lv_label_create(parent, NULL);

	lv_obj_set_pos(cpu0_label, 96, 10);
	lv_obj_set_style_local_text_font (cpu0_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &lv_font_roboto_bi16);

	lv_obj_set_pos(cpu1_label, 96, (lv_obj_get_height_fit( parent ) / 2)+10);
	lv_obj_set_style_local_text_font (cpu1_label, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, &lv_font_roboto_bi16);

    page1 = lv_page_create( parent, NULL );
    lv_obj_set_size( page1, 800, ((lv_obj_get_height_fit( parent ) / 2)-45) );
    lv_obj_set_pos( page1, 96, 35 );
    setup_scrl_bar( page1, LV_PAGE_PART_SCROLLBAR, 8);
    lv_page_set_scrollbar_mode(page1, LV_SCROLLBAR_MODE_AUTO);
	tsk_table0 = lv_table_create(page1, NULL);
	lv_obj_set_drag_parent(tsk_table0, pdTRUE);
	lv_obj_set_style_local_text_font (tsk_table0, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, &lv_font_roboto_12);
    lv_obj_set_pos(tsk_table0, 0, 0 );
    lv_table_set_col_cnt(tsk_table0, NUM_COLUMNS);
    lv_table_set_row_cnt(tsk_table0, 1);
    for( col_idx = 0; col_idx < NUM_COLUMNS; col_idx++ ) {
		lv_table_set_cell_type(tsk_table0, 0, col_idx, 2);
		lv_table_set_col_width(tsk_table0, col_idx, 150);
    }

    page2 = lv_page_create( parent, NULL );
    lv_obj_set_size( page2, 800, ((lv_obj_get_height_fit( parent ) / 2)-45) );
    lv_obj_set_pos( page2, 96, (lv_obj_get_height_fit( parent ) / 2)+35 );
    setup_scrl_bar( page2, LV_PAGE_PART_SCROLLBAR, 8);
    lv_page_set_scrollbar_mode(page2, LV_SCROLLBAR_MODE_AUTO);
    tsk_table1 = lv_table_create(page2, NULL);
    lv_obj_set_drag_parent(tsk_table1, pdTRUE);
	lv_obj_set_style_local_text_font (tsk_table1, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, &lv_font_roboto_12);
    lv_obj_set_pos( tsk_table1, 0, 0 );
    lv_table_set_col_cnt(tsk_table1, NUM_COLUMNS);
    lv_table_set_row_cnt(tsk_table1, 1);
    for( col_idx = 0; col_idx < NUM_COLUMNS; col_idx++ ) {
		lv_table_set_cell_type(tsk_table1, 0, col_idx, 2);
		lv_table_set_col_width(tsk_table1, col_idx, 150);
    }
    update_task_list();
    lv_task_create((void*)update_task_list, 2001, LV_TASK_PRIO_LOWEST, NULL );  // Update the process table every 2 seconds

}

Hope that helps.

Kind Regards,

Pete

Hi, Pete,
Many Thanks!
Great help and I will try it.

Best Regards,
James