Formatting a dataset without converting into JSON

How do i style a row in a BasicDataset based on its value without converting to json?

Here’s my data -

I have an invisible column ‘IsUsed’. Based on its value (‘Y’ or empty quotes), the row colour should be blue

  • If ‘Y’ - row in blue
  • If ‘ ' - no colour

I do not really want to convert to JSON, because i have a few more functions on the screen which depend on the data source being a dataset.

Thanks

I hope this ignition article could help you out

Yep…Have used this before but my current requirement needs me to style a dataset without converting to any other type

Just use a view cell renderer and apply the style on the view.

Move the data binding to a view custom property. Use a property binding and transform (or my Integration Toolkit's expression functions) on the table's props.data to convert to JSON from the dataset, adding the styling on the fly.

All other consumers of the source dataset would point at the custom prop.

(You should do this anyways, so your other components don't have to use cross-container complex references to get the data. Such things break when you move components around.)

You should also set the view custom property to private, to reduce browser traffic.

2 Likes

Was able to get the row color based on the condition… I wanted to disable the checkbox at the highlighted rows, so the following was the code i added under transform of the table -

	columns = system.dataset.getColumnHeaders(value)
	data = []
	for row in range(value.getRowCount()):
		row_obj = {}
		for column in range(value.getColumnCount()):
			row_obj[columns[column]] = value.getValueAt(row, column)
		data.append(row_obj)
		
	formatted_data = [] 
	for row in data: 
		row_dict = {}
		for k, v in row.items():
			if k in ('Length1', 'Length2', 'OD Size', 'Width'):
				if str(row.get("IsUsed", "")).strip() == 'Y': 
					style = {"backgroundColor": "cyan"} 
					editable = False
				else: 
					style = {"backgroundColor": ""} 
					editable = True
			else:
				if str(row.get("IsUsed", "")).strip() == 'Y': 
					style = {"backgroundColor": "cyan"} 
					editable = False
				else: 
					style = {"backgroundColor": ""} 
					editable = False
			if k == 'Selection':
				if str(row.get("IsUsed","")).strip() == 'Y':    # Setting selection to 0 and disabling checkbox
					v = 'false'   # Yes, tried False, doesn't work...Since the checkbox component has a lowercase false/true, i used the same thing
					editable = False
		
			
			row_dict[k] = {"value": v, "style": style, "editable": editable}
			
		formatted_data.append(row_dict)
#	column_names = ['SL_No','SerialNo', 'Length1','Length2','OrderNo','Selection','IsUsed','DateAndTime']
	dataset = self.view.custom.cupDetails
	column_names = dataset.getColumnNames()
	for l in range(len(column_names)):
		self.props.columns[l].field = column_names[l]
	return formatted_data

When i try to set the value of v to false when condition is true, i get no change. Any ideas on how i might be able to achieve this?

Thanks :slight_smile:

1 Like

I think you have really complicated this:

I believe (untested code) that this code will accomplish what you are looking for.

def transform(self, value, quality, timestamp):
	#You manually converted the dataset into a list of dicts so that you could iterate through the data
	#sysetm.dataset.toPyDataSet() already does this natively.
	#Yes I know it isn't exactly a "lists of dicts" but this is python, and it looks like that duck
	rawPyData = system.dataset.toPyDataSet(value)
	data = []
	for row in rawPyData:		
		#We'll hold the value of IsUsed for further processing,
		isUsed = row['IsUsed'] == 'Y':
		
		#Now loop through the columns and modify the 
		data.append({k:{ 
			# value is only changed if the column is the Selection column and the row IsUsed
			'value':v if k != 'Selection' or (k == 'Selection' and not IsUsed) else False, 
			# Add the background color if the row IsUsed
			"style":{"backgroundColor":"cyan" if isUsed else "" },
			# Column is marked editable if it is one of the listed columns and the row is not used.
			# Otehrwise it is false
			"editable": True if k in ('Length1', 'Length2', 'OD Size', 'Width') and not isUsed else False
			} 
			for k,v, in row.iteritems() 
		})

	return data[]

You comment in your code that the builtins (True and False) don't work, however, I have had no issues using these, I suspect there is something else wrong. Perhaps there is something else writing to the data the table is displaying.

You have code in this that is setting the field names of all of the columns, but this is a side effect and really shouldn't be in a transform. This type of thing leads to other values being overwritten and then a programmer not understanding why they're values are not working. This also muddies the waters when looking at the code as the code is unrealted to the true purpose of the transform.

The code should really be located in a property change event on the custom property that is holding the raw dataset, if the field names truely need to be dynamic. I highly doubt this is the case and would suggest you just hard code the names into the fields.

If they do need to be dynamic, then you really only want to do this when the raw dataset changes, not for instance if the transform is triggered through some other event.

I haven't tested this specific code on anything that closely resembles your data, but I did check similar code on sample data. It worked as I expected.

	def reorder_columns(data, order): 
		
		reordered = [] 
		for row in data: 
			new_row = {key: row.get(key) for key in order if key in row} 
			reordered.append(new_row) 
		return reordered	
	
	rawPyData = system.dataset.toPyDataSet(value)
	columns = value.getColumnNames()
	data = []
	
	for row in rawPyData:       
	    # Hold the value of IsUsed for further processing
	    isUsed = str(row['IsUsed']).strip() == 'Y'
	    row_dict = {}
	
	    for k in columns:
	        v = row[k]
	
	        if k == 'Selection':
	            row_dict[k] = {
	                "value": False if isUsed else True,
	                "style": {"backgroundColor": "cyan" if isUsed else ""},
	                "props": {"enabled": False} if isUsed else {"enabled": True}
	            }
	        else:
	            row_dict[k] = {
	                "value": v,
	                "style": {"backgroundColor": "cyan" if isUsed else ""},
	                "editable": True if k in ('Length1','Length2','OD Size','Width') and not isUsed else False
	            }
	
	    data.append(row_dict)
	data = reorder_columns(data, ['SL_No', 'SerialNo', 'Length1', 'Length2', 'OrderNo', 'Selection', 'IsUsed', 'DateAndTime'])
	return data

Wrote a separate function to reorder the dataset…made a few changes to the script

Able to get disabling of columns, making Selection as false requirements. But, still not able to disable the checkbox of the highlighted row. Even though, the data is actually being changed to do so. Data - (Json for the highlighted row should be at SLNo 2)

[
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113369"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 1
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123533
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 300
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 46.55
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": "cyan"
      },
      "value": "1125113371"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": "cyan"
      },
      "value": 2
    },
    "Selection": {
      "style": {
        "backgroundColor": "cyan"
      },
      "value": false,
      "props": {
        "enabled": false
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": "cyan"
      },
      "value": "Y         "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": "cyan"
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123580
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": "cyan"
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": false,
      "style": {
        "backgroundColor": "cyan"
      },
      "value": 298
    },
    "Length2": {
      "editable": false,
      "style": {
        "backgroundColor": "cyan"
      },
      "value": 47.65
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113373"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 3
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123613
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 296
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 48.75
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113374"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 4
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123630
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 294
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 49.85
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113375"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 5
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123660
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 292
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 50.95
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113376"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 6
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123690
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 290
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 52.05
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113377"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 7
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123707
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 288
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 53.15
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113378"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 8
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123753
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 286
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 54.25
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113379"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 9
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123783
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 284
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 55.35
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113380"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 10
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123817
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 282
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 56.45
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113381"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 11
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123847
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 280
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 57.55
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113382"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 12
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123880
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 278
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 58.65
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113383"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 13
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123923
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 276
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 59.75
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113384"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 14
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123957
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 274
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 60.85
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113386"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 15
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285123987
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 272
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 61.95
    }
  },
  {
    "SerialNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "1125113387"
    },
    "SL_No": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": 16
    },
    "Selection": {
      "style": {
        "backgroundColor": ""
      },
      "value": true,
      "props": {
        "enabled": true
      }
    },
    "IsUsed": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "          "
    },
    "DateAndTime": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": {
        "$": [
          "ts",
          192,
          1770701420477
        ],
        "$ts": 1770285124020
      }
    },
    "OrderNo": {
      "editable": false,
      "style": {
        "backgroundColor": ""
      },
      "value": "6192208"
    },
    "Length1": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 270
    },
    "Length2": {
      "editable": true,
      "style": {
        "backgroundColor": ""
      },
      "value": 63.05
    }
  }
]

Any ideas on how to disable the checkbox?

When you say "disable" do you mean make it uneditable? As in the user can not change if it is checked or not?

My understanding of your requierement was there could be many "highlighted rows", do you need the check boxes to be marked as false and also disabled?

I would suggest that instead of using a script to reorder the columns you use an expression binding and the columnRearrange() expression. This can also be used to exclude columns you don't want to be used. Then it just gets passed to the expression as value

Yep. Was able to achieve that using -

"Selection": { 
     "style": { 
       "backgroundColor": "cyan"     
 },      
"value": false,      
"props": {       
 "enabled": false      
}

Had to bind the props[‘enabled‘] value to props.enabled of the checkbox template.

Well that explains why you're having issue with using the builtin boolean values. If the column's data values are boolean values the table componet will automatically format the column with check boxes. You shouldn't need to use an embedded view to get a simple checkbox in the cells.

Otherwise, yes if you need to disable the checkbox you need to set the enabled prop to false.

1 Like