避免在网格上重复输入

时间:2017-03-14 09:15:21

标签: extjs combobox

我想比较组合框列说公共汽车时间。在下面的代码中。当输入新数据时,它应该比较之前的时间,如果有相同的时间,我想弹出一个错误,因为存在同一时间,我怎么能够实现这个作为验证功能而不是将它添加到监听器。

代码

Ext.application({
    name: 'Fiddle',

    launch: function () {
        run();
        window.show();
    }
});

function run() {
    var myStore=Ext.create('Ext.data.Store', {
        storeId: 'simpsonsStore',
        fields: ['busname', 'time', 'typebus',],
        pageSize:2,
        proxy: {
           type: 'memory',
          enablePaging: true
    },
        data: [{
            busname: 'ABCD',
            time: '15:30:00',
            typebus: 'AC Volvo',

        }, {
            busname: 'aaa',
            time: '13:30:00',
            typebus: 'Seater',

        },{
            busname: 'AAAA',
            time: '18:30:00',
            typebus: 'Sleeper',

        },{
            busname: 'ABCD',
            time: '19:30:00',
            typebus: 'AC Volvo',

        },]
    });

    Ext.create('Ext.grid.Panel', {
        xtype :'gridpanel',
        itemId:'busTimegrid',
        pageSize:1,
        title: 'BUS DEATILS',
        mapperId:'getBusTime',
        store: Ext.data.StoreManager.lookup('simpsonsStore'),
        columns: [{
            header: 'Bus Name',
            dataIndex: 'busname',
            editor: 'textfield'
        }, {
            text: 'Bus Time',
                dataIndex: 'time',
            xtype: 'gridcolumn',
            renderer: function (value) {
                if (value instanceof Date)
                    return Ext.util.Format.date(value, 'H:i:s');
                else
                return value;
            },
            flex: 1,
            editor: {
                xtype: 'timefield',
                format: 'H:i:s',
                allowBlank: true,
                maskRe: /[0-9,:]/,
            }
        }, {
            header: 'Bus TYpe',
            dataIndex: 'typebus',
            editable:true,
            renderer: function (value) {
                if (Ext.isNumber(value)) {
                    var store = this.getEditor().getStore();
                    return store.findRecord('id', value).get('name');
                }
                return value;
            },
            editor: {
                xtype: 'combo',
                displayField: 'name',
                valueField: 'id',
                editable:true,
                forceSelection:true,
                store: Ext.create('Ext.data.Store', {
                    fields: ['id', 'name'],
                    data: [{
                        id: 1,
                        name: 'AC Volvo'
                    }, {
                        id: 2,
                        name: 'Seater'
                    }, {
                        id: 3,
                        name: 'Sleeper'
                    }]
                })

            }
        }],
        selModel: 'cellmodel',
        plugins: {
            ptype: 'cellediting',
            clicksToEdit: 1,
        },
        listners: [{
            fn: 'onUsernamefieldBlur',
            event: 'blur',
            delegate: 'busname'
        }],
        onUsernamefieldBlur: function (textfield, e, eOpts) {

        if (textfield.getValue() === '') {
            Ext.Msg.alert("Busname can't be empty");
            textfield.setFocus(true);
        }
    },
        height: 200,
        width: 400,
            dockedItems: [{
            xtype: 'pagingtoolbar',
            store: myStore,   // same store GridPanel is using
            dock: 'bottom',
            displayInfo: true
        }],
        renderTo: Ext.getBody()

    });
}

Fiddle

2 个答案:

答案 0 :(得分:2)

  /**
   * Check the store if there's duplicate record based on the given field.
   *
   * @param {Ext.data.Store} store The store to be check
   * @param {String} field The field of the store to be check if has duplicate.
   **/
  function hasDuplicateRecord(store, field) {
      var hasDuplicate = false;
      // Iterate to all store data
      for(var i=0; i < store.data.items.length; i++) {
          var item = store.data.items[i];
          // Check if more than 1 record exists in the given item.
          if(store.queryRecords(field, item.data[field]).length > 1) {
              return true;
          }
      }
      return false;
  }

只需在网格中的某处调用上述功能即可。

  /**
   * Check the store if there's duplicate record based on the given field.
   *
   * @param {Ext.data.Store} store The store to be check
   * @param {String} field The field of the store to be check if has duplicate.
   **/
  function hasDuplicateRecord(store, field) {
      var hasDuplicate = false;
      // Iterate to all store data
      for(var i=0; i < store.data.items.length; i++) {
          var item = store.data.items[i];
          // Check if more than 1 record exists in the given item.
          if(store.queryRecords(field, item.data[field]).length > 1) {
              return true;
          }
      }
      return false;
  }

答案 1 :(得分:1)

我通过添加以下代码段

解决了这个问题

var gridRow = myStore.getModifiedRecords();
 Ext.each(gridRows, function (gridRow) {
	 var dirtyInd = myStore.indexOf(gridRow);
	 var newTime = new Date(store.getAt(dirtyInd).data.time);
   }
   myStore.each(function (record, idx) {
   var newT = new Date(record.get('time'));
   if (Ext.Date.diff(newTime, newT,Ext.Date.SECOND)=== 0)
   {
   samebustime= true;
   }
   });