2012-07-26 21 views
0

我从JSON Web服务通过Ext JS JSON代理返回.NET序列化对象(项目对象)的列表。 我无法让我的Ext.grid.Panel正确显示我的日期格式字段。为什么会这样?搜索下面的“这一个”。所有其他字段在我的Ext网格中正确显示。当我从日历控件中保存日期时,它会将日期正确存储在数据库中。为了排除其他问题,我已经在你的对象中硬编码了你的日期。Ext JS 4:返回.NET可空的DateTime?从Web方法作为JSON对象到ExtJS不显示在Ext.grid.Panel

序列化类:

[Serializable] 
    public class Project 
    { 
     public string project_id; 
     public string project_number; 
     public string project_name; 
     public string description; 
     public DateTime? date_start; 
    } 

Web方法:

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false, XmlSerializeString = false)] 
    public List<Project> GetProjects(string myTest, string bar) 
    { 
     Database db = DatabaseFactory.CreateDatabase("HIMC-DEV"); 

     DbCommand cmd = db.GetStoredProcCommand("project_get_list"); 
     db.AddInParameter(cmd, "@user_id", DbType.String, ""); 

     DataSet ds = db.ExecuteDataSet(cmd); 
     DataTable dt = ds.Tables[0]; 
     List<Project> projectList = new List<Project>(); 
     foreach (DataRow row in dt.Rows) 
     { 
      Project p = new Project(); 
      p.project_id = row[0].ToString(); 
      p.project_number = row[1].ToString(); 
      p.project_name = row[2].ToString(); 
      p.description = row[3].ToString(); 
      p.date_start = new DateTime(2012, 1, 12); // <=== this one 
      projectList.Add(p); 
     } 

     return projectList; 
    } 

分机型号:

Ext.define('Project', { 
    extend: 'Ext.data.Model', 
    fields: [ 
     { name: 'project_id' }, 
     { name: 'project_name' }, 
     { name: 'project_number' }, 
     { name: 'description' }, 
     { name: 'date_start', type: 'date' } 
    ] 
}); 

JSON代理:

Ext.define('Ext.ux.AspWebAjaxProxy', { 
    extend: 'Ext.data.proxy.Ajax', 
    require: 'Ext.data', 

    buildRequest: function (operation) { 
     var params = Ext.applyIf(operation.params || {}, this.extraParams || {}), 
           request; 
     params = Ext.applyIf(params, this.getParams(params, operation)); 
     if (operation.id && !params.id) { 
      params.id = operation.id; 
     } 

     params = Ext.JSON.encode(params); 

     request = Ext.create('Ext.data.Request', { 
      params: params, 
      action: operation.action, 
      records: operation.records, 
      operation: operation, 
      url: operation.url 
     }); 
     request.url = this.buildUrl(request); 
     operation.request = request; 
     return request; 
    } 
}); 

分机窗口嵌入形式:

Ext.define('ProjectEdit', { 
     extend: 'Ext.window.Window', 

     alias: 'widget.projectedit', 
     title: 'Edit Project', 
     layout: 'fit', 
     autoShow: true, 
     closable : true, 

     initComponent: function() { 
      this.items = [ 
       { 
        xtype: 'form', 
        width: 650, 
        height: 300, 
        bodyPadding: 20, 
        items: [ 
         { 
          xtype: 'textfield', 
          name: 'project_id', 
          fieldLabel: 'Project ID' 
          //disabled: true 
         }, 
         { 
          xtype: 'textfield', 
          name: 'project_number', 
          fieldLabel: 'Project Number' 
         }, 
         { 
          xtype: 'textfield', 
          name: 'project_name', 
          fieldLabel: 'Project Name' 
         }, 
         { 
          xtype: 'datefield', 
          format: 'm/d/Y', 
          allowBlank: true, 
          name: 'date_start', 
          fieldLabel: 'Start Date' 
         }, 
         { 
          xtype: 'combo', 
          fieldLabel: 'Manager', 
          emptyText: 'select keyword', 
          store: keywordStore, 
          valueField: 'name', 
          displayField: 'name', 
          mode: 'remote', 
          autoSelect: false, 
          selectOnFocus: true, 
          shadow: true, 
          forceSelection: true, 
          triggerAction: 'all', // not sure what this is? 
          hideTrigger: true, 
          //multiSelect:true, 
          typeAhead: true, 
          minChars: 1, 
          renderTo: document.body 
         }, 
         { 
          xtype: 'htmleditor', 
          name: 'description', 
          fieldLabel: 'Description', 
          enableColors: false, 
          enableAlignments: false, 
          width: '100%' 
         } 
        ] 

       } 
      ]; 
... 

分机网格:

Ext.define('ProjectGrid', { 
     extend: 'Ext.grid.Panel', 

     initComponent: function() { 
      var me = this; 

      Ext.applyIf(me, { 
       store: store, 
       columns: [ 
        { text: 'Project ID', dataIndex: 'project_id', sortable: true }, 
        { text: 'Project Number', dataIndex: 'project_number', sortable: true }, 
        { text: 'Project Name', dataIndex: 'project_name', sortable: true, width: 300 }, 
        { text: 'Start Date', dataIndex: 'date_start', sortable: true, width: 300, renderer: Ext.util.Format.dateRenderer('Y-m-d') }, 
        { text: 'Description', dataIndex: 'description', sortable: true, width: 500 } 
       ], 
       listeners: { 
        //itemdblclick: this.editProject 
        itemdblclick: function(view, record, item, index, e) { 
         //var w = new ProjectEdit(); 
         var w = Ext.widget('projectedit'); 
         w.show(); // show the window 
         w.down('form').loadRecord(record); // load the record in the form 
         w.callback = Ext.bind(this.editProject, this); // bind lets you set the scope of the callback (for the project that was double clicked) 
        } 
       } 
      }); 

      me.callParent(arguments); 
     }, 

... 

    }); 

JSON请求从萤火虫NET标签图像(打开一个新标签使其更大):

enter image description here

回答

2

您遇到这个问题似乎与这个日期栏的日期格式进行连接。
默认情况下,ExtJS预计日期的发送格式与.Net使用的格式不同。

更新你的模型通过以下方式应该有所帮助:

{ name: 'date_start', type: 'date', dateFormat: 'MS' } 

{ name: 'date_start', type: 'date', dateFormat: 'U' } 

见ExtJS的API为dateFormat及其可能values

相关问题