How to Sort a Store leaving One value - extjs

I have a variable sequence number in my Data.model . sequence number can be greater or equal to -1. i would like to display the store values (whose sequence number are greater than or equal to 0) sorted in ascending Order.this sorting should ignore records with sequence number -1 and display that at the last.

You have to use sorters confing of your store and custom function to sort with sorterFn property.
Working fiddle
sorters: [{
sorterFn: function(record1, record2) {
var data1 = record1.get('sortValue'),
data2 = record2.get('sortValue');
if(data1 === -1) {
return 1;
}
if(data1 === data2) {
return 0;
} else {
if(data1 < data2) {
return -1;
} else if (data1 > data2) {
return 1;
}
}
}
}],

Related

React table sort and search

i have data like
const data =[
{
name:'gacvuc',
pot1[
{
density:1
}
]
pot2[
{
density:2
}
]
}
.....
]
now i made a toggle in that do we wanna see pot1 density or pot2 density
and made a table with that like this
<tablebody>
data.map(a,in)=>{
const show= toggleChoice === 0 ? a.pot1 : a.pot2;
}
return(
<tablecell>{a.name}</tablecell>
<tablecell>{show.density}</tablecell>
</tablebody>
);
now what i wanna do is i wanna sort by density in ascending or descending or search by it's name can anyone show me how to do that
First, I would recommend taking a look at the format of your data and looking for mistakes.
To sort your data, you can simply use the built-in sort method and pass in a function that gives instructions on how to sort two different objects in your data.
data.sort(function(a, b) {
// you can define this based on your toggle variable
const pot = "pot1";
// densities of both object
const aDensity = a[pot][0].density;
const bDensity = b[pot][0].density;
// compare them and then return -1, 0, or 1 accordingly
if (aDensity == bDensity) { // if they are equal, return 0
return 0;
}
else if(aDensity > bDensity) { // > return 1
return 1;
}
else { // < return -1
return -1;
}
})
In this snippet, you get the densities of two arbitrary objects, a and b, and then return -1, 0, or 1 depending on the values of the density. In this example. I sorted them in ascending order, but if you want descending, you could swap the 1 for -1 and vice versa in the code.

AngularJS multiselect custom filter not working as intended

In an AngularJS record display (with filters) I have a multiselect array of territories that a user can select from to find out if a certain item is available in a certain territory.
The array returns a list of values such as
['001','010','200']
based on the ID of the territories selected. This is then checked against a JSON list of records which has a JSON value looks like this
territoriesnotavailable: "001, 085, 090"
Each record either has this set to null, or has a list from one to many numbers.
I currently use the following code (customFilter) which works perfectly if you only select ONE value.. it basically makes the item filter out if the territory selected in the multiselect is in the list of territoriesnotavailable
function CustomTerritoryFilter() {
return function(data, query) {
if (query.length === 0) return data;
if (data) return data.filter(function(item) {
for (var i = 0; i < query.length; i++) {
var queryitem = query[i]["id"];
if(item.territoriesnotavailable) {
stringB = item.territoriesnotavailable;
} else {
stringB = 'xxxxxxxx';
}
stringA = queryitem;
if (!(stringB.indexOf( stringA ) > -1)) {
return data;
}
}
});
return [];
};
}
So if I choose only one filter (resulting in a query of ['010'] for example. and this appears in territoriesnoavailable for the record.. it vanishes as expected.. but if I choose any value that is NOT in territoriesnotavailable the item appears again.. i need the record to vanish if ANY selected territory appears in the list regardless of any that do not
function CustomTerritoryFilter() {
return function(data, query) {
if (query.length === 0) return data;
if (data) return data.filter(function(item) {
for (var i = 0; i < query.length; i++) {
var queryitem = query[i]["id"];
if(item.territoriesnotavailable) {
stringB = item.territoriesnotavailable;
} else {
stringB = 'xxxxxxxx';
}
stringA = queryitem;
if (!(stringB.indexOf( stringA ) > -1)) {
return false;
}
}
return true;
});
return [];
};
}
This is the code that I settled on. It basically sets a flag as 1 (show) and then checks each number in the filter.. if there is just one occurance of the number in the list, the flag is set to zero. At the end of the check data is shown if the flaG REMAins as 1
function CustomTerritoryFilter() {
return function(data, query) {
if (query.length === 0) return data;
if (data) return data.filter(function(item) {
var flag = 1;
for (var i = 0; i < query.length; i++) {
var queryitem = query[i]["id"];
if(item.territoriesnotavailable) {
stringB = item.territoriesnotavailable;
} else {
stringB = 'xxxxxxxx';
}
stringA = queryitem;
if (stringB.indexOf( stringA ) > -1) {
flag = 0;
}
}
if(flag === 1) {
return data;
}
});
return [];
};
}

how to sort array by date and count in ionic 2

I want to sort my array by date and count, i'm able to sort array only by date my data is as below
count date
"0" "2018-03-06T07:09:02+00:00"
"0" "2018-03-06T07:07:02+00:00"
"0" "2018-03-06T07:06:03+00:00"
"0" "2018-03-06T07:02:06+00:00"
"0" "2018-03-06T06:39:55+00:00"
"0" "2018-03-06T06:30:14+00:00"
"1" "2018-03-06T06:22:20+00:00"
"1" "2018-03-06T06:07:04+00:00"
"0" "2018-03-06T06:03:17+00:00"
"14" "2018-03-01T10:28:27.998000+00:00"
"0" null
"0" null
"0" null
my code is below..
this.nodelist.sort((a, b) => {//lastDate dsc
if (new Date(b.lastDate) > new Date(a.lastDate)) {
return 1;
}
if (new Date(b.lastDate) < new Date(a.lastDate)) {
return -1;
}
return 0;
});
i want to sort array by count and date both means if array is having count > 0 then it should be first then count with zero and on last all other records. can anyone help me to solve this?
You can use your code and modify it like this:
this.nodelist.sort((a, b) => {
// 1st property, sort by count
if (a.count > b.count)
return -1;
if (a.count < b.count)
return 1;
// 2nd property, sort by date
if (new Date(b.lastDate) > new Date(a.lastDate))
return 1;
if (new Date(b.lastDate) < new Date(a.lastDate))
return -1;
return 0;
});
How does it work?
The first two if statements will sort the array by count.
If count is equal, the code will consider the second property (lastDate).
Try this:
let xyz = numbers.sort(function(a, b) {
var countA = a.count;
var countB = b.count;
var dateA = new Date(a.date);
var dateB = new Date(b.date);
if(countA == countB)
{
return (dateB < dateA) ? -1 : (dateB > dateA) ? 1 : 0;
}
else
{
return (countB < countA) ? -1 : 1;
}
});
console.log(xyz);

Angular 2: Delete object in Array

I want to delete an object in an array when that object's ID is equal to the ID of the object getting compared. Currently, it only removes the first object in the array
if(this.selectedProducts.length > 0){
for(let x of this.selectedProducts){
if(prod._id === x._id){
this.selectedProducts.splice(x,1); //this is the part where I 'delete' the object
this.appended = false;
}else{
this.appended = true;
}
}
if (this.appended) {
this.selectedProducts.push(prod);
}
}else{
this.selectedProducts.push(prod);
}
this.selectEvent.emit(this.selectedProducts);
}
this.selectedProducts.splice(x,1);
The first parameter to splice must be the index, not the object.
If you're using for...of, you can't get the index easily. So you should use a regular for loop instead. With some additional simplifications, your code would look like this:
for (let i = this.selectedProducts.length - 1; i >= 0; this.selectedProducts.length; i--) {
if (prod._id === this.selectProducts[i]._id) {
this.selectedProducts.splice(i, 1); //this is the part where I 'delete' the object
}
}
this.selectedProducts.push(prod);
It's highly likely that using filter would be better anyway:
this.selectedProducts = this.selectedProducts.filter(x => prod._id !== x._id).concat(prod);

