2011-12-02 26 views
1

我需要用户将数据添加到网格,而不会回发。我认为cellsubmit:'clientArray'会做到这一点,但我仍然得到“没有网址已设置”的错误。以下代码是我从Oleg借来的一个网格,但是使用了cellsubmit集。jqgrid将数据添加到网格,无需回传

$(document).ready(function() { 
     'use strict'; 
     var myData = [ 
       { id: "1", invdate: "2007-10-01", name: "test", note: "note", amount: "200.00", tax: "10.00", closed: true, ship_via: "TN", total: "210.00" }, 
       { id: "2", invdate: "2007-10-02", name: "test2", note: "note2", amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00" }, 
       { id: "3", invdate: "2011-07-30", name: "test3", note: "note3", amount: "400.00", tax: "30.00", closed: false, ship_via: "FE", total: "430.00" }, 
       { id: "4", invdate: "2007-10-04", name: "test4", note: "note4", amount: "200.00", tax: "10.00", closed: true, ship_via: "TN", total: "210.00" }, 
       { id: "5", invdate: "2007-10-31", name: "test5", note: "note5", amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00" }, 
       { id: "6", invdate: "2007-09-06", name: "test6", note: "note6", amount: "400.00", tax: "30.00", closed: false, ship_via: "FE", total: "430.00" }, 
       { id: "7", invdate: "2011-07-30", name: "test7", note: "note7", amount: "200.00", tax: "10.00", closed: true, ship_via: "TN", total: "210.00" }, 
       { id: "8", invdate: "2007-10-03", name: "test8", note: "note8", amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00" }, 
       { id: "9", invdate: "2007-09-01", name: "test9", note: "note9", amount: "400.00", tax: "30.00", closed: false, ship_via: "TN", total: "430.00" }, 
       { id: "10", invdate: "2007-09-08", name: "test10", note: "note10", amount: "500.00", tax: "30.00", closed: true, ship_via: "TN", total: "530.00" }, 
       { id: "11", invdate: "2007-09-08", name: "test11", note: "note11", amount: "500.00", tax: "30.00", closed: false, ship_via: "FE", total: "530.00" }, 
       { id: "12", invdate: "2007-09-10", name: "test12", note: "note12", amount: "500.00", tax: "30.00", closed: false, ship_via: "FE", total: "530.00" } 
      ], 
      myGrid = $("#list"), 
      lastSel = -1, 
      inEdit; 

     myGrid.jqGrid({ 
      datatype: 'local', 
      data: myData, 
      colNames: ['Inv No', 'Date', 'Client', 'Amount', 'Tax', 'Total', 'Closed', 'Shipped via', 'Notes'], 
      colModel: [ 
       { name: 'id', index: 'id', width: 70, align: 'center', sorttype: 'int', formatter: 'int', editable: true, 
        editoptions: { 
         //readonly: 'readonly', 
         disabled: 'disabled', 
         dataInit: function (elem) { 
          if (!inEdit) { 
           $(elem).val($.jgrid.randId()); 
          } 
         } 
        } 
       }, 
       { name: 'invdate', index: 'invdate', width: 75, align: 'center', sorttype: 'date', 
        formatter: 'date', formatoptions: { newformat: 'd-M-Y' }, datefmt: 'd-M-Y', 
        editable: true 
       }, 
       { name: 'name', index: 'name', width: 65, editable: true }, 
       { name: 'amount', index: 'amount', width: 75, sorttype: 'int', formatter: 'int', editable: true, 
        editoptions: { 
         dataInit: function (elem) { 
          $(elem).mask("99:99"); 
         } 
        } 
       }, 
       { name: 'tax', index: 'tax', width: 52, sorttype: 'int', formatter: 'int', editable: true }, 
       { name: 'total', index: 'total', width: 60, sorttype: 'int', formatter: 'int', editable: true }, 
       { name: 'closed', index: 'closed', width: 67, align: 'center', formatter: 'checkbox', editable: true, 
        edittype: 'checkbox', editoptions: { value: 'Yes:No', defaultValue: 'Yes' } 
       }, 
       { name: 'ship_via', index: 'ship_via', width: 95, align: 'center', formatter: 'select', editable: true, 
        edittype: 'select', editoptions: { value: 'FE:FedEx;TN:TNT;IN:Intim', defaultValue: 'Intime' } 
       }, 
       { name: 'note', index: 'note', width: 60, sortable: false, editable: true, 
        editoptions: { 
         dataInit: function (elem) { 
          $(elem).val(inEdit ? "in Edit" : "in Add"); 
         } 
        } 
       } 
      ], 
      rowNum: 10, 
      rowList: [5, 10, 20], 
      pager: '#pager', 
      gridview: true, 
      ignoreCase: true, 
      rownumbers: true, 
      sortname: 'invdate', 
      viewrecords: true, 
      cellsubmit: 'clientArray', 
      sortorder: 'desc', 
      caption: 'Combining Advanced Searching and Toolbar Searching in one grid', 
      height: 'auto' 
     }); 
     myGrid.jqGrid('navGrid', '#pager', 
      { del: false, search: false }, 
      { // Edit 
       recreateForm: true, 
       beforeInitData: function() { 
        inEdit = true; 
       } 
      }, 
      { // Add 
       recreateForm: true, 
       beforeInitData: function() { 
        inEdit = false; 
       } 
      }); 
    }); 

回答

0

你试图做的是使用的形式编辑编辑一个不jqGrid的当前支持本地数据。尽管如此,我如何描述here,其中一个可以实现这一点。

我从the answer获得演示并修改了一点以使用当前版本的jqGrid。相应的demo。演示可以根据需要做更多的事情,例如上下文菜单。您可以删除不需要的功能。

+0

谢谢,这是我一直在寻找。我不知道为什么我在搜索中没有遇到原始答案,但我投了答案,因为它真的回答了我的问题。 – Ron

+0

@Ron:不客气! – Oleg