# Adding Table Columns in Perspective with Script or Expression

**URL:** <https://forum.inductiveautomation.com/t/adding-table-columns-in-perspective-with-script-or-expression/67183>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [December 2, 2022, 3:22pm UTC](https://forum.inductiveautomation.com/t/adding-table-columns-in-perspective-with-script-or-expression/67183 "2022-12-02T15:22:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![CTrenkamp](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ctrenkamp/32/38440_2.png) [@CTrenkamp](https://forum.inductiveautomation.com/u/CTrenkamp)\
**Post date:** [December 2, 2022, 3:22pm UTC](https://forum.inductiveautomation.com/t/adding-table-columns-in-perspective-with-script-or-expression/67183/1 "2022-12-02T15:22:07Z")

</div>

Is it possible to add columns to table props with code dynamically. I was not able to find a solution but only this which states it can be done manually. I am trying to change the number of columns based on what table is selected from a dropdown. I want to do this so I can manipulate the columns for certain items.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/b/8b2603cb1aa5f5af3884097c5234a91b627d2791.png)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 2, 2022, 3:32pm UTC](https://forum.inductiveautomation.com/t/adding-table-columns-in-perspective-with-script-or-expression/67183/2 "2022-12-02T15:32:55Z")

</div>

So, you're binding to data gets results from multiple tables with different columns?

The generic solution would be to bind the columns property to the data property with a transform that yields the appropriate column configuration for the columns present in the table data. Or for the most flexibility, consider using a custom property for the raw binding to data, in dataset format, then bind that to both `columns` and `data`, with the appropriate transforms for each. (Make that dataset property private, too, to minimize unnecessary browser traffic.)

---

<div class="post-metadata">

**Author:** ![CTrenkamp](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ctrenkamp/32/38440_2.png) [@CTrenkamp](https://forum.inductiveautomation.com/u/CTrenkamp)\
**Post date:** [December 2, 2022, 3:36pm UTC](https://forum.inductiveautomation.com/t/adding-table-columns-in-perspective-with-script-or-expression/67183/3 "2022-12-02T15:36:13Z")

</div>

I want the columns property to expand and contract based on the number of columns. Yes the column headers are different in most cases.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/2/621fa615e3cfd662c05af2a70c78936d84123375.png)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 2, 2022, 3:36pm UTC](https://forum.inductiveautomation.com/t/adding-table-columns-in-perspective-with-script-or-expression/67183/4 "2022-12-02T15:36:54Z")

</div>

> [@CTrenkamp](#):
>
> I want the columns property to expand and contract based on the number of columns.

Generate the entire `columns` property in a transform script.

---

<div class="post-metadata">

**Author:** ![CTrenkamp](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ctrenkamp/32/38440_2.png) [@CTrenkamp](https://forum.inductiveautomation.com/u/CTrenkamp)\
**Post date:** [December 2, 2022, 3:37pm UTC](https://forum.inductiveautomation.com/t/adding-table-columns-in-perspective-with-script-or-expression/67183/5 "2022-12-02T15:37:23Z")

</div>

My biggest issue is the datetime columns don't export or display correctly. My project allows you to select a table and export to the local computer in excel.

---

<div class="post-metadata">

**Author:** ![ShaneH](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@ShaneH](https://forum.inductiveautomation.com/u/ShaneH)\
**Post date:** [December 2, 2022, 3:41pm UTC](https://forum.inductiveautomation.com/t/adding-table-columns-in-perspective-with-script-or-expression/67183/6 "2022-12-02T15:41:13Z")

</div>

Chris-

Probably not the best, but what I typically do in scripting when trying to programmatically add a column at run time (assuming the table is already setup with at least one column):

1. make a copy of column[0]
2. update the settings of the column (field name, labels, types, etc.)
3. append the new column to the 'columns' property of the table

It's important that the field name referenced exists in the table data, or you will get a component error.

You can of course try to create the column object completely from scratch, I've just found that for more basic table layouts, coping an existing column and updating the field and label props to be easiest/fastest.

Header/footer groups, styles, etc. get a bit more "hairy" to update in scripting, but it can be done as well.

HTH.  
-Shane

---

<div class="post-metadata">

**Author:** ![CTrenkamp](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ctrenkamp/32/38440_2.png) [@CTrenkamp](https://forum.inductiveautomation.com/u/CTrenkamp)\
**Post date:** [December 2, 2022, 3:43pm UTC](https://forum.inductiveautomation.com/t/adding-table-columns-in-perspective-with-script-or-expression/67183/7 "2022-12-02T15:43:04Z")

</div>

Unfortunately I do not know how to do this. If I see code I can decode it for myself but to create it I struggle sometimes with situations like this I have never had to do. Would you happen to have a guide on how to approach these types of items with code examples?

---

<div class="post-metadata">

**Author:** ![ShaneH](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@ShaneH](https://forum.inductiveautomation.com/u/ShaneH)\
**Post date:** [December 2, 2022, 3:50pm UTC](https://forum.inductiveautomation.com/t/adding-table-columns-in-perspective-with-script-or-expression/67183/8 "2022-12-02T15:50:14Z")

</div>

Something like this, probably located in an on change script of the table data property:

```auto
	ds = self.props.data     
	num_cols = ds.getColumnCount()

	# add columns to table out to num_cols
	while (len(self.props.columns) < num_cols):
		self.props.columns.append(dict(self.props.columns[0]))

	# if 3 were your new column
	self.props.columns[3].field = ds.getColumnName(3)
	self.props.columns[3].header.title = 'Field Name'

```

Obviously a lot more cases to handle... like if table already has more columns than the ds, etc.  
And you might need to set the render type (or just use 'auto' everywhere.)  
But hopefully this let's you get started.

HTH.  
-Shane
