0

I have data displayed in a JQGrid, and I get an Actions column, where I have icons for Delete and Edit actions. In the data I retrieve to display in the grid, I have a boolean, and I would like to display the icons only when the boolean is true. How could it be done?

Here the piece of code of my JQGrid display:

jQuery("#datagrid").jqGrid({
        stateOptions: getStateOptions("creation-site"),
        url: listUrl,
        datatype: "json",
        loadError: viewError,
        colNames: ['', 'Nom', 'N de dépôt Geopost', 'IATA', 'Groupe ID', 'Site de rattachement', 'Poste comptable', 'Centre cout', 'Description'],
        colModel: [
            {name: 'myac', width: 80, fixed: true, sortable: false, resize: false, formatter: 'actions', formatoptions: {keys: true, editbutton: true, }},
            {name: 'nom', index: 'nom', editable: true, edittype: "text", sortable: true},
            {name: 'geopostDepotNumber', index: 'geopostDepotNumber', editable: true, edittype: "text", sortable: true},
            {name: 'iata', index: 'iata', editable: true, edittype: "text", sortable: true},
            {name: 'groupeId', index: 'groupeId', editable: true, edittype: "text", sortable: true},
            {name: 'siteRattachement', index: 'siteRattachement', editable: true, edittype: "text", sortable: true},
            {name: 'posteComptable', index: 'posteComptable', editable: true, edittype: "text", sortable: true},
            {name: 'centreCout', index: 'centreCout', editable: true, edittype: "text", sortable: true},
            {name: 'description', index: 'description', editable: true, edittype: "text", sortable: true}
        ],
        rowList: [10, 20, 50, 100, 500, 1000, 5000],
        pager: '#navGrid',
        sortname: 'title',
        sortorder: "asc",
        viewrecords: true,
        loadonce: true,
        gridview: true,
        ignoreCase: true,
        height: 'auto',
        editurl: 'clientArray',
        caption: '<spring:message code="creationsite.title"/>'
    });

Edit: Here a sample JSON, for 2 lines:

[{"id":1,"centreCout":"211177","geopostDepotNumber":"0401","iata":"MLV","posteComptable":"77999","referentielId":5,"siteRattachement":" ","nom":"Ceci est un nom","networkRefId":1,"networkRefName":"FR-CHR","description":"MARNE-LA-VALLEE","groupeId":"CHRF","manual":false},
{"id":2,"centreCout":"211174","geopostDepotNumber":"0402","iata":"FTV","posteComptable":"75998","referentielId":5,"siteRattachement":" ","nom":null,"networkRefId":1,"networkRefName":"FR-CHR","description":"ALFORTVILLE","groupeId":"CHRF","manual":false}]

The actions should be disabled depending on the "manual" field in the JSON.

Edit: Following what I understood from Oleg's answer, I added that to my grid:

rowattr: function (rd) {
            if (rd.manual === false) { // verify that the testing is correct in your case
                return {"class": "not-editable-row"};
            }
        },

but it's still not working.

Alexis Dufrenoy
  • 11,784
  • 12
  • 82
  • 124

1 Answers1

1

If I understand you correctly then it would be enough to hide the column myac conditionally directly after the grid is created or after you set the value isReadOnly:

if (isReadOnly) {
    jQuery("#datagrid").jqGrid("hideCol", "myac");
}

Another common remark. I'd recommend you to examine the value from Default column on the page of the documentation. You will see that default value of edittype and sortable are "text" and true values which you use for all columns of colModel. Additionally you can consider to change default value of colModel items with respect of cmTemplate (see the old answer). One more remark: the values of index have to be the same as the value of name in case of usage loadonce: true. If one don't specify index then the value of name property will be used instead automatically. So you can reduce colModel from your code example to

colModel: [
    { name: 'myac', width: 80, fixed: true, sortable: false, resize: false,
        formatter: 'actions', formatoptions: { keys: true, editbutton: true },
        editable: false },
    { name: 'nom' },
    { name: 'geopostDepotNumber' },
    { name: 'iata' },
    { name: 'groupeId' },
    { name: 'siteRattachement' },
    { name: 'posteComptable' },
    { name: 'centreCout' },
    { name: 'description' }
],
cmTemplate: { editable: true }

