📄 store.js
字号:
* reloaded. If local sorting is used, the cache is sorted internally.
* @param {String} fieldName The name of the field to sort by.
* @param {String} dir (optional) The sort order, "ASC" or "DESC" (case-sensitive, defaults to "ASC")
*/
sort : function(fieldName, dir){
var f = this.fields.get(fieldName);
if(!f){
return false;
}
if(!dir){
if(this.sortInfo && this.sortInfo.field == f.name){ // toggle sort dir
dir = (this.sortToggle[f.name] || "ASC").toggle("ASC", "DESC");
}else{
dir = f.sortDir;
}
}
var st = (this.sortToggle) ? this.sortToggle[f.name] : null;
var si = (this.sortInfo) ? this.sortInfo : null;
this.sortToggle[f.name] = dir;
this.sortInfo = {field: f.name, direction: dir};
if(!this.remoteSort){
this.applySort();
this.fireEvent("datachanged", this);
}else{
if (!this.load(this.lastOptions)) {
if (st) {
this.sortToggle[f.name] = st;
}
if (si) {
this.sortInfo = si;
}
}
}
},
/**
* Calls the specified function for each of the Records in the cache.
* @param {Function} fn The function to call. The Record is passed as the first parameter.
* Returning <tt>false</tt> aborts and exits the iteration.
* @param {Object} scope (optional) The scope in which to call the function (defaults to the Record).
*/
each : function(fn, scope){
this.data.each(fn, scope);
},
/**
* Gets all records modified since the last commit. Modified records are persisted across load operations
* (e.g., during paging).
* @return {Ext.data.Record[]} An array of Records containing outstanding modifications.
*/
getModifiedRecords : function(){
return this.modified;
},
// private
createFilterFn : function(property, value, anyMatch, caseSensitive){
if(Ext.isEmpty(value, false)){
return false;
}
value = this.data.createValueMatcher(value, anyMatch, caseSensitive);
return function(r){
return value.test(r.data[property]);
};
},
/**
* Sums the value of <i>property</i> for each record between start and end and returns the result.
* @param {String} property A field on your records
* @param {Number} start The record index to start at (defaults to 0)
* @param {Number} end The last record index to include (defaults to length - 1)
* @return {Number} The sum
*/
sum : function(property, start, end){
var rs = this.data.items, v = 0;
start = start || 0;
end = (end || end === 0) ? end : rs.length-1;
for(var i = start; i <= end; i++){
v += (rs[i].data[property] || 0);
}
return v;
},
/**
* Filter the records by a specified property.
* @param {String} field A field on your records
* @param {String/RegExp} value Either a string that the field
* should begin with, or a RegExp to test against the field.
* @param {Boolean} anyMatch (optional) True to match any part not just the beginning
* @param {Boolean} caseSensitive (optional) True for case sensitive comparison
*/
filter : function(property, value, anyMatch, caseSensitive){
var fn = this.createFilterFn(property, value, anyMatch, caseSensitive);
return fn ? this.filterBy(fn) : this.clearFilter();
},
/**
* Filter by a function. The specified function will be called for each
* Record in this Store. If the function returns <tt>true</tt> the Record is included,
* otherwise it is filtered out.
* @param {Function} fn The function to be called. It will be passed the following parameters:<ul>
* <li><b>record</b> : Ext.data.Record<p class="sub-desc">The {@link Ext.data.Record record}
* to test for filtering. Access field values using {@link Ext.data.Record#get}.</p></li>
* <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li>
* </ul>
* @param {Object} scope (optional) The scope of the function (defaults to this)
*/
filterBy : function(fn, scope){
this.snapshot = this.snapshot || this.data;
this.data = this.queryBy(fn, scope||this);
this.fireEvent("datachanged", this);
},
/**
* Query the records by a specified property.
* @param {String} field A field on your records
* @param {String/RegExp} value Either a string that the field
* should begin with, or a RegExp to test against the field.
* @param {Boolean} anyMatch (optional) True to match any part not just the beginning
* @param {Boolean} caseSensitive (optional) True for case sensitive comparison
* @return {MixedCollection} Returns an Ext.util.MixedCollection of the matched records
*/
query : function(property, value, anyMatch, caseSensitive){
var fn = this.createFilterFn(property, value, anyMatch, caseSensitive);
return fn ? this.queryBy(fn) : this.data.clone();
},
/**
* Query the cached records in this Store using a filtering function. The specified function
* will be called with each record in this Store. If the function returns <tt>true</tt> the record is
* included in the results.
* @param {Function} fn The function to be called. It will be passed the following parameters:<ul>
* <li><b>record</b> : Ext.data.Record<p class="sub-desc">The {@link Ext.data.Record record}
* to test for filtering. Access field values using {@link Ext.data.Record#get}.</p></li>
* <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li>
* </ul>
* @param {Object} scope (optional) The scope of the function (defaults to this)
* @return {MixedCollection} Returns an Ext.util.MixedCollection of the matched records
**/
queryBy : function(fn, scope){
var data = this.snapshot || this.data;
return data.filterBy(fn, scope||this);
},
/**
* Finds the index of the first matching record in this store by a specific property/value.
* @param {String} property A property on your objects
* @param {String/RegExp} value Either a string that the property value
* should begin with, or a RegExp to test against the property.
* @param {Number} startIndex (optional) The index to start searching at
* @param {Boolean} anyMatch (optional) True to match any part of the string, not just the beginning
* @param {Boolean} caseSensitive (optional) True for case sensitive comparison
* @return {Number} The matched index or -1
*/
find : function(property, value, start, anyMatch, caseSensitive){
var fn = this.createFilterFn(property, value, anyMatch, caseSensitive);
return fn ? this.data.findIndexBy(fn, null, start) : -1;
},
/**
* Find the index of the first matching Record in this Store by a function.
* If the function returns <tt>true</tt> it is considered a match.
* @param {Function} fn The function to be called. It will be passed the following parameters:<ul>
* <li><b>record</b> : Ext.data.Record<p class="sub-desc">The {@link Ext.data.Record record}
* to test for filtering. Access field values using {@link Ext.data.Record#get}.</p></li>
* <li><b>id</b> : Object<p class="sub-desc">The ID of the Record passed.</p></li>
* </ul>
* @param {Object} scope (optional) The scope of the function (defaults to this)
* @param {Number} startIndex (optional) The index to start searching at
* @return {Number} The matched index or -1
*/
findBy : function(fn, scope, start){
return this.data.findIndexBy(fn, scope, start);
},
/**
* Collects unique values for a particular dataIndex from this store.
* @param {String} dataIndex The property to collect
* @param {Boolean} allowNull (optional) Pass true to allow null, undefined or empty string values
* @param {Boolean} bypassFilter (optional) Pass true to collect from all records, even ones which are filtered
* @return {Array} An array of the unique values
**/
collect : function(dataIndex, allowNull, bypassFilter){
var d = (bypassFilter === true && this.snapshot) ?
this.snapshot.items : this.data.items;
var v, sv, r = [], l = {};
for(var i = 0, len = d.length; i < len; i++){
v = d[i].data[dataIndex];
sv = String(v);
if((allowNull || !Ext.isEmpty(v)) && !l[sv]){
l[sv] = true;
r[r.length] = v;
}
}
return r;
},
/**
* Revert to a view of the Record cache with no filtering applied.
* @param {Boolean} suppressEvent If true the filter is cleared silently without notifying listeners
*/
clearFilter : function(suppressEvent){
if(this.isFiltered()){
this.data = this.snapshot;
delete this.snapshot;
if(suppressEvent !== true){
this.fireEvent("datachanged", this);
}
}
},
/**
* Returns true if this store is currently filtered
* @return {Boolean}
*/
isFiltered : function(){
return this.snapshot && this.snapshot != this.data;
},
// private
afterEdit : function(record){
if(this.modified.indexOf(record) == -1){
this.modified.push(record);
}
this.fireEvent("update", this, record, Ext.data.Record.EDIT);
},
// private
afterReject : function(record){
this.modified.remove(record);
this.fireEvent("update", this, record, Ext.data.Record.REJECT);
},
// private
afterCommit : function(record){
this.modified.remove(record);
this.fireEvent("update", this, record, Ext.data.Record.COMMIT);
},
/**
* Commit all Records with outstanding changes. To handle updates for changes, subscribe to the
* Store's "update" event, and perform updating when the third parameter is Ext.data.Record.COMMIT.
*/
commitChanges : function(){
var m = this.modified.slice(0);
this.modified = [];
for(var i = 0, len = m.length; i < len; i++){
m[i].commit();
}
},
/**
* Cancel outstanding changes on all changed records.
*/
rejectChanges : function(){
var m = this.modified.slice(0);
this.modified = [];
for(var i = 0, len = m.length; i < len; i++){
m[i].reject();
}
},
// private
onMetaChange : function(meta, rtype, o){
this.recordType = rtype;
this.fields = rtype.prototype.fields;
delete this.snapshot;
this.sortInfo = meta.sortInfo;
this.modified = [];
this.fireEvent('metachange', this, this.reader.meta);
},
// private
findInsertIndex : function(record){
this.suspendEvents();
var data = this.data.clone();
this.data.add(record);
this.applySort();
var index = this.data.indexOf(record);
this.data = data;
this.resumeEvents();
return index;
}
});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -