0

Hello everyone..!!!

I upgraded my jQgrid from version 4.8.2 to 4.9.2. Due to this I am facing problem to get selected value of dropdown column in custom func.

The scenario is, when I go to save record during Add/Edit operation, as per jQgrid work flow, the execution pointer first comes in custom_func: function (value, colName){}. The function parameter value should contain selected value of dropdown, but its gives me selected text.

I google this problem, but I didn't found any relevant solution. I hope someone here have faced same problem and might have found any possible solution.

Here is my jQgrid code,

var oGrid = $('#tbPOFields'), topPagerSelector = '#' + $.jgrid.jqID(oGrid[0].id) + "_toppager", lastSel;

    oGrid.jqGrid({
        url: sRelativePath + '/WSAjax.asmx/GetDataForGridInEdit',
        mtype: "POST",
        datatype: "json",
        ajaxGridOptions: { contentType: "application/json; charset=utf-8" },
        serializeGridData: function (data) {
            return JSON.stringify(data);
        },
        jsonReader: {
            root: "d.rows",
            page: "d.page",
            total: "d.total",
            records: "d.records"
        },
        colNames: ['ConsigneeId', 'Consignee'],
        colModel: [
            { name: 'ConsigneeId', index: 'ConsigneeId', hidden: true },
            { name: 'Consignee', index: 'Consignee', sortable: false, title: false, width: 42, editable: true, edittype: "select",
                editrules: {
                    required: true,
                    custom: true,
                    custom_func: function (value, colName) {
                        if ($('#tbItems_Consignee').length === 1) {
                            var selrow = $('#tbItems').jqGrid('getGridParam', 'selrow');
                            $('#tbItems').jqGrid('setCell', selrow, 'ConsigneeId', value);
                        }

                        if (g_bEditMode && value === g_sValueNONE && !g_bIsOrderType_Maintenance)
                            return [false, " is an invalid Consignee.<br/>Please select a valid Consignee before saving."];
                        else {
                            return [true, ""];
                        }
                    }
                },
                editoptions:
                {
                    value: eval('(' + g_oConsigneeList + ')'),
                    dataEvents: [
                        { type: 'keyup', fn: function (e) { $(e.target).trigger('change'); } },
                        {
                            type: 'change',
                            fn: function (e) {
                                if (!e.isTrigger) {
                                    var selrow = $('#tbItems').jqGrid('getGridParam', 'selrow');
                                    var ConsigneeId = $(this).val();
                                    $('#tbItems').jqGrid('setCell', selrow, 'ConsigneeId', ConsigneeId);
                                }
                            }
                        }]
                }
            }
        ],
        prmNames: { page: "pageIndex", rows: "pageSize", sort: "sortIndex", order: "sortDirection" },
        autowidth: true,
        postData: {
            filters: null,
            spName: 'GetPOFieldsDetailsList',
            paramXML: ''
        },
        width: 'auto',
        height: 'auto',
        rowNum: 1000,
        rowList: [1000],
        sortname: "",
        sortorder: "asc",
        page: 1,
        gridview: true,
        toppager: true,
        autoencode: true,
        ignoreCase: true,
        viewrecords: true,
        caption: 'Fields',
        editurl: 'clientArray',
        emptyrecords: "No records to view.",
        footerrow: true,
        onSelectRow: function (rowid) {
            if (rowid && rowid != lastSel) {
                if (typeof lastSel !== "undefined") {
                    $(this).jqGrid('restoreRow', lastSel);
                }
                lastSel = rowid;
            }
        }
    });
    oGrid.jqGrid('navGrid', topPagerSelector, { add: false, edit: false, search: false, refresh: false }, {}, {}, {});
    oGrid.jqGrid('inlineNav', topPagerSelector, {
        addParams: {
            addRowParams: {
                oneditfunc: function (rowid) {
                    updateJqGridButtonState($(this), jqGridMode.Add);
                },
                aftersavefunc: function (rowid, response) {
                    g_bIsDataChanged = true;
                    SavePOFieldsDetails($(this), rowid);
                    updateJqGridButtonState($(this), jqGridMode.None);
                },
                afterrestorefunc: function () {
                    updateJqGridButtonState($(this), jqGridMode.None);
                }
            }
        },
        editParams: myFieldsEditParams
    });  
Oleg
  • 220,925
  • 34
  • 403
  • 798
