1

I am using free jqgrid version 4.13.2 and i have a grid with 2 select columns and a custom column with multiple checkboxes per cell.

One of the select columns has the values hardcoded and it works perfectly. The other one creates the select string dynamicly based on another grids rows and as far as i can see, it has nothing different from the working select.

When i am in edit, they are displayed correctly but after the row gets saved the dynamicly filled select displays the numeric value and so does the checkbox column.

My question is, is there something wrong with the generated select string? What can i do to get them working?

enter image description here enter image description here

 function getSpecificCell(grid, col){
        var id = grid.find('tr[aria-selected="true"]').attr('id');
        return grid.jqGrid('getRowData',id)[col];
    }
 function getAdresseSelectValues(){
     var adrese = $("#adreseGrid").getGridParam('data'),
         adreseDeReturnat="";
     for(var i=0;i<adrese.length;i++){
         adreseDeReturnat += adrese[i].adresseId + ":Str. " +  adrese[i].streetId +" Nr. " + adrese[i].streetNumber;
         if(adrese[i].building){
             adreseDeReturnat += "Bl. " + adrese[i].building;
         }
         if(adrese[i].floor){
             adreseDeReturnat += " Etaj "+ adrese[i].floor;
         }
         if(adrese[i].apartament){
             adreseDeReturnat += " Ap. " + adrese[i].apartament;
         }
         if(adrese[i].interPhone){
             adreseDeReturnat += " Interfon: " + adrese[i].interPhone;
         }
         adreseDeReturnat += ";";

     }
     adreseDeReturnat = adreseDeReturnat.substring(0,adreseDeReturnat.length-1);
     return adreseDeReturnat;
 }

var custOrdGrid = $("#customersOrdersGrid"),
    custOrdPager = "#customersOrdersPager",
    restURL = "/LicentaREST/rest/",
    validationCount={},
    getOrderDDLs = {};
