Custom sorting method for dhtmlx grid not working - dhtmlx

Could anyone provide custom sorting method for dhtmlx grid? Mine is not working. Do not know what I am missing.

I use this function to sort ignoring the string case:
function str_uncase(a, b, order) {
return (a.toLowerCase() > b.toLowerCase() ? 1 : -1) * (order === "asc" ? 1 : -1);
}
Call it like:
grid1.setColSorting("str_uncase,str_uncase,... ");

Related

Sort by key desc in polymerfire with firebase-query

I am using firebase and polymer to build an app, and I have a data I got using the firebase-query element. I want to reorder the data so that it displays the new data first, so I thought I could use the sort attribute on template tag to sort it by the key in a descending order, but it's not working as expected.
<firebase-query
id="questionQuery"
path="/questions"
limit-to-last="15"
data="{{questions}}">
</firebase-query>
And the template looks like this method:
<template is="dom-repeat" items="[[questions]]" sort="_computeSort" as="question"><p>[[question.body]]</p></template>
and inside my element definition, I have this:
_computeSort: function(a, b) {
console.log(a.__firebaseKey__);
if (a.__firebaseKey__ == b.__firebaseKey__) {
return 0;
}
return a.__firebaseKey__ > b.__firebaseKey__ ? -1 : 1;
},
This is not working. The output of the log to console is just bunch of undefindes, so the problem should be there, but how can I access each question's key?
Okay, I got an anser
$key is the expression to access the key generated by the firebase, so just change the sorting function to this:
_computeSort: function(a, b) {
if (a.$key == b.$key) {
return 0;
}
return a.$key > b.$key ? -1 : 1;
},
I actually had just copied the code from here Polymer 1.0: Sorting dom-repeat and that's how I ended up with code like _firebasekey__.

ExtJs 6 doSort method

I didn't find doSort function available in EXT 6 with respect to the grid columns and also didnt find it in any upgrade notes. may be because it is a private function, can anyone please tell me what is the alternative to do the same thing what doSort was doing in Ext 4 ?
I tried to use sorters instead,
{
text: 'columnText',
dataIndex: 'columnIndex',
sorter: me.sort
}
sort: function(v1,v2) {
...
}
but i didn't found smth like dataIndex or columnName in v1, v2 parameters to do sort. (it's just a model)
I need empty cell be from below after Sort Ascending, empty cell be from above after Sort Descending
Thanks.
What is the problem here? You can use the model object to retrieve your column data to sort. From the docs:
sorter: function(record1, record2) {
var name1 = record1.data.columnIndex;
var name2 = record2.data.columnIndex;
return name1 > name2 ? 1 : (name1 === name2) ? 0 : -1;
}
EDIT: If you dont want to rewrite this for every column, then you can do a trick like this:
sorter: (function(columnIndex){ return function(v1, v2){ me.sort(v1, v2, columnIndex);} })("column1")
Now, you can get the column name as 3rd argument in your sort function.
You want to sort the store, not a column. Have a look at the doSort function in ExtJS 4 for a moment:
doSort: function(state) {
var tablePanel = this.up('tablepanel'),
store = tablePanel.store;
// If the owning Panel's store is a NodeStore, this means that we are the unlocked side
// of a locked TreeGrid. We must use the TreeStore's sort method because we cannot
// reorder the NodeStore - that would break the tree.
if (tablePanel.ownerLockable && store.isNodeStore) {
store = tablePanel.ownerLockable.lockedGrid.store;
}
store.sort({
property: this.getSortParam(),
direction: state
});
},
/**
* Returns the parameter to sort upon when sorting this header. By default this returns the dataIndex and will not
* need to be overriden in most cases.
* #return {String}
*/
getSortParam: function() {
return this.dataIndex;
},
Now, this code is still working in ExtJS 6, just that it is no longer part of the framework. You can "put it back into the framework" (e.g. as an override) and it should work again. Or you can use the relevant parts directly from the column event, e.g.
columns:[{
dataIndex:'ABC',
listeners:{
headercontextmenu:function(ct, column) {
column.mySortState = column.mySortState=='ASC'?'DESC':'ASC';
ct.up('grid').getStore().sort({
property:column.dataIndex;
direction:column.mySortState
});
}
}
}]
Maybe you need to define it in the model like this:
fields: [{
name: "textField",
type: 'string',
//sortType: 'asInt'
}]

Ignore nulls during sort in jqGrid