Rahul More
  • 615
  • 3
  • 13
  • 41
  • different people, different issues......Would you mind to post the relevant code too? Your question is not helping us to post answers. – Jai Sep 02 '15 at 06:54
  • @Jai: Sure.. Give me a minute. – Rahul More Sep 02 '15 at 07:02
  • If I correctly understand you then you use [free jqGrid](https://github.com/free-jqgrid/jqGrid) 4.9.2. You use `editrules: {custom: true,custom_func: function (value, colName, iCol) {...}}` in the column which have `formatter: "select", edittype: "select"`. Options of the select used for editing have different `value` and text. I tried to reproduce the problem, but I had correct **value** as the first parameter of `custom_func`. So I could not reproduce the problem. Could you post the code/the demo which you used? – Oleg Sep 02 '15 at 07:14
  • @Jai: I have pasted my jQgrid code. Kindly refer it and let me know. – Rahul More Sep 02 '15 at 07:14
  • Could you post one or two row of the test data which allows to reproduce the problem. The value for `g_oConsigneeList` is required too. – Oleg Sep 02 '15 at 07:16
  • @oleg: Thank you sir for reading my question and to take interest in it. Just give me a minute, I will post it right now. – Rahul More Sep 02 '15 at 07:23
  • @Oleg: Here it is the value of `g_oConsigneeList` and the value is in JSON format. `"{"NONE":"--NONE--","b92e41b7-1ca2-4eca-a5e6-0d2ce9d1bd58":"M/s Harmony Pvt Ltd","2fcb117c-c943-46d7-9c73-d1079c17c580":"M/s Harmony"}"`. – Rahul More Sep 02 '15 at 07:31
  • **Could you post test input data for the grid? The response with 1-2 rows of data returned from `url: sRelativePath + '/WSAjax.asmx/GetDataForGridInEdit'` would be enough**. I suppose that you should use `formatter: "select"` and to fill the grid with ids (like `"b92e41b7-1ca2-4eca-a5e6-0d2ce9d1bd58"`, `"2fcb117c-c943-46d7-9c73-d1079c17c580"`), but you fill the grid with texts instead (like `"M/s Harmony"`) and you don't use `formatter: "select"`. – Oleg Sep 02 '15 at 07:37
  • @oleg: Actually sir, I am filling grid with Ids only. – Rahul More Sep 02 '15 at 08:17
  • 1
    [The demo](http://www.ok-soft-gmbh.com/jqGrid/OK/Rahul.htm) fill the column `ship_via` with ids too (see `"TN"` and `"FE"`) in the input data. The data will be displayed as `"TNT"` and `"FedEx"` corresponds to `editoptions.value` and `formatter: "select"`. You can see that the user see the select with texts during editing (both inline of form editing), but the vales `"TN"` and `"FE"` will be forwarded to the `custom_func` and finally the values will be saved in local grid data, but displayed as texts `"TNT"` and `"FedEx"` of cause. So all works OK if I use **`formatter: "select"`**. – Oleg Sep 02 '15 at 08:43
  • @oleg: You are absolutely right sir. because of absence of formatter property, I was facing the problem. Now problem is solved. – Rahul More Sep 02 '15 at 09:21
  • @Oleg: I accepted the answer. Thank you sir to refresh my jQgrid concept. Keep supporting like this. Have great day. take care – Rahul More Sep 02 '15 at 09:33
  • If you get time, please have look on my following question too. [link] http://stackoverflow.com/questions/31400537/toolbar-search-local-data-retain-value-in-cell-even-after-searching [link]. I would feel happy if you provide some useful suggestion on this question. – Rahul More Sep 02 '15 at 09:41
  • @Rahul: You are welcome! I will read your other question later and will write my answer or at least comments. – Oleg Sep 02 '15 at 09:56

1 Answers1

2

There are two main scenario used with edittype: "select".

The first one: one want allows the user to choose values from the list. For example, the user sees the grid which some column which have values like "send", "closed", "pending", "paid". No other values should be allowed for the user to be chosen. In the case one can use the options like edittype: "select", editoptions: { value: "send:send;closed:closed;pending:pending;paid:paid" }. It's important to understand, that one uses the values "send", "closed", "pending", "paid" to fill the grid and the same values will be saved in the grid during editing or to be send to the server if editurl not equal "clientArray".

The second scenario: one want to work with ids, but to display some readable texts for the user. For example one hold value 3, 5, 11, 7 internally instead of texts "send", "closed", "pending", "paid". Nevertheless one want to display the value (3, 5, 11, 7) as readable texts for the user. The corresponding options for the column will be formatter: "select", edittype: "select", editoptions: { value: "3:send;5:closed;11:pending;7:paid" }. It's important that one should fill jqGrid with input data having the value 3, 5, 11, 7 instead of the texts "send", "closed", "pending", "paid". In the scenario jqGrid hold values in the cells on the columns, but displays the values as texts for the user. During editing the user have select with the options which looks like <option value="3">send</option>. So the user sees the texts during editing in the same ways like in the grid. After the end of editing jqGrid saves values (the old value 3, 5, 11, 7 could be replaced to the new numeric value from the list). The scenario work in the same way in case of local editing (editurl: "clientArray") and in case of saving the data to the server.

I wrote above only about editing and not about the custom_func callback (specified by usage of the options like editrules: {custom: true, custom_func: function (value, colName, iCol) {...}}). It works here corresponds to the same scenario. If one uses formatter: "select" then the values will be saved in the column and so the values needed be validated. So the values will be forwarded to custom_func as the first parameter. On the other side if one uses the first scenario (without formatter: "select") then the texts will be saved in grid and the texts needed be validated and the texts will be forwarded as the first parameter of custom_func.

Thus you should verify which one from above two scenario you need to implement. If you really need to have values in custom_func then you should use

formatter: "select"

You should don't forget that you should use values for filling of the grid too. In the case only the values will be in the data of the column and the texts will be just displayed to the user to make editing more comfortable of to implement some other requirements, like localization for example. Using formatter: "select" you could for example hold texts "send", "closed", "pending", "paid" internally in the database, but to display the translation of the texts based on the user locale. The user will see and edit only translated texts, but you will save the original values in English.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • You are absolutely right sir. because of absence of formatter property, I was facing the problem. Now problem is solved. – Rahul More Sep 02 '15 at 09:29
  • If you get time, please have look on my following question too. [link] http://stackoverflow.com/questions/31400537/toolbar-search-local-data-retain-value-in-cell-even-after-searching [link]. I would feel happy if you provide some useful suggestion on this question. – Rahul More Sep 02 '15 at 09:37
  • @ oleg: Hello sir, please provide guidance for my this question http://stackoverflow.com/questions/32453474/free-jqgrid-4-9-2-on-delete-confirmation-box-showing-id-of-dropdown-instead-tex – Rahul More Sep 08 '15 at 09:12