Jqgrid: changing row color dynamically - jqgrid

I am using Jqgrid . I would like to change the row color based on a column value . I am able to change the class of the row based on this column value .But what I would require is to change the font color with the color value I get from server. How this could be done?

You can do this by using a column custom formatter.
The formatter will be a javascript function that you write using the following format:
function myformatter ( cellvalue, options, rowObject )
{
// format the cellvalue to new format
return new_formated_cellvalue;
}
Where the grid will route these values:
cellvalue - is the value to be formatted.
options - is an object that contains information in regards to the
cell/row.
rowObject - is the row data in the format determined by your grid
datatype option.
So in your custom formatter you can take the cell value and apply either a class or inline font style to it, like so:
function myformatter ( cellvalue, options, rowObject )
{
if (cellvalue == "red")
return '<font color="red">' + cellvalue + '</font>';//or use classes
else
return '<font color="blue">' + cellvalue + '</font>';//or use classes
}
Then on your column definition you just specify which columns will be using this formatter, like so (add to any columns that requires a font color):
colModel: [
...
{name:'price', index:'price', width:60, align:"center", editable: true, formatter:myformatter},
...
]

Or you can try this also, in loadComplete, get all the row ids of jqgrid like this
var allRowsOnCurrentPage = $('#grid').jqGrid('getDataIDs');
and lets say you have name and company column in jqgrid and there are two rows. For one row data is like this
Name:xxx Company:yyy
and for the second row you have data like this
Name:aaa Company:bbb
So you get the Name value inside a for loop
for(int i=1;i<=allRowsOnCurrentPage.length;i++)
{
var Name=$('#grid').jqGrid('getCell',i,'Name');
if(Name="aaa")
{
$('#grid').jqGrid('setCell',i,"Name","",{'background-color':'yellow');
}
}
code is not tested, but should work.

Use cellattr property in colModel
cellattr: function (rowId, value, rowObject, colModel, arrData){
return 'style=background-color:black;'; }

Related

Kendo MultiSelect in grid no show default values

I use kendo MultiSelect as custom editor in kendo grid ,
MultiSelect work correctly when save changes but no show textvalue when press edit row button (MultiSelect is empty).
my custom editor function is:
function GRID_MULTISELECT_CUSTOM_EDITOR(container, options) {
var columnValue = String(options.model.POST_HISTORY).replace(/,/g,'","');
$('<input name="GRID_POST_LVL_MULTISELECT" id="GRID_POST_LVL_MULTISELECT" data-value-primitive="true" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoMultiSelect({
filter: "contains",
optionLabel: " ",
width: "100px",
dataTextField: "NAME_UNIT",
dataValueField: "CD_UNIT",
dataSource: prsListDataSource,
value: [columnValue],
change: function(e) {
selectedValue = e.sender.value();
apex.event.trigger($("#PRS_LIST_REG_POST_HISTORY_MULTISELECT"),"kapex-multiselect-change");
apex.event.trigger($("#PRS_LIST_REG_POST_HISTORY_MULTISELECT"),"kapex-multiselect-databound");
}
});
var ms = $("#GRID_MULTISELECT_CUSTOM_EDITOR").data("kendoMultiSelect");
console.log(ms.value());
}
console.log(ms.value()); show value is set but textvalue no show in MultiSelect widget.
When 1 value is stored in database MultiSelect work correctly and textvalue show in edit. but when sotre multi value, textvalue not show.
I store datavalue with this format in database column as varchar.
001,100,110,111,112
I recommend you to serialize the POST_HISTORY values as arrays to the client. This will spare the need to modify the model value on the fly in the custom editor function. You will also don't need to think how to transform the MultiSelect's array value back to a comma-separated string value after the user finishes editing a row.
http://dojo.telerik.com/IDUBI
Keep in mind that using the value configuration in the MultiSelect declaration will not work in this case, because the MVVM value binding is applied at a later stage and takes precedence.
On the other hand, if you absolutely need to serialize the POST_HISTORY values as comma-separated strings to the client, then use dataSource.schema.parse to transform these strings to arrays before the Grid is databound:
http://dojo.telerik.com/OQAGa
Finally, create the MultiSelect widget from a <select multiple>, not <input>.

Jqgrid using cellattr to toggle a class on seperate cell

in my col model i have two cells 'Status' and 'HiddenStatus'. The reason for this is 'Status' is translatable so this way is better rather than checking the value for each language. As you can see below i am attempting to set the class of the 'Status' cell based on the value of the 'HiddenStatus' cell. However this is not working as i hoped since the class is not being set correctly.
I believe the issue i am having is, 'getCell' is used to return the value rather than an object. How can i get the cell as an object so i can then manage what class i add or remove.
{name: 'Status', width: 70, index: 'Status'},
{name: 'HiddenStatus', width: 70, hidden: true, cellattr : function(rowId, cellValue, rawObject, cm, rdata){
var statusCell = $(this).jqGrid('getCell',rowId,'Status');
if(cellValue != "Assigned"){
$(statusCell).removeClass('status-assigned');
return '';
}
if(cellValue == "Assigned"){
$(statusCell).addClass('status-assigned');
return '';
}
}},
First of all, it's important to understand what cellattr do. Any changes on the existing page are expensive. Thus jqGrid try to build the whole HTML fragment with the table body as one long string. Every column will be used to build the cells (<td> elements) of the corresponding columns of the grid. The callback cellattr will be called during building the cell and can be used to set attributes on the cell. The cellattr callback have to return the string. If it returns, for example, " class='status-assigned'" then the <td> will have the class (<td class='status-assigned'>...</td>).
The callback cellattr will be called before the grid will be created. Thus one can't access to the grid using $(this).jqGrid('getCell',rowId,'Status'), for example.
If you need to set class status-assigned conditionally on the cells of the column Status then you should define cellattr callback in the column Status. Inside the callback you can use rawObject.HiddenStatus or rdata.HiddenStatus. One don't need typically to create hidden columns like HiddenStatus. Instead of that, it's enough to use include all properties of input data, which one need, in additionalProperties option of jqGrid. For example, additionalProperties: ["HiddenStatus"].
Your original code could be modified to the following:
{
name: 'Status', width: 70,
cellattr: function (rowId, cellValue, item) {
if (item.HiddenStatus === "Assigned") {
return " class='status-assigned'";
}
}
}
See the old answer for an example of usage cellattr.

Inline Editing: How can I access the edited row or cell-data?

onSelectRow: function (id) {
var row = jQuery('#list').jqGrid('getRowData', lastSel)
...
lastSel = id;
},
Specified in the [Docu]: http://www.trirand.com/jqgridwiki/doku.php?id=wiki:methods it will not give the actuall value. What can I use instead? The eventually changed data is not submited.
You posted too few code. So it's unknown how you implemented inline editing. In any way you will have the value of the editing cell as the value of the corresponding HTML control. One uses typically <input> or <select> for editing. So to get the value you need find the corresponding HTML element and get directly its value. For example you can use
$("#" + rowid + ">td:nth-child(" + (i + 1) + ")>input").val()
to get the value from the input of the cell from the i-th column or the row having id equal to rowid.
The old answer demonstrate a little other way to do the same. In any way you have to get the value of the corresponding cell directly.
function getTextFromCell(cellNode) {
return cellNode.childNodes[0].nodeName === "INPUT" ?
cellNode.childNodes[0].value :
cellNode.textContent || cellNode.innerText;
}
;
function getActualRowData(rowid) {
var row = [];
$('#' + rowid).find('td').each(function () {
row.push(getTextFromCell(this));
});
return row;
}

Need to pass a jqgrid column name to function triggered by the "onclickSubmit" event

I need to pass or make available a jqgrid colModel column name to a function triggered by the jqgrid event "onclickSubmit:" defined in the edit options of navGrid - but i don't know how to do that.
here are the jqgrid and javascript code segments:
..., onclickSubmit: fixpostdata}, // navGrid edit options
.
.
.
var fixpostdata = function(params, postdata){
var rowid = $('#tab3-grid').getGridParam('selrow');
// when the onclickSubmit event fires and calls this function,
// a string containing a jqgrid colmodel column name needs to be
// made available in order to modify that cell's value contained
// in the postdata array prior to posting it to the server.
columnName = ???;
var value = $('#tab3-grid').jqGrid('getCell', rowid, columnName );
postdata[ columnName ] = value;
return;
}
Can anyone help?
also,
what's contained in the params argument?
If you need to send contain of some hidden column to the server together with other editable columns you need include editable: true in the hidden column and add one more property
editrules: { edithidden: false }

JQGrid setCell customFormatter

I'm using setCell to set the value of a cell. The problem is it is still calling the customFormatter specified for the column. Is there anyway I can set the value of this cell without it having to go through the customFormatter?
First of all the custom formatter will be used on every grid refresh, so to set the cell value you have to do this after the custom formatter. The best place to do this is inside of loadComplete or gridComplete event handler.
To set the cell value you can use jQuery.text for example. So you should get jQuery object which represent the cell (<td> element) and then use jQuery.text or jQuery.html to change the cell contain. How I understand you, you knows the rowid of the cell and the column name which you want to change. The following code could be:
loadComplete: function() {
var rowid = '2', colName = 'ship_via', tr,
cm = this.p.colModel, iCol = 0, cCol = cm.length;
for (; iCol<cCol; iCol++) {
if (cm[iCol].name === colName) {
// the column found
tr = this.rows.namedItem(rowid);
if (tr) {
// if the row with the rowid there are on the page
$(tr.cells[iCol]).text('Bla Bla');
}
break;
}
}
}
See the corresponding demo here.

Resources