Change the background color of a column in a grid - axapta

I have following form and I want to change the background color of a column, based on the values of other columns;
In the orange columns, instead of displaying orange background, I want the cell color to be the RGB combo of Red, Green & Blue fields under COLOR ATTRIBUTES section.

Let's say that the control the background of which you need to change is named FirstFieldControl. Set its AutoDeclaration property to Yes and BackgroundColor to Window background.
Now you need to override the displayOption method on your datasource, e.g.:
public void displayOption(Common _record, FormRowDisplayOption _options)
{
YourTable yourTable = _record;
int color;
;
switch (yourTable.Name)
{
case 'Red' :
color = WINAPI::rgbCon2int([255, 0, 0]);
break;
case 'Green' :
color = WINAPI::rgbCon2int([0, 255, 0]);
break;
case 'Blue' :
color = WINAPI::rgbCon2int([0, 0, 255]);
break;
}
if (color)
{
_options.backColor(color);
_options.affectedElementsByControl(FirstFieldControl.id());
}
else
{
super(_record, _options);
}
}
This is just an example to give you an idea - don't copy-paste :)
It's easier to store the color value in the table, then the code will be much nicer.
P.S. If you're changing the colors run-time you might need to use the following piece of code to refresh the record:
yourTable_ds.clearDisplayOption(yourTable);
yourTable_ds.refresh();

I want to show variable color according to warehouse in on-hand form. If I override displayOption on inventdim datasource, it does get called. It does get called if I override InventSum datasource. But I cannot get the actual inventdim record. In this form, InventSum is master table, and InventDim is the joined child table.

Related

Change one color in all css style by javascript

