0

I have been struggling with some issues for a while now on jquery grid.

The first one is ... if my current row is currently being edited, if clicked outside of row, i dont want it to cancel my editing and select the new row. My current code is:

beforeSelectRow : function(id){ 
    if($('tr#'+id).is('[editable="1"]') == '1'){

    return false;
    }

    else if ($('tr#'+id).is('editable') != '1')  {

        return true;            
    }
}

How can i make it to work?

Also, i have a column with checkboxes [ department head column with yes or no values]. If i have a single checked checkbox, i want it to pop a alert when i click a new box and not leave edit.

At the moment i managed to make a little code for aftersavefunc, that will alert and change the Yes to a No if another box is checked but its not what i need as it acts after the row has been saved.

var checkboxCol = grid.getCol('HeadDepartament');
var numberOfCheckedBoxes=[];

for(k=0;k<ids.length;k++){ 
    if(checkboxCol[k] == 'Yes'){
        numberOfCheckedBoxes.push(checkboxCol[k]);
        if (numberOfCheckedBoxes.length >1){
            grid.setCell(id, 'sefDepartament', 'No');
            alert('Please deselect the other checked box first');
            numberOfCheckedBoxes = 0;
        }
    }
}

Update [ by oleg's request] :

$(function(){

    var mydata = [
    {id:0, nume:'Razvan', prenume:'Ciprian',username:'Razvan.Ciprian',email: 'Razvan.Ciprian@test.com','sefDepartament':'No',position: 'position 1',joinYear:'17-11-2015'},
    {id:1, nume:'Jijel', prenume:'Codru',username:'Jijel.Codru',email: 'Jijel.Codru@test.com','sefDepartament':'No',position: 'position 5',joinYear:'18-11-2015'},
    {id:2, nume:'Ionica', prenume:'Drumlung',username:'Ionica.Drumlung',email: 'Ionica.Drumlung@test.com','sefDepartament':'Yes',position: 'position 2',joinYear:'19-11-2015'},
    {id:3, nume:'Rodent', prenume:'Dumitrache',username:'Rodent.Dumitrache',email: 'Rodent.Dumitrache@test.com','sefDepartament':'No',position: 'position 4',joinYear:'20-11-2015'}];

    var grid = $('#grid');
    var newId=0;
    var newIdGeneratorFunction = function(){        
    return   newId++  ;

    };


    var checkboxFormatFunc = function(cellvalue, options ,rowObject){

        if(cellvalue == 'Yes')
        return 'Yes';
        else return 'No';

    };


    var  afterSaveFunction =   function(id){

                var checkboxCol = grid.getCol('sefDepartament');
                var ids = grid.jqGrid('getDataIDs');
                var k;
                var firstFoundId ;
                var numberOfCheckedBoxes=[];
                // Number of Checked Boxes Function begins;
                for(k=0;k<ids.length;k++){ 
                    if(checkboxCol[k] == 'Yes'){
                        numberOfCheckedBoxes.push(checkboxCol[k]);
                        if (numberOfCheckedBoxes.length >1){
                            grid.setCell(id, 'sefDepartament', 'No');
                            alert('Please deselect the other checked box first');
                            numberOfCheckedBoxes = 0;
                        }
                    }
                    };                          
                // Number of Checked Boxes Function ends;
            };




    var colModelSettings = [

        {name:'id', label:'id',key: true,hidden: true,formatter: newIdGeneratorFunction , width:10,sorttype:'number',editable: false},      
        {name:'nume',label:'Nume',width:90, align: 'center',editable:true,searchoptions: {sopt: ['eq','bw','ew','cn']}, editrules:{required:true}, editoptions: {defaultValue: ' '},formatter: 'text'},
        {name:'prenume',label:'Prenume',width:100,editable:true,searchoptions: {sopt: ['eq','bw','ew','cn']},align: 'center',editrules:{required:true},editoptions: {defaultValue: ' '},formatter: 'text'},
        {name:'username',label:'Username',searchoptions: {sopt: ['eq','bw','ew','cn']},width:125,align: 'center'  },
        {name:'email',label:'Email',width:135,searchoptions: {sopt: ['eq','bw','ew','cn']},align: 'center'},
        {name:'sefDepartament',label:'Sef Departament',width:90,editable:true,align: 'center', stype:"select", searchoptions:{sopt: ['eq','ne'],value: "Yes:Yes;No:No"},formatter: checkboxFormatFunc,edittype:'checkbox',editoptions: { value:'Yes:No', defaultValue: 'No' }},

        {name:'position',label:'Position',editable:true,stype: 'select',formatter: 'select',searchoptions: {sopt: ['eq','ne'],value: ' : ;position 1:position 1;position 2:position 2;position 3:position 3;position 4:position 4;position 5:position 5'},
        align: 'center',edittype:'select',editoptions:{defaultvalue: 'P0: ',value: ' : ;position 1:position 1;position 2:position 2;position 3:position 3;position 4:position 4;position 5:position 5'},width: 75},

        {name:'joinYear',label:'joinYear',editable:true,sorttype:'date',stype: 'datepicker',align: 'center',width: 70,
        searchoptions:{sopt: ['eq','ne'],dataInit: function (elem) {
            $(elem).datepicker(
            { dateFormat:'dd-mm-yy',
            showButtonPanel: true }
        )}},

        editoptions:{size:20,defaultValue: ' ',
        dataInit: function (elem) {
            $(elem).datepicker(
            { showButtonPanel: true,
              dateFormat:'dd-mm-yy',}
        )}}},

        {name:'experience', label:'Experience',searchoptions:{sopt: ['eq','bw','ew','cn']}, editable:false, editoptions:{defaultValue: ' '},align: 'center',width: 60},

        {name:'actiuni',label: 'Actiuni',formatter: 'actions', formatoptions: {onEdit: onEditActionFunction,afterSave:afterSaveFunction},editable: false,sortable: false,search: false,width: 60 }

        ];

    grid.jqGrid({

        pager: '#pager', 
        data: mydata ,
        datatype: 'local',
        editurl:'clientArray',
        height: 250,    
        loadonce: true, 
        viewrecords: true,
        scrollOffset:0,
        sortorder: 'asc', 
        caption:'Employee List' ,
        autowidth: true,
        colModel: colModelSettings,


        beforeSelectRow : function(id){ 


                            if($('tr#'+id).is('[editable="1"]') == '1'){

                            return false;
                            }

                            else if ($('tr#'+id).is('editable') != '1')  {

                                return true;            
                            }


                        }
});

    grid.jqGrid('navGrid', '#pager', {edit:false, add:false, save:false, cancel:false, search:true, searchtext: 'Search', refresh:true, del:false});
    grid.jqGrid('inlineNav','#pager',
    {

        edit:true,
        edittext: 'Edit',
        save:true,
        savetext: 'Save',
        cancel: false,
        add:true,
        cancel: true,
        canceltext: 'Cancel',
        cancelicon: 'ui-icon-cancel',
        addicon:'ui-icon-plus',
        addtext: 'Add New Row',
        addedrow: 'last',
        addParams: {
            position: 'last',
            addRowParams: {
            aftersavefunc : afterSaveFunction,
            keys: true,
            }

                },

        aftersavefunc : afterSaveFunction,

        },

    });
});

