Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a CSS class to a column

Tags:

jqgrid

How can you add your own class to an column within jqgrid. As I see the html input elements are getting class "FormElement". I need to add an class to a particular column. I found this http://www.trirand.com/blog/?page_id=393/help/cell-tooltip-1/#p21526, but it I am not sure whether it is the most convenient way to achieve this? I mean to go through all rows and cells and then manually change a class property - it seems to be an overhead for such "simple" task.

UPDATE

I want to add class because I want to use the functionality of this multiselect widget http://quasipartikel.at/multiselect/. This widget works through defined classes. That's why.

like image 294
Anatoliy Avatar asked Aug 29 '12 14:08

Anatoliy


People also ask

How do I add a class in CSS?

If you want to use a class, use a full stop (.) followed by the class name in a style block. Next, use a bracket called a declaration block that contains the property to stylize the element, such as text color or text size. CSS Classes will help you stylize HTML elements quickly.

How do I inherit a class in CSS?

CSS Inheritance in CSS We can move the inheritance to CSS with the attribute selector. The attribute selector allows us to apply the base button properties to any element with a class that matches “button-“. It looks like this: [class*=“button-“] { /* base button properties */ } .

What does class *= mean in CSS?

[class*="button_type"] is CSS class Selector (equivalent to CSS attribute selector) means that will select all elements whose class contains at least one substring "button_type".


2 Answers

Probably you need to use classes property for the corresponding column.

I am not sure that it's what you need because you wrote about FormElement class existing in form. In the case you have to use beforeShowForm callback of the form editing for example to add class to the input field of the corresponding field of the edit form. The id of the fields in the form are the same as the name property of the corresponding column of colModel.

If you really need to add class attribute to the cells of one column you have one more possibility: defining cellattr callback for the column of colModel. The way could be practical if you need to add the class not for all cells of the column. You can test some conditions based on the row content and set the class only if the condition take place. For example the usage of classes:'ui-state-error-text ui-state-error' will set corresponding two classes (ui-state-error-text and ui-state-error) on all cells on the column. On the other side the callback function

cellattr: function(rowId, val, rawObject) {
    if (parseFloat(val) > 200) {
        return " class='ui-state-error-text ui-state-error'";
    }
}

allows you to set the class only if the cell value is higher as 200. I don't used rawObject in the above callback and so one could remove the optional parameter. I added it in the callback only to remind you that one can use the parameter to access to the values from another columns of the row. So you can implement even more complex scenarios in cellattr.

As the result one can get the grid like on the following picture:

enter image description here

UPDATED: If you need to add class on the input field of the edit forme you can additionally usedataInit callback of editoptions. In the case the usage will be very simple. You can do for example the following:

editoptions: {
    dataInit: function (domElem) {
        $(domElem).addClass("ui-state-highlight");
    }
}

As the result you will get the edit form like

enter image description here

The demo you can find here.

like image 95
Oleg Avatar answered Sep 23 '22 00:09

Oleg


There is a classes colmodel option that does just what you need. From the jqGrid documentation:

classes

string

This option allow to add classes to the column. If more than one class will be used a space should be set. By example classes:'class1 class2' will set a class1 and class2 to every cell on that column.

In the grid css there is a predefined class ui-ellipsis which allow to attach ellipsis to a particular row. Also this will work in FireFox too.

like image 44
Justin Ethier Avatar answered Sep 21 '22 00:09

Justin Ethier