How to freeze a Dynamic column in JqGrid?

250 Views Asked by At

I have a something like this: i want to freeze the Doctor Name column which is Dynamic. Actually, the whole grid is dynamic.

  if (result[7] != null) {
                        var resu = JSON.parse(result[7]);
                        if (resu.length > 0) {
                            {
                                ColModel = [];
                                var model = Object.keys(resu[0]);
                                for (var i = 0; i < model.length; i++) {
                                    var responColNM = "";
                                    if (model[i] == "Doctor Name") {
                                        responColNM = {
                                            name: model[0], index: model[0], label: model[0], width: 140, editable: false, sortable: false, frozen: true,
                                        }
                                    }
                                    else {
                                        responColNM = {
                                            name: model[i], index: model[i], label: model[i], width: 43, editable: false, sortable: false,
                                        }
                                    }
                                    ColModel.push(responColNM);
                                }
                            }
                        }
                        strNew = resu;
                    }
                    else {
                        //store in arr
                        //str = { DOCTORNAME: '', CNT: '', DT: '' };
                        //strNew.push(str);
                    }

 if (str == 1) {
                colnames = [];
                colmodel = ColModel;
                $("#gvtable").jqGrid('setGridParam', { data: response }).trigger("reloadGrid");
                $("#gvtable").jqGrid('setFrozenColumns').trigger("reloadGrid");

here, the column Doctor name does not freeze. any suggestions? Thanks in advance!

1

There are 1 best solutions below

1
On

Some notes before to answer.

  1. jqGrid name in colModel can't contain spaces. In your case it contain spaces
  2. After call of setFrozenColumns method you do not need to trigger reloading the grid.
  3. Finally, but not least, colModel can't be a dynamically changed. In order to do this you will need to destroy the grid and recreate it with the new colModel

The frozen column should be the first one in the array of colModel in order to be frozen. In your code it is not clear if it is first. You set the first column, but it is not known if it is first added in the colModel