2017-11-30 11 views
0

私はjavaでWebアプリケーションを開発しています。私はjqgridをJSONデータで埋めています。問題は、列「生年月日」が間違った形式を示していることです。 MySQLデータベースではフィールドはDATEタイプですが、jqgridでは "1989年12月30日"と表示されます。私は、次のフォーマットを必要とする: '1989年12月30日'日付の形式JSON jqgrid

私のサーブレットコード:

protected void doPost(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, IOException { 

    String parameter = request.getParameter("parameter"); 
    String username = request.getParameter("username"); 
    int option = Integer.parseInt(request.getParameter("option")); 

    DBConnection db = new DBConnection(); 
    StringBuilder sb = new StringBuilder(); 
    Connection con = (Connection) db.getCon(); 
    PreparedStatement ps = null; 
    ResultSet rs = null; 
    ArrayList<Customers> customerList = new ArrayList<Customers>(); 

    try { 
     sb.append("{call SP_MSTCUSTOMERS_FIND(?,?,?)}"); 
     ps = con.prepareCall(sb.toString()); 
     ps.setString(1, parameter); 
     ps.setString(2, username); 
     ps.setInt(3, option); 
     rs = ps.executeQuery(); 
     while (rs.next()) { 
      Customers customer = new Customers(); 
      customer.setIdMstCustomers(rs.getInt("IDMSTCUSTOMERS")); 
      customer.setFirstName(rs.getString("FIRST_NAME")); 
      customer.setLastName(rs.getString("LAST_NAME")); 
      customer.setBirthdate(rs.getDate("BIRTHDATE"));  
      customerList.add(customer); 
     } 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

    Gson gson = new Gson(); 
    JsonElement element = gson.toJsonTree(customerList, new TypeToken<List<Customers>>() { 
    }.getType()); 

    JsonArray jsonArray = element.getAsJsonArray(); 
    response.setContentType("application/json"); 
    response.getWriter().print(jsonArray); 
} 

jqgridのコード:

$("#list").jqGrid({ 
    url: 'PopulateCustomersTable', 
    postData: { 
     parameter: function() { 
      return $("#parameter").val(); 
     }, 
     username: function() { 
      return $("#txtusrID").val(); 
     }, 
     option: function() { 
      return $("input[name='srch']:checked").val(); //$('[name="srch"]').val(); 
     } 
    }, 
    datatype: "json", 
    mtype: 'POST', 
    colNames: ['Id', 'First Name', 'Last Name', 'Birthdate'], 
    colModel: [{ 
      name: 'IdMstCustomers', 
      index: 'IdMstCustomers', 
      width: 50, 
      fixed: true, 
      align: 'center' 
     }, { 
      name: 'FirstName', 
      index: 'FirstName', 
      width: 100, 
      fixed: true, 
      align: 'center', 
      editable: true 
     }, { 
      name: 'LastName', 
      index: 'LastName', 
      width: 100, 
      fixed: true, 
      align: 'center', 
      editable: true 
     }, { 
      name: 'Birthdate', 
      index: 'Birthdate', 
      width: 100, 
      editable: true,     
     }], 
    pager: '#pager', 
    rowNum: 10, 
    rowList: [10, 20, 30], 
    sortname: 'FirstName', 
    sortorder: 'desc', 
    viewrecords: true, 
    gridview: true, 
    caption: 'Customers', 
    jsonReader: { 
     repeatitems: false 
    }, 
    editurl: "CustomersServlet" 
}); 
$('#list').trigger('reloadGrid'); 
$('#list').jqGrid('setGridWidth', '900'); 
jQuery("#list").jqGrid('navGrid', '#pager', { 
    edit: true, 
    add: false, 
    del: false, 
    search: true 
}); 

The jqgrid data looks like this

私の顧客クラス

public Customers(int IdMstCustomers, String FirstName, String LastName, Date Birthdate) { 
    this.setIdMstCustomers(IdMstCustomers); 
    this.setFirstName(FirstName); 
    this.setLastName(LastName); 
    this.setBirthdate(Birthdate); 
} 

public Customers() { 

} 
private int IdMstCustomers; 
private String FirstName; 
private String LastName; 
private Date Birthdate; 

public void setIdMstCustomers(int IdMstCustomers) { 
    this.IdMstCustomers = IdMstCustomers; 
} 

public void setFirstName(String FirstName) { 
    this.FirstName = FirstName; 
} 

public void setLastName(String LastName) { 
    this.LastName = LastName; 
} 

public void setBirthdate(Date Birthdate) { 
    this.Birthdate = Birthdate; 
} 

public int getIdMstCustomers() { 
    return IdMstCustomers; 
} 

public String getFirstName() { 
    return FirstName; 
} 

public String getLastName() { 
    return LastName; 
} 

public Date getBirthdate() { 
    return Birthdate; 
} 
+1

をソースJSONデータではないjqGridに示すデータを投稿してください。また、他のいくつかの注意:コマンド '$( '#list')。trigger( 'reloadGrid');'は絶対に実行する必要はありません - これはビルド時に再度グリッドをリロードしますが、 –

答えて

0

あなたは、あなたの必要な形式ごとに日付値をフォーマットするformatterを追加することができます。

{ 
    name: 'Birthdate', 
    index: 'Birthdate', 
    width: 100, 
    editable: true, 
    formatter: 'date', formatoptions: { srcformat: 'M, d, Y', newformat: 'Y-m-d'} 
} 

あなたがここでそれについての詳細を読むことができます:http://www.trirand.com/jqgridwiki/doku.php?id=wiki:custom_formatter

+0

私のために働かなかった、まだ間違ったフォーマットを示します:70707070-JanJan-0101 – Chris

+0

私の更新された答えを確認してください – Waqas

+0

ありがとう@Waqas!非常に小さな調整で私のために働いた:formatoptions:{srcformat: 'M d、Y'、newformat: 'Ym-d'}月のMの部分が問題だった後のコンマ – Chris