将表单数据输入添加到jqGrid

时间:2016-04-14 23:27:30

标签: javascript jquery jqgrid

我正在尝试添加从表单中获取的数据并将其显示给JQGrid。

我的表格中有以下元素。

用户名

的文本框

出生日期的Datepicker

Combobox用于选择国家。

两个按钮添加和清除按钮。

每当我单击Add按钮时,它都必须向JQGrid添加一行。然后单击它所具有的清除按钮来重置整个表。

目前,我正在尝试将表单中的数据显示到行中。

以下是我的努力。

 <script>
    $(function() {
    $( "#pwd" ).datepicker();
     var source = [{
       text: "Australia",
       value: 0
         }, 
      {
        text: "India",
       value: 1
         }, 
         {
       text: "United States",
       value: 2
         },
         {
    text: "United Kingdom",
    value: 3
     }];

 $("#jqxComboBox").jqxComboBox({
    source: source,
    theme: 'energyblue',
    width: '240px',
    height: '30px',
    displayMember: 'text',
    selectedIndex: 0,
    valueMember: 'value'
  });

       $('#add').click(function(){

                 name=$('#name').val();
                 date=$('#pwd').val();
                 country=$('#jqxComboBox').val();
                 alert(name);
                  $('#jqGrid').jqGrid('addRowData',name,date,country);
                   }); });
    </script>
   <style type="text/css">
   </head>
   <body>
   <div class="container">
   <h2>Horizontal form</h2>
   <form class="form-horizontal" role="form" id="add_form">
    <input type="text" id="name"></input>
    <input type="text" id="pwd"></input>
   <div id="jqxComboBox"></div>
   <input type="submit" value="add">
   <input type="submit" value="reset">
   </div>        
   </form><table id="jqGrid">
      </table>
      </body>
      </html>

1 个答案:

答案 0 :(得分:0)

可能你应该首先初始化jqGird并运行如下代码:

&#13;
&#13;
$(function() {
    $("#jqGrid").jqGrid({
        'datatype' : 'local',
        // if there is no data at the beginning, just define an empty array [],
        // or you can set init data with data option below
        'data' : [ {
        'name' : 'testUser',
        'date' : '15/4/2016',
        'country' : 'somewhere'
        } ],
        'colNames' : [ 'Name', 'Date', 'Country' ],
        'colModel' : [ {'name' : 'name'}, {'name' : 'date'}, {'name' : 'country'} ],
        'loadComplete' : function() { // You can add data manually using code below.
            // You can define a datarow variable as single object and also an
            // array of objects.
            // array data example: var datarow = [{"name":"addedName",
            // "date":"16/4/2016", "country":"any"},
            // {"name":"addedName2", "date":"16/4/2016", "country":"any2"}];
            var datarow = {
                "name" : "addedName",
                "date" : "16/4/2016",
                "country" : "any"
            };
            
            // second parameter of method below is rowid just for generate id
            // attribute of tr element.
            // if keep rowid variable as undefined, jqGrid will generate a
            // random id instead.
            var rowid;
            
            // last paremeter tell jqGrid add new data after last row.
            $("#jqGrid").jqGrid("addRowData", rowid, datarow, "last");
        }
    });
});
&#13;
<script src="http://www.trirand.com/blog/jqgrid/js/jquery.js"></script>
<script src="http://www.trirand.com/blog/jqgrid/js/jquery.jqGrid.js"></script>
<table id="jqGrid"></table>
&#13;
&#13;
&#13;