getOrderDDLs = ajaxCall(restURL+"ddl/getOrderDDL");
var pizzaValues = getOrderDDLs.pizzaValues,
    pasteValues = getOrderDDLs.hamburgerValues,
    hamburgerValues = getOrderDDLs.pasteValues,
    drinksValues = getOrderDDLs.drinksValues,
    desertValues = getOrderDDLs.desertValues;
    custOrdGrid.jqGrid({
        datatype: "local",
        mtype: "POST",
        url: restURL + "orders/getCustomerOrders",
        editurl: restURL +"orders/createUpdateOrder",
        colModel: [
             { name: "orderId", index:"orderId",label:"",key:true,hidden:true},
             { name: "custId", index:"custId",label:"",hidden:true},
             { name: 'createdBy', index: 'createdBy', label:'Creat de', width: 90,editable: false,hidden:true },
             { name: 'orderedBy', index: 'orderedBy',search:false, label:'Client', width: 50, editable: false},
             { name: 'phoneNumber', index: 'phoneNumber',label:'Telefon',editable: false,width: 50},
             { name: 'pizza', index: 'pizza',editable: true,search:false,edittype:"custom",
                 editrules: {custom: true, custom_func: function(value,colname){
                    if(value=="" || value==null){
                        validationCount[colname] = false;
                    }
                    else{
                        validationCount[colname] = true;
                    }
                     return [true, ""];
                }},
                 editoptions:{custom_element:multiCheckElem, custom_value:multiCheckVal,list:pizzaValues,dataEvents:[
                                {type: 'change', data:{ i: 7 }, fn: function(e) { onChangeFunc(e,custOrdGrid,getOrderDDLs);}}]}, label:'Pizza', width: 86 },
             { name: 'paste', index: 'paste',editable: true,search:false,edittype:"custom",
                                    editrules: {custom: true, custom_func: function(value,colname){
                                        if(value=="" || value==null){
                                            validationCount[colname] = false;
                                        }
                                        else{
                                            validationCount[colname] = true;
                                        }
                                         return [true, ""];
                                    }},
                                    editoptions:{custom_element:multiCheckElem, custom_value:multiCheckVal,list:pasteValues,dataEvents:[
                                {type: 'change', data:{ i: 7 }, fn: function(e) { onChangeFunc(e,custOrdGrid,getOrderDDLs);}}]}, label:'Paste', width: 80 },
             { name: 'hamburger', index: 'hamburger',editable: true,search:false,edittype:"custom",
                                    editrules: {custom: true, custom_func: function(value,colname){
                                        if(value=="" || value==null){
                                            validationCount[colname] = false;
                                        }
                                        else{
                                            validationCount[colname] = true;
                                        }
                                         return [true, ""];
                                    }},
                                    editoptions:{custom_element:multiCheckElem, custom_value:multiCheckVal,list:hamburgerValues,dataEvents:[
                                {type: 'change', data:{ i: 7 }, fn: function(e) { onChangeFunc(e,custOrdGrid,getOrderDDLs);}}]}, label:'Hamburger', width: 80 },
             { name: 'drinks', index: 'drinks',search:false,editable: true,edittype:"custom",
                                    editrules: {custom: true, custom_func: function(value,colname){
                                        if(value=="" || value==null){
                                            validationCount[colname] = false;
                                        }
                                        else{
                                            validationCount[colname] = true;
                                        }
                                         return [true, ""];
                                    }},
                                    editoptions:{custom_element:multiCheckElem, custom_value:multiCheckVal,list:drinksValues,dataEvents:[
                                {type: 'change', data:{ i: 7 }, fn: function(e) { onChangeFunc(e,custOrdGrid,getOrderDDLs);}}]}, label:'Bauturi', width: 80 },
             { name: 'desert', index: 'desert',search:false,editable: true,edittype:"custom",
                                    editrules: {custom: true, custom_func: function(value,colname){
                                        if(value=="" || value==null){
                                            validationCount[colname] = false;
                                        }
                                        else{
                                            validationCount[colname] = true;
                                        }
                                        if(validationCount.Pizza === false && validationCount.Paste === false && validationCount.Hamburger === false && validationCount.Bauturi === false && validationCount.Desert === false ){
                                            return [false,'Cel putin un produs trebuie selectat'];
                                        }
                                         return [true, ""];
                                    }},
                                    editoptions:{custom_element:multiCheckElem, custom_value:multiCheckVal,list:desertValues,dataEvents:[
                                {type: 'change', data:{ i: 7 }, fn: function(e) { onChangeFunc(e,custOrdGrid,getOrderDDLs);}}]}, label:'Desert', width: 80 },
             { name: 'notes', index: 'notes',label:'Nota',edittype:'textarea', editoptions: { rows: "12", cols: "20"}, width: 80,editable: true},
             { name: 'total', index: 'total',label:'Total', width: 40,editable: false,align:'center' },
             { name: 'orderedAt', index:'orderedAt', label:'Data', width: 80,formatter: 'date', formatoptions: {newformat:"d/m/Y"},search:false},
             { name: 'adresa', index:'adresa',label:'Adresa',formatter:'select',stype:'select',searchoptions: {sopt: ['eq','ne'],value: getAdresseSelectValues()},editable:true,edittype:'select',editoptions:{value: getAdresseSelectValues}},
             { name: 'orderStatus', index: 'orderStatus', label:'Status',stype:'select',edittype:'select', editoptions:{value:"1:Comanda Primita;2:Comanda in livrare;3:Comanda anulata de client;4:Comanda refuzata de client;5:Comanda Finalizata"},formatter:'select', width: 90,editable: true}
        ],
        pager: custOrdPager,
        rowNum: 10,
        beforeProcessing: function(data){
            if(data && data.length>0){
                var test = getAdresseSelectValues();
                console.log(test);
                for(var i=0;i<data.length;i++){

                    data[i].orderedAt = new Date(data[i].orderedAt);
                    data[i].total += " lei";
                }
            }
            console.log(getAdresseSelectValues());
        },
        ajaxGridOptions: {contentType: 'application/json; charset=utf-8'},
        serializeGridData: function (postData) {
              postData = parseInt($("#customersGrid").find('tr[aria-selected="true"]').attr('id'));
              return JSON.stringify(postData);
          },
        rowList: [10, 50, 100, 250,500],
        sortname: 'id',
        loadonce: true,
        rownumbers: true,
        forceClientSorting: true,
        inlineEditing: {
            ajaxSaveOptions: { contentType: "application/json" },
            serializeSaveData: function (postData) {
                if(postData.oper == "add"){
                    postData.createdBy = $("#userIdSession").val();
                    postData.custId = $("#customersGrid").find("tr[aria-selected='true']").attr("id");
                    postData.createdAt = new Date();
                }
                if(postData.oper == "edit"){
                    var data =custOrdGrid.getRowData(postData.orderId);
                    postData.createdBy = data.createdBy;
                    postData.custId = data.custId;
                }
                postData.adresa=parseInt(postData.adresa.replace('"',""),10);
                postData.total = parseInt(custOrdGrid.jqGrid("getCell",postData.orderId,"total"),10);
                return JSON.stringify(postData);
            }
        },
        height: 220,
        iconSet: "fontAwesome",
        sortorder: 'desc',
        viewrecords: true,
        caption: 'Customer Orders',
        autowidth: true

 });