I'm trying to implement a custom sorting where I try creating an impression of ignoring nulls. From the accepted answer here here's how I would go about doing things:
Implement a custom sorting function
In the sorting function I would determine if the sort order is ascending or descending
If the sort order is ascending I would assign a higher value like 1000 so the null values remain at the bottom.
If the sort order is descending I would assign a value of -1 so the null values remain at the bottom.
Is there a way to determine the sort order in custom sorting function? I would appreciate if someone knows a better way of solving this problem.
This is how I solved this problem.
In the colModel I defined the column like
colModel:[
...
{
name: 'HD Column', width: 50,
sorttype: function (cellObj, rowObj) {
var sortColumnName = grid.jqGrid('getGridParam', 'sortname');
var sortOrder = grid.jqGrid('getGridParam', 'sortorder');
if (sortOrder === 'desc') {
return ((cellObj === null || cellObj === '') ? -1000 : Number(cellObj));
}
else if (sortOrder === 'asc') {
return ((cellObj === null || cellObj === '') ? 50000 : Number(cellObj));
}
}
...
]

Magento Layered Navigation - Sort By Count

I would like to sort each of my layered navigation filters by # of items in each filter.
Here's what shows now-
Books - 1
CD's - 2
DVD's - 20
What I want to show-
DVD's - 20
CD's - 2
Books - 1
I've been looking at catalog/layer/filter.phtml, but I can't figure out how to sort magento collections.
Ideally I want something like this:
$this->getItems()->order('Count Desc')
How can I accomplish this?
Found the way to do this-
Modified Mage/Catalog/Model/Layer/Filter/Abstract.php to re-sort using count in the getItems method.
public function getItems()
{
if (is_null($this->_items)) {
$this->_initItems();
}
//5-16-11 Custom sort
$items = $this->_items;
usort($items, array("Mage_Catalog_Model_Layer_Filter_Abstract", "sortByCount"));
return $items;
}
public static function sortByCount($a, $b)
{
if ($a->getCount() == $b->getCount()) {
return 0;
}
return ($a->getCount() > $b->getCount()) ? -1 : 1;
}
A good place to start is Mage/Catalog/Model/Layer/Filter/Attribute.php. The list is built there..

How to use JQUERY to filter table rows dynamically using multiple form inputs

I'm displaying a table with multiple rows and columns. I'm using a JQUERY plugin called uiTableFilter which uses a text field input and filters (shows/hides) the table rows based on the input you provide. All you do is specify a column you want to filter on, and it will display only rows that have the text field input in that column. Simple and works fine.
I want to add a SECOND text input field that will help me narrow the results down even further. So, for instance if I had a PETS table and one column was petType and one was petColor -- I could type in CAT into the first text field, to show ALL cats, and then in the 2nd text field, I could type black, and the resulting table would display only rows where BLACK CATS were found. Basically, a subset.
Here is the JQUERY I'm using:
$("#typeFilter").live('keyup', function() {
if ($(this).val().length > 2 || $(this).val().length == 0)
{
var newTable = $('#pets');
$.uiTableFilter( theTable, this.value, "petType" );
}
}) // end typefilter
$("#colorFilter").live('keyup', function() {
if ($(this).val().length > 2 || $(this).val().length == 0)
{
var newTable = $('#pets');
$.uiTableFilter( newTable, this.value, "petColor" );
}
}) // end colorfilter
Problem is, I can use one filter, and it will display the correct subset of table rows, but when I provide input for the other filter, it doesn't seem to recognize the visible table rows that are remaining from the previous column, but instead it appears that it does an entirely new filtering of the original table. If 10 rows are returned after applying one filter, the 2nd filter should only apply to THOSE 10 rows. I've tried LIVE and BIND, but not working.
Can anyone shed some light on where I'm going wrong? Thanks!
The uiTableFilter plugin doesn't support what you're trying to do. A quick look at the source reveals this:
elems.each(function(){
var elem = jQuery(this);
jQuery.uiTableFilter.has_words(getText(elem), words, false)
? matches(elem)
: noMatch(elem);
});
and that expands to (essentially) this:
elems.each(function(){
var elem = jQuery(this);
jQuery.uiTableFilter.has_words(getText(elem), words, false)
? elem.show()
: elem.hide();
});
So all it does is spin through all the rows, .show() those that match, and .hide() those that don't; uiTableSorter doesn't pay attention to the current shown/hidden state of the rows and there's no way to tell it to filter on multiple columns.
If you really need your desired functionality then you can modify the plugin's behavior (the code is pretty small and simple) or just write your own. Here's a stripped down and simplified version that supports multiple filters and is a more conventional jQuery plugin than uiTableFilter:
(function($) {
$.fn.multiFilter = function(filters) {
var $table = $(this);
return $table.find('tbody > tr').each(function() {
var tr = $(this);
// Make it an array to avoid special cases later.
if(!$.isArray(filters))
filters = [ filters ];
howMany = 0;
for(i = 0, f = filters[0]; i < filters.length; f = filters[++i]) {
var index = 0;
$table.find('thead > tr > th').each(function(i) {
if($(this).text() == f.column) {
index = i;
return false;
}
});
var text = tr.find('td:eq(' + index + ')').text();
if(text.toLowerCase().indexOf(f.word.toLowerCase()) != -1)
++howMany;
}
if(howMany == filters.length)
tr.show();
else
tr.hide();
});
};
})(jQuery);
I'll leave error handling and performance as an exercise for the reader, this is just an illustrative example and I wouldn't want to get in the way of your learning. You could wire it up something like this:
$('#type').keyup(function() {
$('#leeLooDallas').multiFilter({ column: 'petType', word: this.value });
});
$('#color').keyup(function() {
$('#leeLooDallas').multiFilter([
{ column: 'petType', word: $('#type').val() },
{ column: 'petColor', word: this.value }
]);
});
And here's a live example (which assumes that you're going to enter something in "type" before "color"): http://jsfiddle.net/ambiguous/hdFDt/1/

Resources