Angular ng-repeat filtering

I have a deeply nested object. I have some records which contain 2 fields that show keys of object properties. I also have select needed to search records by property of object and input to search by key of object. So if I choose option1 and type in input some text, it will be shown the matches in the first field (not second!). And it's similar for second field.
How I try to realize:
I wrote a filter http://plnkr.co/edit/z9DEmfYz2grW9UonLcFK?p=preview
.filter('appFilter', function() {
return function(value, select, input) {
var result = [];
input = input.toLowerCase();
var reg = new RegExp(input,'g');
if (angular.isArray(value)) {
if (input === '' || $scope.isFiltering) {
return value;
} else if (select.value === 'Sequence') {
for (let i = 0; i < value.length; i++) {
if (value[i].Sequence.toLowerCase().match(reg)) {
result.push(value[i]);
}
}
return result;
} else if (select.value === 'ID') {
for (let i = 0; i < value.length; i++) {
if (angular.isArray(value[i].Document)) {
for (let j = 0; j < value[i].Document.length; j++) {
if (value[i].Document[j].ID.toLowerCase().match(reg)) {
result.push(value[i]);
}
}
}
}
return result;
} else {
console.log('error');
}
}
}
})
In controller I set to select's ng-model first option: $scope.selectParameter = $scope.parameter[0];
In debug I set to input parameter some value (123 for example).
So I searching record by first field that contains 123 value. And result finds and pushes the object. But in browser shows anything.
What's the problem? And I can't avoid the empty option with '?' value in my select :(
UPDATED
Nearly solve my problem: http://plnkr.co/edit/z9DEmfYz2grW9UonLcFK?p=preview
It filters by appropriate field and input value. But I faced with another troubles.
When input is empty it doesn't show any record. And second is when I choose second option (ID) filter duplicates some records.
Also I try to switch off filter without clearing the input text by clicking on checkbox.
It's what I want to do but it doesn't work:
else if (input === '' || $scope.isFiltering) {
return value;
}
$scope.isFiltering is ng-model for checkbox input
I tried using angulars default filter. I'm not sure if this is exactly what you want, but maybe it helps a little.
.filter('appFilter', function($filter) {
return function(value, select, input) {
if( !angular.isDefined(input) || input.length < 1) {
return value;
}
// Angulars "filter" lets you pass in a object-structure to search for nested fields.
var query =
(select.value === 'Sequence') ?
{Sequence:input} : {Document:{ID:input}};
return $filter('filter')(value, query);
}
})
http://plnkr.co/edit/Egkw9bUvTPgooc0u2w7C?p=preview

Resources