I have a template with multiples class using one color, can I dynamically change that color to another using javascript?
When the page loads, locate all the div, span, p, h, with color #512a69 and change it to #ef7e8e.
Is it possible?
Thanks.
Here is a solution which I'll explain step-by-step.
First, call colorReplace("#512a69", "#ef7e8e");. The first value is the target color, and the second is the replacement color.
Inside it, $('*').map(function(i, el) { will select all tags in the DOM tree. For each element, its getComputedStyle(el) styles array is returned. You can customize the selector for faster processing (e.g. $('div').map(function(i, el)) {).
All style attributes containing "color" (e.g. background-color, -moz-outline-color, etc.), it will be checked if the color value is equal to your target color. If so, it will be replaced with the target color.
Colors are returned like rgba(0,0,0,0) or rgb(0,0,0), not like #FFFFFF, so a quick conversion is done from rgb to hex for the comparison. That uses the internal rgb2hex() function.
I hope this is what you are looking for.
function colorReplace(findHexColor, replaceWith) {
// Convert rgb color strings to hex
// REF: https://stackoverflow.com/a/3627747/1938889
function rgb2hex(rgb) {
if (/^#[0-9A-F]{6}$/i.test(rgb)) return rgb;
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
function hex(x) {
return ("0" + parseInt(x).toString(16)).slice(-2);
}
return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
// Select and run a map function on every tag
$('*').map(function(i, el) {
// Get the computed styles of each tag
var styles = window.getComputedStyle(el);
// Go through each computed style and search for "color"
Object.keys(styles).reduce(function(acc, k) {
var name = styles[k];
var value = styles.getPropertyValue(name);
if (value !== null && name.indexOf("color") >= 0) {
// Convert the rgb color to hex and compare with the target color
if (value.indexOf("rgb(") >= 0 && rgb2hex(value) === findHexColor) {
// Replace the color on this found color attribute
$(el).css(name, replaceWith);
}
}
});
});
}
// Call like this for each color attribute you want to replace
colorReplace("#512a69", "#ef7e8e");
Source: https://stackoverflow.com/a/30724171/1136132

Highlighting multiple cells in a JavaFX TableRow

Jewelsea has provided a great example of highlighting table rows and an individual cell at GitHub. However, I am having great difficulty with something that is closely related.
Here is a screen shot:
Using his example, when I perform the updateItem code for an individual cell in the "Will Pay Up" column, I would also like to highlight the corresponding name (which would be found in the same TableRow) with the same color as the cell in the will pay up column. In the code I am actually developing I have ensured that name is always found in the first column.
Here are just a few lines from his code for updateItem (I hope this is okay):
// update the item and set a custom style if necessary
if (item != null) {
setText(item.toString());
this.getStyleClass().add(item ? "willPayCell" : "wontPayCell");
this.getTableRow().getStyleClass().add(item ? "willPayRow" : "wontPayRow");
}
We can highlight the current cell (this.getStyleClass()) or the entire row (this.getTableRow()) but I'm unable to find a way to access another cell within the current row.
Set the style class of the cells to something constant, and the style class of the rows depending on the item's value:
if (item != null) {
setText(item.toString());
if (! this.getStyleClass().contains("willPayCell")) {
this.getStyleClass().add("willPayCell");
}
this.getTableRow().getStyleClass().removeAll("willPayRow", "wontPayRow");
this.getTableRow().getStyleClass().add(item ? "willPayRow" : "wontPayRow");
}
and then you can just use the CSS to determine the colors of different cells:
.table-row-cell.willPayRow .table-cell {
-fx-background: /* color for cells other than willPayCell in willPayRow */;
}
.table-row-cell.wontPayRow .table-cell {
-fx-background: /* color for cells other than willPayCell in wontPayRow */;
}
.table-row-cell.willPayRow .willPayCell {
-fx-background: /* color for willPayCell in willPayRow */;
}
.table-row-cell.wontPayRow .willPayCell {
-fx-background: /* color for willPayCell in wontPayRow */;
}

Ext JS on rowdblclick change clicked row background color

Hello
I'm new to Ext JS and I have created grid whidth folowing fields
columns: [
{
header : 'Firs name',
width : 200,
sortable : true,
dataIndex: 'firstName'
},
{
header : 'Last name',
width : 200,
sortable : true,
dataIndex: 'lastName'
},
{
header : 'Favourite color',
width : 195,
sortable : true,
dataIndex: 'favouriteColor'
}
],
Values will be generated in php.
I have to make that when user doubleCllick on any row, that row's background color turns into user's favourite color (Red, Blue, Yellow).
So far I've done that
grid.on('rowdblclick', function(grid,index,e) {
alert(index);
});
... I got the index of the clicked row, but I don't know how to change its background color. Will appreciate any help.
You need to make use of the GridView object to get the DOM of the selected row. And apply CSS with your favorite color onto that row. First you need to create few CSS classes:
.redrow {
background-color: red !important;
}
Similarly for blue and yellow. Next you need to get the row and add the CSS class to the row.
grid.on('rowdblclick', function(grid,index,e) {
var color = grid.getStore().getAt(index).get('favouriteColor');
if(color == 'red')
Ext.fly(grid.getView().getRow(index)).addClass('redrow');
else if( color == 'blue')
Ext.fly(grid.getView().getRow(index)).addClass('bluerow');
.
.
.
});
If you are trying to change the gird row background color according to the the favouriteColor column, you need to use another technique. You can make use of the ViewConfig and implement the getRowClass method as shown below:
viewConfig: {
forceFit: true,
getRowClass: function(record, index,prarms,store) {
var color = record.get('favouriteColor');
if(color == 'red')
return 'redrow';
else if(color == 'blue')
return 'bluerow';
else if (color == 'yellow')
return 'yellowrow';
else
return;
}
}
The ViewConfig is used along with the grid declaration. You don't make use of the return value of the getRowClass. The framework makes use of the return value. You only need to write logic for selecting the right CSS class for the row. getRowClass method can be used if you are need to display the background colors when the grid is rendered. It cannot be used during user events or after the grid is rendered.
In your case, you don't need this method because you are changing the color of the row when the user double click the row right? So, you can refer to the first part for the answer where you change the row's background according to the favouriteColor value for that row.

How to make cxgrid row color transparent

I'm changing grid's row color to red according to my needs and when it gets colored the text is invisible, under backroung color...
How to change the text color or make it transparent ???
void __fastcall TfRegular::tvFillOutCustomDrawCell(TcxCustomGridTableView *Sender,
TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
bool &ADone)
{
TRect ARec;
ARec=AViewInfo->Bounds;
//AViewInfo->GridRecord->DisplayTexts[7]="+" + AViewInfo->GridRecord->DisplayTexts[7];
// ShowMessage(AViewInfo->GridRecord->Values[13]);
if (AViewInfo->GridRecord->Values[13]>1) {
ACanvas->Canvas->Brush->Color = clRed;
ACanvas->Canvas->Font->Color = clBlack;
ACanvas->Canvas->Pen->Color = clRed;
ACanvas->Canvas->FillRect(ARec);
SetBkMode(ACanvas->Canvas->Handle,TransparentColor);
//InflateRect(AViewInfo->Bounds,2,2);
//ACanvas->DrawTexT("",AViewInfo->Bounds,0);
ADone=true;
}
}
It was really easy than I thought.
I solved this problem using OnGetContentStyle event.

In a QTableWidget, changing the text color of the selected row

I'm using a QTableWidget to display several rows. Some of these rows should reflect an error and their text color is changed :
Rows reflecting that there is no error are displayed with a default color (black text on white background on my computer).
Rows reflecting that there is an error are displayed with a red text color (which is red text on white background on my computer).
This is all fine as long as there is no selection. As soon as a row is selected, no matter of the unselected text color, the text color becomes always white (on my computer) over a blue background.
This is something I would like to change to get the following :
When a row is selected, if the row is reflecting there is no error, I would like it to be displayed with white text on blue background (default behavior).
If the row is reflecting an error and is selected, I would like it to be displayed with red text on blue background.
So far I have only been able to change the selection color for the whole QTableWidget, which is not what I want !
Answering myself, here is what I ended up doing : a delegate.
This delegate will check the foreground color role of the item. If this foreground color is not the default WindowText color of the palette, that means a specific color is set and this specific color is used for the highlighted text color.
I'm not sure if this is very robust, but at least it is working fine on Windows.
class MyItemDelegate: public QItemDelegate
{
public:
MyItemDelegate(QObject* pParent = 0) : QItemDelegate(pParent)
{
}
void paint(QPainter* pPainter, const QStyleOptionViewItem& rOption, const QModelIndex& rIndex) const
{
QStyleOptionViewItem ViewOption(rOption);
QColor ItemForegroundColor = rIndex.data(Qt::ForegroundRole).value<QColor>();
if (ItemForegroundColor.isValid())
{
if (ItemForegroundColor != rOption.palette.color(QPalette::WindowText))
{
ViewOption.palette.setColor(QPalette::HighlightedText, ItemForegroundColor);
}
}
QItemDelegate::paint(pPainter, ViewOption, rIndex);
}
};
Here is how to use it :
QTableWidget* pTable = new QTableWidget(...);
pTable->setItemDelegate(new MyItemDelegate(this));
What you'll want to do is connect the selectionChanged() signal emitted by the QTableWidget's QItemSelectionModel to a slot, say OnTableSelectionChanged(). In your slot, you could then use QStyleSheets to set the selection colours as follows:
if (noError)
{
pTable->setStyleSheet("QTableView {selection-background-color: #000000; selection-color: #FFFFFF;}");
}
else
{
pTable->setStyleSheet("QTableView {selection-background-color: #FF0000; selection-color: #0000FF;}");
}
It looks ok, but you might want to look at the documentation of QStyleOption it can tell you wether the item drawn is selected or not, you don't have to look at the draw color to do that. I would probably give the model class a user role that returns whether the data is valid or not and then make the color decision based on that. I.e. rIndex.data(ValidRole) would return wether the data at this index is valid or not.
I don't know if you tried overriding data for the BackgroundRole and returning a custom color, Qt might do the right thing if you change the color there
You could, of course, inherit from the table widget and override the paint event, but I don't think that is what you want to do.
Instead, should use the QAbstractItemDelegate functionality. You could either create one to always be used for error rows, and set the error rows to use that delegate, or make a general one that knows how to draw both types of rows. The second method is what I would recommend. Then, your delegate draws the rows appropriately, even for the selected row.
You could use e.g. a proxy model for this where you return a different color if you have an error for the specific modelindex;
QVariant MySortFilterProxyModel::data(const QModelIndex & index, int role = Qt::DisplayRole) {
// assuming error state and modelindex row match
if (role==Qt::BackgroundRole)
return Qt::red;
}

Resources