1st column width via script

Is there a way to change the width of the first column of a table via script.
I would like to do this as the number of columns is dinamic but the first one is constant.

Any ideas welcome.
Cheers!

Modify the width property for that column. See below.

1 Like

Thanks @dillon ,Yes, I was thinking to do it there...but as the number of columns are dinamic depending on the current month of the year I can´t hardcode a number of columns.

Once the number of columns is known in the script you could modify the columns array property to have the correct number of objects appended after the static first column.

1 Like

Ooo, I didn´t know this, thanks!, I will give it a try.

Insert my advice was not tested disclaimer here

Seems reasonable to me from past experiences. If you just have the first static column defined in the designer and then use scripting to append to the list, it will show what you need but will not persist.

1 Like

ok, time for me to go home, I give it a try tomorrow, thanks!

1 Like

Put a change script on the 'columns' property, check the length of the array, update columns[0]['width'] if the length is > 1.

1 Like

I share this:

If you need know exact index number of columns on a table i do this Object (dic):

--Expresion Binding:
 {./flex_tabla/tbl_listado.props.columns} (Here is my Table component with JSON return format)

--Transform Script:
def transform(self, value, quality, timestamp):

    column_indices = {}

    #Recorrer filas de col.
    for index, column in enumerate(value):
        # Agregar el nombre del campo ('field') y su indice al dic
        field_name = column.get("field", None)
        if field_name:  # Verificar que exista valor para 'field'
           column_indices[field_name] = index

    #Devolver el diccionario que contiene los campos y sus índices
    return column_indices

And here is, how i know always the exact index of column, sure there's another way, but it's worked quite well for me :slight_smile: .

1 Like

That looks like a good candidate for a comprehension:

def transform(self, value, quality, timestamp):
    return {col['field']: i for i, col in enumerate(value) if col.get('field')}
3 Likes

I don't know if this will help:

If columns.x.strictWidth : true then column.x.width = 120 sets that column to 120 pixels wide.

If columns.x.strictWidth : false then column.x.width sets width of that column relative to the width of the other strictWidth = false columns in the remaining available space.

| <------------------------ table width --------------------------> |
| Col 1 (120px)      | Col 2                     | Col 3            |
| width : 120        | width : 3                 | width : 2        |
| strictWidth :      | strictWidth :             | strictWidth :    |
|               true |                     false |            false |

Columns 2 and 3 are in the ratio 3 : 2 so column 2 is 150% times the width of column 3.

2 Likes

Probably can bind the columns property to the data property and just set the 1st column to strictWidth and the width value.

def transform(self, value, quality, timestamp):
	try:
		columns = value.getColumnNames() # get column names if dataset
	except:
		columns	= value[0].keys() # get column names if json array
		
	return [{
		'strictWidth': True,
		'width': 120,
		'field': columns[0] # grab first column name or can specify which ever one by exact name
	}] + [{}] * (len(columns)-1) # fill in the rest with blank objects for the remaining columns

EDIT: the column property would get reevaluated every time your data changes though. good if your column count changes during run time, if not, you can just bind to a static expression like so and I think it should only execute once when your view opens

def transform(self, value, quality, timestamp):
	try:
		columns = self.props.data.getColumnNames() # get column names if dataset
	except:
		columns	= self.props.data[0].keys() # get column names if json array
		
	return [{
		'strictWidth': True,
		'width': 120,
		'field': columns[0] # grab first column name or can specify which ever one by exact name
	}] + [{}] * (len(columns)-1) # fill in the rest with blank objects for the remaining columns
1 Like

Thank you all for your answers.

Finally I opt for:

binding this.props.data property to the columns.

Aplied a script transfrom to it:

def transform(self, value, quality, timestamp):
	try:
		columns = self.props.data.getColumnNames()
	except:
		columns = list(self.props.data[0].keys())
	
	return [
		{
			'field': col,
			'width': 182,
			'strictWidth': True
		} if i == 0 else {}
		for i, col in enumerate(columns)
	]

and now the first column has the necesary width to show the main table data.

image

Cheers!