IvanSt
  • 360
  • 4
  • 17
  • It seems you have a typing bug: `editoptions:{value: getAdresseSelectValues}` should be fixed to `editoptions:{value: getAdresseSelectValues()}` like `searchoptions: {sopt: ['eq','ne'],value: getAdresseSelectValues()}`, which you already use. Additionally consider to use `height: "auto"` instead of `height: 220`. You can use `maxHeight: 220` **additionally** if you want to restrict the max height of the grid. – Oleg May 29 '16 at 20:10
  • Good evening Oleg, thank you for your reply. It seems that if i change editoptions from getAdresseSelectValues to getAdresseSelectValues(), the dropdownlist doesnt load at all, not even in edit mode for some reason. I will implement the height suggestion right now, thank you. – IvanSt May 29 '16 at 20:19
  • You are welcome! It's difficult to understand which value you want to assign to `editoptions.value`. An example of `getAdresseSelectValues()` could be helpful. I'd recommend you to remove all `index` properties from `colModel` and to use more intensive column templates (see [the old answer](http://stackoverflow.com/a/6047856/315935) for more details). It will allows to reduce the size of your code and to remove code dupplicates. – Oleg May 29 '16 at 20:28
  • You can remove `searchoptions.value` in many cases if you use `editoptions.value`. See [here](https://github.com/free-jqgrid/jqGrid/blob/master/README4.13.1.md#main-new-features-and-improvements-implemented-in-the-version-4131). The usage of 4.13.3 would be better as 4.13.2 too because it contains [some fixes](https://github.com/free-jqgrid/jqGrid/blob/master/README.md#below-one-can-see-the-full-list-of-changes-in-the-version-4133-compared-with-4132) – Oleg May 29 '16 at 20:32
  • Thanks for the suggestion, i will make the changes now. Here is a example of what getAdresseSelectValues() returns and what comes from DB for the "adresa" column. `26:Str. sadasdsd Nr. 34Bl. asdas Etaj asda Ap. asda;27:Str. asdasdada Nr. 123Bl. 123 Etaj 123 Ap. 123` and from db: 26 – IvanSt May 29 '16 at 20:37
  • You use `$("#adreseGrid").getGridParam('data')` inside of `getAdresseSelectValues()`. It seems to me the main problem, which you have. You should set `editoptions.value` with respect of `setColProp` **inside of `beforeProcessing`** to fix your main problem. See [the answer](http://stackoverflow.com/a/17410568/315935) or better [this one](http://stackoverflow.com/a/19427444/315935) – Oleg May 29 '16 at 20:46
  • Oleg, thank you so much! That fixed my problem. For the multiple checkboxes columns i will make a custom formatter that should fix the problem there also. Feel free to post a answer so i can accept it. Thanks alot! – IvanSt May 29 '16 at 20:59
  • A quick question, if i were to use this for multiple checkboxes in a single column, would i be able to display the selected values after save? Instead of "3 selected" how it appears in the demo you posted there. [post](http://stackoverflow.com/questions/10847644/is-there-anyway-to-include-a-multiselect-combobox-in-a-jqgrid/10851429#10851429) – IvanSt May 29 '16 at 21:22
  • You are welcome! I'll post my answer later. About "3 selected" message: `multiselect` supports the option, which allows to specify after how many selected items it displays ""3 selected"". You should increase the value of `selectedList: 2` option. – Oleg May 30 '16 at 04:58

1 Answers1

3

You use datatype: "local", but mtype: "POST", url: restURL + "orders/getCustomerOrders" and beforeProcessing options/callbacks, which shows that you do load the data from the server. The column adresa have the following properties

{
    name: 'adresa', index:'adresa',label:'Adresa',
    formatter:'select',
    stype:'select',searchoptions: {sopt: ['eq','ne'],value: getAdresseSelectValues()},
    editable:true,edittype:'select',editoptions:{value: getAdresseSelectValues()}
}

with formatter:'select'. It means that the data for adresa column return from the server are numbers like 26 and 27 on your first picture and you want to display there as texts Str. sadasdsd Nr. 34Bl. asdas Etaj asda Ap. asda and Str. asdasdada Nr. 123Bl. 123 Etaj 123 Ap. 123 because of usage editoptions.value defined as

"26:Str. sadasdsd Nr. 34Bl. asdas Etaj asda Ap. asda;27:Str. asdasdada Nr. 123Bl. 123 Etaj 123 Ap. 123"

The problem only is: you want to load the data from the server too. The data will be filled in the grid using formatter:'select'. Thus the value editoptions.value of the column adresa have to be set/modified before the formatter start working.

I suggest you to use beforeProcessing callback, where you process the server response and set/modify editoptions.value of the column adresa. You can use setColProp, for example, for setting editoptions.value. See the answer and this one for more implementation details.

I'd recommend you to use column template additionally (see the old answer) to reduce code duplicates in your code. It will simplify later modification/maintain of your code.

Some additional advices: you use hidden column name: "orderId" with key:true property. The value will be saved in <td> of the column and as the value of id attribute of the row (<tr>). I suggest you to remove the column and to use jsonReader: { id: "orderId" } instead (or localReader: {id: "orderId"} if you load the data not from the server). It will inform jqGrid to assign rowids based on the value of orderId property of input data. Additionally jqGrid will use orderId as the name of rowid during editing (set to editurl) instead of default name id.

You can consider to remove other hidden columns too (custId and createdBy). You need just add the option additionalProperties: ["orderId", "custId", "createdBy"] to inform jqGrid to read the properties from input data. The properties will be seen in custom formatter, rowattr, cellattr and be saved in the local data (accessible by getLocalRow method). In other words, the data will be save in JavaScript objects, but not in the DOM. It makes HTML/DOM cleaner and improve the performance of the page.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks for the help! I managed to get it working and ill apply the modifications you suggested also. I am curious, is there a documentation page for free-jqGrid similar to the one from trirand website aimed at the original jqGrid? The one from gifthub only shows the modifications made in each version, if i am not mistaken, right? – IvanSt May 30 '16 at 10:34
  • @IvanSt: You are welcome! I started writing the documentation [here](http://free-jqgrid.github.io/getting-started/index.html), but it contains not so many information. It's difficult to spend many time to the product which I provide to free. :-) I plan to extend the information provided on http://free-jqgrid.github.io/ – Oleg May 30 '16 at 10:49
  • Oh, i totaly understand. Thanks alot for the link. – IvanSt May 30 '16 at 10:52