Clear the sort applied on a table from the code - sorting

I am trying to clear the sort applied on a table from the code by using the active and direction fields but with no success:
#ViewChild(MatSort) sort: MatSort;
private clearSort() {
// Reset the sort column
this.sort.active = "";
// Reset the sort direction
this.sort.direction = "";
}
I looked into the Sort Header Documentation but I didn't find any native method that helps to clear an applied sort on a given table.
Any advice would be appreciated.

You can set a default sort option and use sort function on the MatSort api to set that default option.
defaultSort: MatSortable = {
id: 'defColumnName',
start: 'asc',
disableClear: true
};
then use sort function on MatSort directive:
this.sort.sort(this.defaultSort);
//default sort direction
this.sort.direction = 'asc';
Beware that the above code will trigger the sortChange event subscription.

In general, the way to clear a sort is to re-apply the original sort order.
If the original sort order does not have a natural key, you may need to introduce an original order field in your data so you can sort by that field to "clear" any sorting that has occurred since the data loaded.
I haven't used MatSort, but in the absence of someone confirming otherwise, I would assume that to clear the sorting, you need to apply sorting as described above.
If the library supplied a reset, it would have to maintain the data in the original order in memory somewhere, which is why most libraries don't do this.

Related

Realm React native sorted order

Basically I have a MessageSchema with a date property, on my app I need to query all the messages stored on the database and be able to sort them by their date to display them on a ListView, I do the query as follows:
return realm.objects("Message").sorted('date');
This works, but only one way, the messages are sorted on ascending order only, I haven't found a way to do it on descending order and the docs of the react native only show one example:
let hondas = realm.objects('Car').filtered('make = "Honda"');
// Sort Hondas by mileage
let sortedHondas = hondas.sorted('miles');
Any advice is welcome.
Versions:
react-native: "0.40.0"
realm js: "1.0.2"
return realm.objects("Message").sorted('date', true);
Looking at the source code you can see that the sorted method expects
a descriptor and a Boolean named reverse, that it is set as false unless
you change it, so the code above just reverses the order.

buffered store with local sorting (client-side)

