ExtJs在时间字段中填充值

ExtJs populating values in timefields

本文关键字:填充 字段 时间 ExtJs      更新时间:2023-09-26

我正在学习ExtJs,我有一个与timefield s的表单gridpanel,我想用从数据库中提取的JSON数据填充。

这是我要存储的数据:

Ext.create('Ext.data.Store', {
    storeId:'employeeStore',
    fields:['id', 'app_name', 'open_datetime', 'close_datetime', 'holiday_desc'],
    data:[
        {id:1, app_name:"A", open_datetime:"08:00", close_datetime:"09:00", holiday_desc:"ADay"},
        {id:2, app_name:"B", open_datetime:"00:00", close_datetime:"23:59", holiday_desc:" BDay"}
    ]
});

现在我的表格中有timefield,我想用我存储的数据open_datetimeclose_datetime填充openTime, closeTime字段。

Ext.application({
  name: 'Fiddle',
  launch: function() {
    Ext.create('Ext.form.Panel', {
      renderTo: Ext.getBody(),
      width: 300,
      bodyPadding: 10,
      title: 'getters and setters',
      items: [{
        xtype: 'datemenu',
        floating: false,
        height: 210,
        id: 'datePickerMenu',
        width: 240
      }, {
        xtype: 'text',
        text: 'openTime',
        margin: '5px',
        id: 'open',
        dataIndex: 'open_datetime'
      }, {
        xtype: 'timefield',
        style: {
          margin: '5px'
        }
      }, {
        xtype: 'text',
        text: 'closeTime',
        margin: '5px'
      }, {
        xtype: 'timefield',
        style: {
          margin: '5px'
        }
      }, {
        xtype: 'text',
        text: 'Message',
        margin: '5px'
      }, {
        xtype: 'textfield',
        style: {
          margin: '5px'
        }
      }]
    });
  }
});

现在,我如何在 timefields中显示数据?在使用列时,我应该使用data_Index显示数据吗?

有几种方法可以做到这一点,根据所使用的Ext JS版本,可能只有一种方法。我假设你用的至少是v5版本。我假设的另一件事是,你有一个网格与你的商店,因为它不会有意义有一个表单加载两个记录…它不知道用哪条记录作为它的表单值。

此外,我认为这是一个很好的做法,避免使用id字段,特别是如果你正在创建一个表单…如果你想在同一个视图中两次使用那个表单呢?你不能,因为你有两个相同的id。相反,您可能希望使用itemIdreference

这是我想到的一个例子:

Ext.application({
  name: 'Fiddle',
  launch: function() {
    var store = Ext.create('Ext.data.Store', {
      fields: ['id', 'app_name', 'open_datetime', 'close_datetime', 'holiday_desc'],
      data: [{
          id: 1,
          app_name: "A",
          open_datetime: "08:00",
          close_datetime: "09:00",
          holiday_desc: "ADay"
        }, {
          id: 2,
          app_name: "B",
          open_datetime: "00:00",
          close_datetime: "23:59",
          holiday_desc: " BDay"
        }
      ]
    });
    var first = store.first();
    var second = store.getAt(1);
    /* Non-Bound Form... this doesn't use model binding,
     * but it uses the Name property, which relies on loading
     * the form with a record */
    Ext.define('My.Non.Bound.Form', {
      extend: 'Ext.form.Panel',
      bodyPadding: 10,
      title: 'Non-Bound Form: getters and setters',
      items: [{
        xtype: 'datemenu',
        floating: false
      }, {
        xtype: 'timefield',
        fieldLabel: 'Open Time',
        name: 'open_datetime',
        margin: 5
      }, {
        xtype: 'timefield',
        fieldLabel: 'Close Time',
        margin: 5,
        name: 'close_datetime'
      }, {
        xtype: 'textfield',
        text: 'Message',
        margin: '5px'
      }, {
        xtype: 'textfield',
        style: {
          margin: '5px'
        }
      }]
    });
    /* Bound Form... this makes use of ViewModel binding,
     * so that means you must supply a ViewModel, and its
     * data with a currentRecord property... this has many
     * added benefits... I encourage you to read https://docs.sencha.com/extjs/6.0/application_architecture/view_models_data_binding.html */
    Ext.define('My.Bound.Form', {
      extend: 'Ext.form.Panel',
      bodyPadding: 10,
      title: 'Bound Form: getters and setters',
      items: [{
        xtype: 'datemenu',
        floating: false
      }, {
        xtype: 'timefield',
        fieldLabel: 'Open Time',
        margin: 5,
        bind: {
          value: '{currentRecord.open_datetime}'
        }
      }, {
        xtype: 'timefield',
        fieldLabel: 'Close Time',
        margin: 5,
        bind: {
          value: '{currentRecord.close_datetime}'
        }
      }, {
        xtype: 'textfield',
        text: 'Message',
        margin: '5px'
      }, {
        xtype: 'textfield',
        style: {
          margin: '5px'
        }
      }]
    });
    var myNonBoundForm = Ext.create('My.Non.Bound.Form'); // render plain form approach
    myNonBoundForm.loadRecord(first);
    var myBoundForm = Ext.create('My.Bound.Form', {
      viewModel: {
        data: {
          currentRecord: second
        }
      }
    });
    Ext.create('Ext.container.Container', {
      renderTo: Ext.getBody(),
      layout: {
        type: 'hbox',
        align: 'stretch'
      },
      defaults: {
        flex: 1
      },
      items: [myNonBoundForm, myBoundForm]
    })
  }
});

在这个例子中,我向您展示了两种加载表单数据的方法。第一种方法是利用name属性。这基本上类似于dataIndex属性,但是您必须使用表单的loadRecord方法或其他一些手动方法来填充表单字段值。

第二种方法利用ViewModel绑定…Ext JS 5中的一个新的、强大的概念。无论何时对绑定值进行更改,它都会更新模型的值,并在UI组件中反映该值。我建议您阅读我为ViewModel绑定提供的链接,因为它将涉及更多细节。