2

I want to load my server data in the DropDown of my jqgrid. My code,

UPDATED CODE:

 public ActionResult GetUnit()
    {
        List<UnitModel> getUnitValues = new List<UnitModel>();
        //ToDo db code
        Dictionary<int, string> unitValues = new Dictionary<int, string>();
        unitValues = getUnitValues.ToDictionary(x => x.UnitID, x => x.UnitDesc);
        unitValues.Add(4, "Unit2/3");
        unitValues.Add(1, "Unit1");
        unitValues.Add(2, "Unit2");
        unitValues.Add(3, "Unit3");
        return Json(unitValues, JsonRequestBehavior.AllowGet);
    }

My jqgrid:

     colModel: [...
      {
            name: 'UnitID', index: 'UnitID', editable: true, edittype: 'select', width: "200",
            formatter: 'select', editoptions: { value: unitslist},
            editrules: { custom: true, custom_func: dupicateRecordValidation
            }
        },
...],

       beforeProcessing: function () {
                    $.ajax({
                    url: '/Home/GetUnit/',
                    dataType: 'json',
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {
                        $.map(data, function (value, key) {
                            unitsList += '"' + value + '"' + ':' + '"' + key + '"' + ',';
                        });
                        unitsList += '}';
                        alert(unitsList);
                    }
                });
            },

But, this isn't working. The jqgrid DropDown column loaded with empty cell. Am I missing something? Is this the correct way to do? Please suggest if any alternate way to load the dropdown of jqgrid with server data with default value of that row being selected. Thanks.

Note:I'm using Jquery jqgrid v4.4.4 Visual Studio

Kavitha
  • 147
  • 8

1 Answers1

2

First of all, it's important to understand when you should use formatter: 'select', which you currently use. It's required if you want to fill the grid with id information in UnitID, but you need to display the text, which correspond the ids. For example, the JSON data, which you get from the server could contain the property language, with the content "de", "en", "fr" and so on, but you want to display in the column "German" instead of "de", "English" instead of "en" and "French" instead of "fr". In the case you should define

formatter: 'select', editoptions: { value: 'de:German;en:English;fr:French' },
editable: true, edittype: 'select'

If you really need to use formatter: 'select', and you need to load the editoptions.value via Ajax from the server, then the editoptions.value have to be set before the main data of the grid returned from url will be processed. In the case, I would recommend you to extend the standard data returned from url with the data required for the editoptions.value. One can use beforeProcessing callback (which is supported even in the retro version 4.4.4, which you use) and to set editoptions.value dynamically with respect of setColProp method. See the answer for more details and the code example.

If you don't need to use formatter: 'select' (if ids and the values, used in select, are the same), then you can change the format of data returned from GetUnit action to the serialized array:

["Unit1", "Unit2", "Unit2/3"]

and to use dataUrl with buildSelect properties of editoptions instead of value. The value of dataUrl should be URL of GetUnit action, which return array of strings with all utits. The callback buildSelect should convert the JSON array to HTML fragment, which represent <select> with all the options. See the old answer, for more implementation details and code examples.

Finally, you should fix width: "200px" to width: 200. The value of width property should be the number or the string which could be converted to the number. The usage of px or and other suffix is wrong. The next recommend fix would be removing index: 'UnitID' and all other index properties from colModel, if the value of index property is the same as the value of name property.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • As always you are so helpful. Thanks for your feedback. I tried as you suggested, but still I am getting the empty cells for the dropdown of my inline jqgrid. Please find the UPDATED code and kindly suggest me. Thanks. – Kavitha Dec 28 '16 at 10:08
  • 1
    @Kavitha: Sorry, but you misunderstood my suggestions. You should **not** make any additional Ajax call inside of `beforeProcessing`. The main answer on `url` (used to fill of main data of the grid) should contain the information which provide currently `GetUnit`. Please reread carefully [the answer](http://stackoverflow.com/a/19427444/315935), which I referenced in my current answer on your question. Look at the `colModelOptions` part in the JSON response and to the corresponding C# code. – Oleg Dec 28 '16 at 10:36
  • You are Awesome. Thanks a lot. Its working perfect. Your comments are so helpful. – Kavitha Dec 28 '16 at 11:25