Such code can be much more easy be read and to be maintained.

By the way if you know the value of isReadOnly before the grid is created you can use additionally

cmTemplate: { editable: !isReadOnly }

The last remark: you use sortorder: "asc" option which has default value. You can remove the option. You should verify whether sortname: 'title' is correct, because you don't have the column 'title'. I suppose that it's just a "cut and pasted" error.

UPDATED: If you need to disable editing of some rows you can do the following

rowattr: function (rowData) {
    if (rowData.manual === false) {
        return { "class": "not-editable-row" };
    }
},
loadComplete: function () {
    $(this).find("tr.not-editable-row")
        .find(".ui-inline-edit,.ui-inline-del")
        .hide();
}

The corresponding demo uses the value from the Closed column to disable editing. The resulting grid looks like on the picture below

enter image description here

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Actually, no, that's not what I want to do. Sorry if I wasn't clear enough. I want my icons to appear on some rows (when the boolean defined for each row is true) and not on others (when the boolean is false). – Alexis Dufrenoy Nov 27 '14 at 15:32
  • @AlexisDufrenoy: Sorry, but I still don't understand what you want. You wrote in the text of your question: "I would like to display the icons only when the boolean is true." What you want really? If you execute `jQuery("#datagrid").jqGrid("hideCol", "myac");` then the column which holds the Edit and Delete buttons disappears. Is it not exactly what you just wrote in your comment? I wrote you that it's very important to know **when** you set the value of "the boolean"? Is it known *before* the grid is created? Is it a part of server response with jqGrid data? ... – Oleg Nov 27 '14 at 15:38
  • @AlexisDufrenoy: If you need to make only some *specific* rows editable you can use `rowattr` to add `"not-editable-row"` class to the rows. So editing of the rows will be not possible. Look [the answer](http://stackoverflow.com/a/10531680/315935) where you need just replace `"myAltRowClass"` to `"not-editable-row"`. One more option are described [here](http://stackoverflow.com/a/12956484/315935). – Oleg Nov 27 '14 at 15:41
  • Yes, I get my boolean from my Tomcat server. I will attach a sample JSON to my question. And yes, I want to make some lines non-editable/deletable – Alexis Dufrenoy Nov 28 '14 at 13:29
  • @AlexisDufrenoy: And what boolean you mean from the JSON data? Is it `manual` filed associated with the row? Means the field of input data that the row is editable? – Oleg Nov 28 '14 at 13:35
  • Yes, that's exactly what I would like. – Alexis Dufrenoy Nov 28 '14 at 13:36
  • Ok, `rd` contains the data for each row of the grid. So if I add a hidden column containing the "manual" boolean, it's working: nothing happens when I click on the edit button of a line with manual=false. But I can't distinguish between editable lines and not editable ones. I should probably add another style. Do you know anything that could come handy? – Alexis Dufrenoy Nov 28 '14 at 13:54
  • 1
    @AlexisDufrenoy: See **UPDATED** part of my answer. – Oleg Nov 28 '14 at 14:13
  • By the way, why do I get the inline button? I would like form editing, actually... – Alexis Dufrenoy Nov 28 '14 at 16:10
  • 1
    @AlexisDufrenoy: If you want to use form editing instead of inline editing you should add `editformbutton: true` option to `formatoptions`. If you will need to specify and other options of form editing you should use `editOptions` property of `formatoptions`. For example `formatoptions: { editformbutton: true, editOptions: { closeAfterEdit: true }}` – Oleg Nov 28 '14 at 16:31
  • Sorry to bother you again, but when I click on the + button at the bottom, to add a row, the icons appear again. Do you know why? I'm using a form edit. – Alexis Dufrenoy Dec 01 '14 at 12:49
  • @AlexisDufrenoy: Sorry, but you should describe the problem more detailed. Which "+" button you use? Do you use Add of `inlineNav` or `navGrid`? Which icons appear again? In new added row or in all rows? Which options of inline editing of form editing you use? – Oleg Dec 01 '14 at 16:07