I have a buffered grid on which I've implemented a local sort function (client side). I would like to remove the sort indication (darker background and little arrow) on the column header when the store reloads.
Does anyone know how to accomplish this in 4.1?
To make this clearer:
I want my columns to be sortable. I do not want them to initialize with sorting disabled. Users should be able to click the header and sort it all they want. But, what I want is to be able to turn off the sort programmatically. I.e., remove any sort classes that were applied from user clicks (things like the darker background and the little sort direction arrow).
The reason I would do this is because I am using a modified buffered store which allows me to do local sorting (client-side) with all of the buffered data (not just the chunk that is displayed). Normally, using a buffered store will make local sorting disabled because it would only sort the data that is displayed in the grid - not all of the data in memory, so the guys at Sencha made any grid that has a buffered store automatically disable local sorting - only remote sorting works. Well, as I said, mine is modified so it will work - but then when this buffered store reloads with new data from the database it does not enjoy the handy sortOnLoad feature normal grids get as a matter of course. In my use-case it is more logical to remove the sort state than it is to override the sortOnLoading functionality and make it apply the same sort to the new data, hence, this question.
I do have it worked out now, I'll post an answer shortly along with my implementation of a buffered store with local sorting, in case anyone is interested and for my own future reference.
I should also point out that I am very aware of the client-side performance penalties that come with local sorting on a store that would need to be buffered (as opposed to just using remote sorting of the data on the server). I am aware that this is probably why Sencha does not support local sorting on their buffered stores. I have assessed all of the pros and cons to this and in my specific use-case it is the most sensible thing for me to do.
Buried deep in the dom there is a setSortState function on the Ext.grid.header.Container class and on the Ext.grid.column.Column class. These don't show up anywhere in the 4.1.0 docs but they're in the code nevertheless.
You can look at these functions yourself to get a complete concept of what they do, but the gist of both them is a switch statement that looks for either a 'DESC', 'ASC' or a null in the first argument, e.g.:
setSortState(`DESC`);
setSortState(`ASC`);
setSortState(null);
Calling either the header version or the column version of this function with a single null argument will remove the sort classes on a column. The only real difference is that the header version looks at the grid's store to find the active sorter in the store's sorters property and then uses that data to determine which column to call this function on - the column version simply runs off the column object that it is called from.
In my use-case I don't add a sorter to the store sorters property so I am using the column version (i.e. calling setSortState from an Ext.grid.column.Column object).
First, here is an example of my implementation of a buffered store that allows local (client-side) sorting:
Ext.define('MyApp.store.TheBufferedStoreWithLocalSorting', {
extend: 'Ext.data.Store',
requires: [
'Ext.ux.data.PagingMemoryProxy',
'Ext.util.MixedCollection'
],
model: 'MyApp.model.SomeModel',
buffered: true,
pageSize: 100,
remoteSort: true, // this just keeps sorting from being disabled
proxy: {
type: 'pagingmemory',
reader: 'json'
},
/*
* Custom sort function that overrides the normal store sort function.
* Basically this pulls all the buffered data into a MixedCollection
* and applies the sort to that, then it puts the SORTED data back
* into the buffered store.
*/
sort: function(sorters) {
var collection = new Ext.util.MixedCollection();
collection.addAll(this.proxy.data);
collection.sort(sorters);
this.pageMap.clear();
this.getProxy().data = collection.getRange();
this.load();
}
});
Now, to answer my question, to remove the sorter classes whenever the store reloads I just need to do this:
Ext.each(myGrid.columns, function(column, index) {
if (column.hasCls('x-column-header-sort-ASC') ||
column.hasCls('x-column-header-sort-DESC')) {
myGrid.columns[index].setSortState(null);
return false;
}
});
When the store is reloaded you could add the following to the store's load event handler:
Ext.create("Ext.data.Store", {
listeners: {
load: {
fn: function () {
grid.addCls("no-sort-icon");
}
}
}
};
Then modify your css to hide the icon when element is child of "no-sort-icon" (this would be on the grid)
.no-sort-icon .x-column-header-text {
background-image: none;
}
.no-sort-icon .x-column-header {
background-color: #C5C5C5;
}
If I understand you correctly, setting this config on the specified column should solve your problem
columns: [
{text: 'First Name', dataIndex:'firstname', sortable: false},
{text: 'Last Name', dataIndex:'lastname'},
]
more details here http://docs.sencha.com/ext-js/4-0/#!/api/Ext.grid.column.Column-cfg-sortable
This is a configuration option, so it would disable the sortability of a column when the grid is first rendered.
Here is a jsfiddle demo
* Note that I use Ext. 4.0.7, you can switch to 4.1.0, but for some reason an unrelated display bug with the dropdown pops out

Sorting a NotesDocumentCollection based on a date field in SSJS

Using Server side javascript, I need to sort a NotesDcumentCollection based on a field in the collection containing a date when the documents was created or any built in field when the documents was created.
It would be nice if the function could take a sort option parameter so I could put in if I want the result back in ascending or descending order.
the reason I need this is because I use database.getModifiedDocuments() which returns an unsorted notesdocumentcollection. I need to return the documents in descending order.
The following code is a modified snippet from openNTF which returns the collection in ascending order.
function sortColByDateItem(dc:NotesDocumentCollection, iName:String) {
try{
var rl:java.util.Vector = new java.util.Vector();
var tm:java.util.TreeMap = new java.util.TreeMap();
var doc:NotesNotesDocument = dc.getFirstDocument();
while (doc != null) {
tm.put(doc.getItemValueDateTimeArray(iName)[0].toJavaDate(), doc);
doc = dc.getNextDocument(doc);
}
var tCol:java.util.Collection = tm.values();
var tIt:java.util.Iterator = tCol.iterator();
while (tIt.hasNext()) {
rl.add(tIt.next());
}
return rl;
}catch(e){
}
}
When you construct the TreeMap, pass a Comparator to the constructor. This allows you to define custom sorting instead of "natural" sorting, which by default sorts ascending. Alternatively, you can call descendingMap against the TreeMap to return a clone in reverse order.
This is a very expensive methodology if you are dealing with large number of documents. I mostly use NotesViewEntrycollection (always sorted according to the source view) or view navigator.
For large databases, you may use a view, sorted according to the modified date and navigate through entries of that view until the most recent date your code has been executed (which you have to save it somewhere).
For smaller operations, Tim's method is great!

Rearranging active record elements in Yii

I am using a CDbCriteria with its own conditions, with & order clauses. However, the order i want to give to the elements in the array is way too complex to specify in the order clause.
The solution i have in mind consists of obtaining the active records with the defined criteria like this
$theModelsINeed = MyModel::model()->findAll($criteria);
and then rearrange the order from my php code. How can i do this? I mean, i know how to iterate through its elements, but i donĀ“t know if it is possible to actually change them.
I have been looking into this link about populating active records, but it seems quite complicated and maybe someone could have some better advice.
Thanks
There is nothing special about Yii's active records. The find family of methods will return an array of objects, and you can sort this array like any other array in PHP.
If you have complex sort criteria, this means that probably the best tool for this is usort. Since you will be dealing with objects, your user-defined comparison functions will look something like this:
function compare($x, $y)
{
// First sort criterion: $obj->Name
if ($x->Name != $y->Name) {
return $x->Name < $y->Name ? -1 : 1; // this is an ascending sort
}
// Second sort criterion: $obj->Age
if ($x->Age != $y->Age) {
return $x->Age < $y->Age ? 1 : -1; // this is a descending sort
}
// Add more criteria here
return 0; // if we get this far, the items are equal
}
If you do want to get an array as a result, you can use this method for fetching data that supports dbCriteria:
$model = MyModel::model()->myScope();
$model->dbCriteria->condition .= " AND date BETWEEN :d1 AND :d2";
$model->dbCriteria->order = 'field1 ASC, field2 DESC';
$model->dbCriteria->params = array(':d1'=>$d1, ':d2'=>$d2);
$theModelsINeed = $model->getCommandBuilder()
->createFindCommand($model->tableSchema, $model->dbCriteria)
->queryAll();
The above example shows using a defined scope and modifying the condition with named parameters.
If you don't need Active Record, you could also look into Query Builder, but the above method has worked pretty well for me when I want to use AR but need an array for my result.

Server Side sorting in an ExtJS GridPanel

I have a paged GridPanel and don't know how to go about adding server side sorting. Do I add a listener for the sortChange() event? Do I override it? Or is there another event that I should work with.
Any help would be appreciated.
No. In the definition for your store just set
remoteSort: true, // to enable sorting
sortInfo: { // the default sort
field: 'someField',
direction: 'ASC' | 'DESC'
}
And on the server side you will now be getting a sort and dir request parameters specifying the sort field and direction. When the columns are clicked the store will update sorted by the column and direction you pick.

Resources