开发者

How to load Values into the edit-form of jqGrid when user select from select box

In the edit Form is a select dropdown. When the user select an item, i want to load some values and fill them into the form.

My code so far:

var grid = $("#list").jqGrid({
  parameters...,
  colNames:[...],
  colModel :[
    ...
  ]
});  

$("#list").jqGrid(
  'navGrid',
  '#pager',
  {
    view:true,
    edit:true,
    del:true,
    search:false,
  },

  /* EDIT */
  {
    closeAfterEdit: true,
    afterSubmit: processAddEdit,
    onInitializeForm: setFormEvents,
    ...
  }
  ...
);


function setFormEvents(formid) {
  /* It sometim works when using timeout..
   * It seems to be a timing issue.
   * But i have no idea why and how to solve
   */
  setTimeout ( function(){
    $('select#data_id', formid).unbind();
    $('select#data_id', formid).change(function() {
      $.getJSON("/URL?dataid=" + $('select#data_id option:selected').val(),
        function(data){
          $.each(data, function(i,item){
            if (i开发者_如何学Ctem.field == "anrede") { $("#anrede").val(item.value); }
            else if (item.field == "titel") { $("#titel").val(item.value); }
            else if (item.field == "vorname") { $("#vorname").val(item.value); }
            else if (item.field == "nachname") { $("#nachname").val(item.value); }
            else if (item.field == "firma") { $("#firma").val(item.value); }
            else if (item.field == "strasse") { $("#strasse").val(item.value); }
            else if (item.field == "hausnummer") { $("#hausnummer").val(item.value); }
            else if (item.field == "plz") { $("#plz").val(item.value); }
            else if (item.field == "ort") { $("#ort").val(item.value); }
            else if (item.field == "land") { $("#land").val(item.value); }
          });
        });
    });
  }, 1000 );
}


To bind event (like change event in your case) to the edit field you should use dataEvents of the editoptions. See here, here or here examples. Moreover I recommend you to use recreateForm:true option additionally.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