Bean
Bean

Reputation: 550

jqGrid Make a Row readonly

I have a grid setup using form editing. I want the the user to be able to edit only some of the rows. As a start, I figured the easiest way to do this was to have a column (probably hidden) in my server query and XML that denotes the Access or Role the user has. So essentially the grid now has a column "Access Role" with 'Y' or 'N' for each row. (where Y = user can edit, N = View/readonly)

I've tried a couple things to implement this. The best I've come up with is using the rowattr function, but my use is flawed since it hides the row in the grid (I don't want it hidden, just readonly):

function (rd) {
console.log('Row = '+rd.WWBPITM_SURROGATE_ID);
    if (rd.ACCROLE === "N") {
      console.log('RowAttr '+rd.ACCROLE);
        return {"style": "display:none"};
    }

This might be a start, but I'm not sure where to go from here and I'm not sure if I'm barking up the wrong tree with using rowattr.

I also tried using setCell in a loadComplete function, like this:

function GridComplete() {
  var grid = $('#list1');
  var rowids = grid.getDataIDs();
  var columnModels = grid.getGridParam().colModel;
  console.log('Check ACCROLE');
  // check each visible row
  for (var i = 0; i < rowids.length; i++) {
    var rowid = rowids[i];
    var data = grid.getRowData(rowid);
    console.log('ACCROLE for '+rowid+' is '+data.ACCROLE);
    if (data.ACCROLE == 'N') {       // view only
      // check each column
      //console.log(data);

      for (var j = 0; j < columnModels.length; j++) {
        var model = columnModels[j];

        if (model.editable) {
        console.log('Is Editable? '+model.editable);
          //grid.setCell(rowid, model.name, '', 'not-editable-cell', {editable: false, edithidden: true});
          grid.setCell(rowid, model.name, '', 'not-editable-cell', {editoptions: { readonly: 'readonly', disabled: 'disabled' }});
        }
      }
    }
  }
}

But the editoptions don't seem to do anything with this.

Any ideas how to do this?

Upvotes: 0

Views: 3437

Answers (3)

Bean
Bean

Reputation: 550

In case it will be helpful for others, here is how I am implementing Read Only rows in Form Editing mode, based on a column which designates what level of access the user has to each row:

In editoptions, use the beforeShowForm event, like so:

beforeShowForm: function (formid) {
console.log('Checking for READONLY '+formid.name);                         
            var selectedRow = jQuery("#list1").jqGrid('getGridParam','selrow');  //get selected rows
            var selRole = $("#list1").jqGrid('getCell', selectedRow, 'ACCROLE');
            if(selRole == 'N' || selRole == 'S' || selRole == 'R')
            {
                //$("<div>Sorry, you do not have access to edit this record.</div>").dialog({title: "Access Denied"});
                formid.find("input,select,textarea")
                .prop("readonly", "readonly")
                .addClass("ui-state-disabled")
                .closest(".DataTD")
                .prev(".CaptionTD")
                .prop("disabled", true)
                .addClass("ui-state-disabled");

                formid.parent().find('#sData').hide();
                var title=$(".ui-jqdialog-title","#edithd"+"list1").html();
                title+=' - READONLY VIEW';
                $(".ui-jqdialog-title","#edithd"+"list1").html(title);
                formid.prepend('<span style="color: Red; font-size: 1em; font-weight: bold;">You viewing READONLY data.</span>');

            }

Upvotes: 0

Brian Woelfel
Brian Woelfel

Reputation: 26

OK thanks for explaining about Form editing. Here's an example of how to prevent edits on certain records for jqGrid with form editing:

  • Start with this example of jqGrid form edit: http://www.ok-soft-gmbh.com/jqGrid/MulticolumnEdit.htm
  • Use the beforeInitData event to check your data before the edit form is displayed. Note that this is bound to the pager object.
  • Use getGridParam and getCell methods to get the current value you want. In my example I grabbed the client name
  • Add your own business logic for checking (I don't allow edits on 'test2')
  • Return false to prevent the edit form from popping up.
  • This example only handles edit, not insert or delete.
  • Replace $grid.jqGrid("navGrid", "#pager",...) from the example with this:

    $grid.jqGrid("navGrid", "#pager", {view: true}, 
        // Events for edit
        {
            beforeInitData: function (formid) {
                var selectedRow = jQuery("#list").jqGrid('getGridParam','selrow');  //get selected rows
                var selectedClient = $("#list").jqGrid('getCell', selectedRow, 'name');
                if(selectedClient == 'test2')
                {
                    alert('You are not allowed to edit records for client "' + selectedClient + '"');
                    return false;
                }
            }       
        },
        // Events for add
        {
            beforeShowForm: function (formid) {
            }       
        }
    );
    

Upvotes: 1

Brian Woelfel
Brian Woelfel

Reputation: 26

You didn't provide much information about how you're updating rows (there are various methods as described in JQGrid web page demos, but I took a guess as to a possible solution. I started with the example on the bottom of this page (trirand's web site wiki for inline_editing) http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing and made a few changes.

  • Added a new data column securityGroup, and put in data like 'A', 'B', 'C'
  • Displayed the new data column in the grid
  • The example used the onSelectRow event to start editing a row if you clicked on a new row. I updated this callback to check the value of row['securityGroup'], and only start .editRow if it's in securityGroupA
  • JSFiddle at http://jsfiddle.net/brianwoelfel/52rrunar/

Here's the callback:

onSelectRow: function(id){
    var row = $(this).getLocalRow(id);

  if(id && id!==lastsel2){
    jQuery('#rowed5').restoreRow(lastsel2);
    if(row['securityGroup'] == 'A') {
        jQuery('#rowed5').editRow(id,true);
    }
      lastsel2=id;
  }
},

If this method won't work for you, please provide more information about how you're currently doing edits with jqGrid. This example obviously is very trivial and doesn't even post to PHP or mysql or anything.

Upvotes: 0

Related Questions