Bootstrap-table: Can cellStyle know what field it is styling?

Created on 15 Mar 2016  路  6Comments  路  Source: wenzhixin/bootstrap-table

cellStyle can receive the value, row, and row index, but how can it know the name of the column? Sometimes it is possible to guess the column by comparing value to all of the row object property values, but if the values are, e.g. true or false, this approach doesn't work.

feature

Most helpful comment

Added field parameter to cellStyle:

function cellStyle(value, row, index, field) {}

All 6 comments

@martinburch - its the same argument being asked about similar functions like the footerFormatter

1856 Add Field to Footer-Formatter

The short answer that this is a column level option, meaning that it ofcourse should know the column its related to without needed fieldname param.

To make that fit into a slim code approach without redundant code, just use convenience functions like in the linked topic #1856 .

I would also be curious if (like footerFormatter) the this context worked there, but even if not wrappers should be enough.

A similar idea would be apply classname that is derivative of column name or purpose to each column. Then in cellStyle return generic classes for true|false or similar. Its then the combo of those 2 which the css rule applies to, something like:

.col1{ color:#333; }
.valTrue{ border-color:cyan; }
.col1.valFalse{ color:red; }
.col1.valTrue{ color: green; }
.col2.valTrue{ color: blue; }

Unfortunately, the this context for cellStyle has this.fields which is an array of all fields in the row. As far as I can tell, it doesn't have a property to get the current field.

Those convenience functions aren't very convenient. I currently have about 10 functions that look like this:

function fieldNameCellStyle(value, row, index) {
    return assembleCellStyle(value, row, index, "fieldName");
}

@martinburch - look at css classes then and intersection of classes to provide overrides, css is prob cleanest way since i cant imagine too much variance and you can explicit the 'type' of column in data-classes column property, rather than strict class per column.

@dabros I appreciate your suggestion, but I am using the field name to look up additional styling information on a per-cell basis (row+field in another object), so the style isn't predetermined by the column or row position.

@martinburch - i understand that, but was saying it could help simplify and cut down on the number of overloads if same basic result came out of multiple combinations - define one or more baseline styles for all false booleans, then override only when needed using combo of outputted classes (from column AND cellStyle)

Just some thoughts, np

Added field parameter to cellStyle:

function cellStyle(value, row, index, field) {}
Was this page helpful?
0 / 5 - 0 ratings