Update 2: I managed to block selecting a different row and leaving edit while editing the current row with this function:

beforeSelectRow : function(id){ 
                            var idsArray = grid.jqGrid('getDataIDs');
                            var i;
                            for(i=0;i<idsArray.length;i++){
                                if($('#'+idsArray[i]).is('[editable="1"]') ){
                                grid.editRow(idsArray[i],true);
                                return false;
                                }}; 
                            return true;}
IvanSt
  • 360
  • 4
  • 17
  • Could you include more full code which you use? It's important to understand which editing mode you use and how you start it. You write "i don't want it to cancel my editing and select the new row", but it's unclear why it should be canceled. It should do another part of your code which you not posted. The requirement with chechboxes is not clear enough for me. You use `getCol` to get *the whole column* and set someone. I didn't understand what exactly you try to implement. In any way you should always write which version of jqGrid you use and which fork (free jqGrid, Guriddo jqGrid JS , ...) – Oleg Dec 01 '15 at 19:52
  • Thanks for the feedback Oleg. I will add a bigger piece of the code in just a moment. – IvanSt Dec 01 '15 at 19:59
  • Which version of jqGrid you use? You use both `inlineNav` and `formatter: 'actions'` which have many small problems in old versions of jqGrid. – Oleg Dec 01 '15 at 20:10
  • I use the latest version, downloaded from their website. 4.8.0 to be exact. I use both of those because that was my requirment, i also have to figure out a fix for those 2 as well but i like to believe i can handle that on my own. I didnt want to post all of my issues as i need to learn something too but those 2 have been a pain for a while now. – IvanSt Dec 01 '15 at 20:20
  • The code miss `onEditActionFunction` function. The 4.8.0 is not the latest version. The last version of "jqGrid" was 4.7. After that there are two alternative forks: [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) (last version 5.0.1) developed by Tony and [free jqGrid](https://github.com/free-jqgrid/jqGrid) (last version 4.11.0) developed by me. See [the post](http://www.trirand.com/blog/?p=1438) which was the start of changing of license agreement and the end of old jqGrid. – Oleg Dec 01 '15 at 20:31
  • I inserted your code in http://jsfiddle.net/OlegKi/cdp1szd8/, (without `onEditActionFunction`). Free jqGrid have a lot of improvements which allows to simplify and to reduce your code. See [the wiki article](https://github.com/free-jqgrid/jqGrid/wiki/New-style-of-usage-options-of-internal-methods) for the beginning. **Can you modify http://jsfiddle.net/OlegKi/cdp1szd8/ so that it works like in your project and then to describe test case (step by step instruction to reproduce the problem)?** – Oleg Dec 01 '15 at 20:32
  • Thanks for the updates. I am quite the noob in jqgrid and i was not aware of the changes. "Especially newcomer have problem to understand where exactly the options should be inserted." I couldnt agree more. That was one of my biggest obstacles. I am not sure what you are asking me though. Should i try to implement the checkbox function check in the onEditActionFunction? I noticed the function doesnt work anymore in the JSFiddle but i couldnt figure out why. – IvanSt Dec 01 '15 at 21:00
  • The demo http://jsfiddle.net/OlegKi/cdp1szd8/ contains the code which you posted, I just commented `onEditActionFunction` to make the code executable. The code starts editing, but it not really work and I'm not sure what exactly you want to implement. I suddested you to modify the code of my jsfiddle.net, save it and to most me the URL of the modified code. You can look at the source code of [the demo](http://www.ok-soft-gmbh.com/jqGrid/OK/navButtons1-fa4-.htm) which is simple enough and which demonstrate the usage of `inlineNav` and `formatter: "actions"` in one grid. – Oleg Dec 01 '15 at 21:35
  • Hey! I fixed the inlineNav and formatter:"actions" interaction by simply using the latest version of free jqGrid. Also, i fixed the problem i had with NOT leaving editing when clicking another row by using a function that i will edit my main post to show it. My question is now.. is it possible to parse a entire column for all its values within custom formatter? My goal now is to prevent someone from selecting a checkbox, if another one is already checked. My current function does it but only after save. I need it to be on checkbox click. – IvanSt Dec 02 '15 at 10:49
  • Why you use `multiselect: true` if you want allow the user to select only one row? In general you can implement any scenario of selection by usage correct `beforeSelectRow`. You need just return false. Custom formatter have of case access to all row (see the 3-d parameter), many other callbacks like `rowattr` and `cellattr` too. All your questions now seems be *new question* and the original question seems be solved now. I'll post short answer with the information from previous comments. You can close the problem by accepting the answer. – Oleg Dec 02 '15 at 11:02
  • You can consider to use `hasMultiselectCheckBox` callback or `multiPageSelection: true` option which exist in free jqGrid. See the demo from [the answer](http://stackoverflow.com/a/33021115/315935). – Oleg Dec 02 '15 at 11:04
  • Oki, thanks for the help. – IvanSt Dec 02 '15 at 11:07

1 Answers1

0

I recommend you to try to use free jqGrid. It's the fork of jqGrid, which I develop since almost one year. It allows to simplify your code dramatically using the inline editing of inlineNav and formatter: "actions" which you use. All the options and callbacks of inline editing you can place directly in jqGrid options inside of inlineEditing option. I described the idea in the wiki article. Look at the demo which demonstrates it.

To customize selection or rows you can use beforeSelectRow which just returns false if you need to prevent selection. It's the correct way.

Oleg
  • 220,925
  • 34
  • 403
  • 798