javascriptjqueryasp.net-mvc-4jqgridmvcjqgrid

Unable to bind the data to dropdown in jqgrid when it is editing getting data using web api


$j(document).ready(function () {
 $j.ajax({
        type: "GET",
        url: "http://localhost:9611/api/Master/GetBackendUsersList",
        contentType: "json",
        dataType: "json",
        success: function (data) {
            var dataList;
            var StatusList = '';

            $j('#list2').jqGrid({
                caption: "Users Details",
                data: data,
                datatype: "local",
                height: '100%',
                width: '100%',

                colNames: ["UserName", "RoleId", "Name", "RoleName", "LoginId"],
                colModel: [
                      { name: "UserName", index: 'UserName', editable: true },
                      { name: 'RoleId', index: "RoleId", hidden: true, width: 150, editable: true },
                      { name: "Name", index: "Name", editable: true },
                      {
                          name: "RoleName", index: "RoleName", editable: true, edittype: 'select', editoptions: {

                              dataInit: function (element)
                              {

                                  $j.ajax({
                                      type: "GET",
                                      url: "http://localhost:9611/api/Master/GetRoles",
                                      contentType: "json",
                                      dataType: "json",
                                      success: function (mydata) {
                                          dataList = mydata;

                                          for (var i = 0; i < dataList.length; i++) {
                                              //if (StatusList == "")
                                              //    StatusList = dataList[i].RoleId + ":" + dataList[i].RoleName;
                                              //else
                                              StatusList = StatusList + dataList[i].RoleId + ":" + dataList[i].RoleName+ ';' ;
                                          }   
                                      }
                                  });
                              },
                              value: "0:Select;" + StatusList,
                          }
                      },
                      { name: 'LoginId', index: "LoginId", hidden: true, width: 150 }
                ],
                gridview: true,
                rowNum: 5,
                rowList: [5, 10, 15],
                pager: '#jQGridDemoPager',
                sortname: "UserName",
                viewrecords: true,
                sortorder: "desc",
                //width: '100%',
                //height: '100%',

                shrinkToFit: false,
                editurl: SiteUrl + "api/Master/UpdateBackendUserDetails/" ,
            });
$j.extend(true, $j.jgrid.edit, {
                recreateForm: true,
                beforeShowForm: function ($jform) {
                    $jform.closest(".ui-jqdialog").position({
                        of: window, // or any other element
                        my: "center center",
                        at: "center center"
                    });
                }
            });
            $j('#list2').jqGrid('navGrid', '#jQGridDemoPager',
                   {
                       add: false,
                       search: false,
                       recreateForm: true,
                       beforeShowForm: function ($jform) {
                           $jform.find(".FormElement[readonly]")
                               .prop("disabled", true)
                               .addClass("ui-state-disabled")
                               .closest("LoginId")
                               .prev(".CaptionTD")
                               .prop("disabled", true)
                               .addClass("ui-state-disabled");
                       },

                   },
            { recreateForm: true }
                   );
        }
    });
});

Dropdown data from api is like:

[{"RoleId":1,"RoleName":"Administrator"},{"RoleId":2,"RoleName":"Sales"},{"RoleId":3,"RoleName":"Secretory/President"},{"RoleId":4,"RoleName":"Apartment Owner"},{"RoleId":5,"RoleName":"Apartment User"}]

Solution

  • Use this way :

        editoptions:{value: getData()}
    

    and then create one method

        function getData(){
         var states = [{"RoleId":1,"RoleName":"Administrator"},{"RoleId":2,"RoleName":"Sales"},{"RoleId":3,"RoleName":"Secretory/President"},{"RoleId":4,"RoleName":"Apartment Owner"},{"RoleId":5,"RoleName":"Apartment User"}];        
          return states;
    
        }
    

    In your case you need to add ajax call in getData() and return values/array which has been created by the result.