I have a DataTable where my first column is a VIN number.
Example: FLXVU3822G1000013
Now VIN numbers are just a bunch of information tacked together. The last 6 numbers are the sequence number for that year. You can see that this example vehicle is the 13th one of the year. I'd really like to have my list filtered on those last 6 digits. Is there a way to do that?
You can easily solve this by a custom sorting plugin. In fact you just need to extract the last 6 digits and return them as a number, then dataTables will sort the column using the internal number sorting algorithm :
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"last-6-digits-pre": function ( a ) {
var n = a.substring(a.length - 6, a.length)
return parseInt(n)
}
})
Usage :
var table = $('#example').DataTable({
columnDefs : [
{ targets: 0, type: 'last-6-digits' }
]
})
where targets: 0 is the index of the column you want to be sorted this particular way.
See demo -> http://jsfiddle.net/zhmcLkb9/
Related
I'm trying to parse data from certain server, and need to export it to excel or csv.
before I export, I need to do some post processing such as merging values between parsed data.
for example,
There are two series out of all data.
series#1 - {Name: "MATH", Student:"Zay",Id:"MATH-123", Date:"12/25/2022", Status:"Good"}
series#2 - {Name: "MATH", Student:"Zay",Id:"MATH-124", Date:"12/26/2022", Status:"Bad"}
What I want to do is,
I want to update series#1's Status to
{Name: "MATH", Student:"Zay", Id:"MATH-123,MATH-124", Date:"12/25/2022,12/26/2022", Status:"Bad"}
Id, Date ==> combining with ","
Status ==> changing to latest result
Now I'm using Filter method of DataFrames,
type MyDataSet struct{
Name string
Student string
Id string
Date string
Status string
}
totalDF:=series1_result //overall result dataframe
df := dataframe.LoadStructs(series2_result) //new dataframe which needs to be compared to previous data `totalDF`
length := df.Nrow()
for i:=0;i<length;i++ {
name:=df.Subset(i).Col("Name")
student:=df.Subset(i).Col("Student")
query:= totalDF.Filter(
dataframe.F {
Colname:"Name",
Comparator:series.Eq,
Comparando:name,
},
).Filter(
dataframe.F {
Colname:"Student",
Comparator:series.Eq,
Comparando:student,
}
)
if query.Nrow()==0 {
totalDF = totalDF.Concat(df.Subset(i))
} else {
newDF:= dataframe.LoadStructs([]MyDataSet{
{
Name:query.Col("Name").String(),
Student:query.Col("Student").String(),
Id:query.Col("Id").String()+","+df.Subset(i).Col("Id").String(),
Date:query.Col("Date").String()+","+df.Subset(i).Col("Date").String(),
Status:df.Subset(i).Col("Status").String(),
}
})
query.Set(
series.Ints([]int{0}, newDF) //It's not updated as query was not an pointer
)
}
}
Even though I updated values on result of query, it's not updated on totalDF
How can I query the data from totalDF and update the data on that totalDF?
How can I get index number of Filtered item using Filter function?
Should I implement search function instead of using Filter function?
I would really appreciate it if you could help me.
Thanks everyone!
Merry Christmas!
*I tried to find out from official docs.
*But every method returns Value, not Pointer.
Is there a way to add exceptions to the natural ordering plugin, so that it ignores things like c. , [, ], ? ?
This is an example of my data:
161?
1604
[1563]
c. 1476
I'd like the sorted asc. output to be:
c. 1476
[1563]
1604
161?
Right now what I get is all the numbers first, and the strings beginning with [ afterwards.
My initialisation code:
<script type="text/javascript" src="//cdn.datatables.net/plug-ins/1.10.24/sorting/natural.js"></script>
$('#sourcesList').DataTable({
"paging": false,
"columnDefs": [
{ type: 'natural-nohmtl', targets: '_all' }
],
[...]
PS: this is my data in the wild.
The natural sorting option takes care of certain types of numeric data which you can reasonably expect to encounter in the "real world" - such as numbers with different thousands separators, or with currency symbols of different types in different positions - or "1st", "2nd" "3rd" and so on. Don't quote me on these exact examples, as I have not looked at that add-on in detail. But that is the overall idea.
Items such as question-marks - I would not expect that add-on to handle those. Items in square brackets - same thing.
Given your clarification in a comment, I think you do not need or want this add-on.
Instead you can use a column renderer with DataTables' ability to store different versions of a value - for display, sorting, and filtering.
var dataSet = [ ["161?"], ["1604"], ["[1563]"], ["c. 1476"] ];
$(document).ready(function() {
$('#example').DataTable( {
data: dataSet,
columnDefs: [ {
type: "natural-nohmtl",
targets: [ 0 ],
title: "My Data",
"render": function ( data, type, row, meta ) {
if ( type === 'sort' ) {
//return parseInt(data.replace(/\D/g, '')); // numbers as numbers
return data.replace(/\D/g, ''); // numbers as strings
} else {
return data;
}
}
} ]
} );
} );
This saves a version of each value with all non-digits stripped from the value, using replace(/\D/g, ''). This altered version of the data is stored as the value which will be used when sorting. The unaltered version is what the user will see.
My result:
This is the case where numbers are treated as text.
You can uncomment the commented-out return statement to get numbers as numbers.
However...
This crude approach of stripping non-digits from the data meets the narrow example from your question, but it may not be sufficient for all the data you are expecting to handle. So, you may need to refine the "replace" logic.
But the render function should meet your needs, more generally.
I am very new to Vue js. We are displaying a table on UI having 4 columns as
S.No | Column | Description | Type
We need to list all the unique value in Type in a drop-down (describing the datatype of a column), I am able to make Type as a drop-down however struggling to get the list of unique values in Type.
I am getting all data in an Array [sno:(..), column:(..), description:(..), type:(..)] and I am trying to get to type to put the value in one of the array and the taking unique value from that.
The code I tried first is
this.columnType = this.columnData.filter(data => {return data.type.toUpperCase()});
Also, I tried the following but it is just filtering on the specific datatype in this case "Timestamp"
this.columnType = this.columnData.filter(function(data) {
return data.type.toUpperCase() == "TIMESTAMP_NTZ"
});
Looking for some guidance to get this right.
Thanks in advance to all for helping
Use the following computed property to get the list of unique Type values:
computed:
{
onlyTypes()
{
// we want an Array with all the Type values
return this.tableDataRows.map(dataItem => dataItem.type);
},
uniqueTypes()
{
// we want only the unique Type values
return this.onlyTypes.filter((value, index, self) => self.indexOf(value) === index);
}
}
I have a problem when querying mongoDB with nested objects notation:
db.messages.find( { headers : { From: "reservations#marriott.com" } } ).count()
0
db.messages.find( { 'headers.From': "reservations#marriott.com" } ).count()
5
I can't see what I am doing wrong. I am expecting nested object notation to return the same result as the dot notation query. Where am I wrong?
db.messages.find( { headers : { From: "reservations#marriott.com" } } )
This queries for documents where headers equals { From: ... }, i.e. contains no other fields.
db.messages.find( { 'headers.From': "reservations#marriott.com" } )
This only looks at the headers.From field, not affected by other fields contained in, or missing from, headers.
Dot-notation docs
Since there is a lot of confusion about queries MongoDB collection with sub-documents, I thought its worth to explain the above answers with examples:
First I have inserted only two objects in the collection namely: message as:
> db.messages.find().pretty()
{
"_id" : ObjectId("5cce8e417d2e7b3fe9c93c32"),
"headers" : {
"From" : "reservations#marriott.com"
}
}
{
"_id" : ObjectId("5cce8eb97d2e7b3fe9c93c33"),
"headers" : {
"From" : "reservations#marriott.com",
"To" : "kprasad.iitd#gmail.com"
}
}
>
So what is the result of query: db.messages.find({headers: {From: "reservations#marriott.com"} }).count()
It should be one because these queries for documents where headers equal to the object {From: "reservations#marriott.com"}, only i.e. contains no other fields or we should specify the entire sub-document as the value of a field.
So as per the answer from #Edmondo1984
Equality matches within sub-documents select documents if the subdocument matches exactly the specified sub-document, including the field order.
From the above statements, what is the below query result should be?
> db.messages.find({headers: {To: "kprasad.iitd#gmail.com", From: "reservations#marriott.com"} }).count()
0
And what if we will change the order of From and To i.e same as sub-documents of second documents?
> db.messages.find({headers: {From: "reservations#marriott.com", To: "kprasad.iitd#gmail.com"} }).count()
1
so, it matches exactly the specified sub-document, including the field order.
For using dot operator, I think it is very clear for every one. Let's see the result of below query:
> db.messages.find( { 'headers.From': "reservations#marriott.com" } ).count()
2
I hope these explanations with the above example will make someone more clarity on find query with sub-documents.
The two query mechanism work in different ways, as suggested in the docs at the section Subdocuments:
When the field holds an embedded document (i.e, subdocument), you can either specify the entire subdocument as the value of a field, or “reach into” the subdocument using dot notation, to specify values for individual fields in the subdocument:
Equality matches within subdocuments select documents if the subdocument matches exactly the specified subdocument, including the field order.
In the following example, the query matches all documents where the value of the field producer is a subdocument that contains only the field company with the value 'ABC123' and the field address with the value '123 Street', in the exact order:
db.inventory.find( {
producer: {
company: 'ABC123',
address: '123 Street'
}
});
I'm using jquery data tables and I'm assigning an array of values to the initialization of the data table. The table basically looks like this.
based on an an radio button I would like to limit the items that are display in the table and the items that are searched in the table.
For my example it would be based on the "Chart column". I want to limit the table to only show the items that are based on chart "D" or Chart "S". Here is how I'm initializing the table.
if (!$.fn.DataTable.isDataTable( '#fundLookUptbl' ) ) {
fundTable = $('#fundLookUptbl').DataTable( {
data: funds,
columns: [
{ "mData": "chart" },
{ "mData": "fund" },
{ "mData": "orgDefault" },
{ "mData": "progDefault" }
]
} );
var filteredData = fundTable
.columns( [0, 1] )
.data()
.eq( 0 )
.filter( function ( value, index ) {
return value = 'D' ? true : false;
} );
}
This is obviously not working, and the filterData variable is a lousy attempt on trying to make it work. I'm having a hard time understanding the API's. So the question is , How can initialize the table to only show the items that are based on a given chart. I know that I can remove the items of the array but i don't want to do that since I would simple like to be able to switch between chart "D" and "S" but still continue to search through the other columns.
I believe that filtering the column would solve your problem.
table.column(0).search('Bruno').draw()
So you could just filter the column when the radio button selection change
Here is a fiddle example
I´m not sure to be understanding what you want to do but here are some options:
One way is selecting by default value example "s". You can use a dropdown is easier to handled .Then select with jQuery the dafault value "s" on that dropdown and add a function
$("#DropdownId").change(function () {
var chart=$("#DropdownId").val();
});
$.ajax({
url: "url")",//url to reload page with new value
type: "POST",
data: {chart:chart},
success: function (data) {
}
});
});
on this way the filter is on backend. If you want to do something on the row depending of a column value you shoud to add something like this
"fnRowCallback": function (nRow, mData, iDisplayIndex, iDisplayIndexFull) {
if (mData["chart"] =="s") {
return nRow;
}
},
Datatables: custom function inside of fnRowCallback.
Good luck
fundTable.order( [0, 'asc'] );
Try that or look at this particular page for reference:
https://datatables.net/reference/api/order%28%29
Basically orders in pair of columnIndex in either asc(ending) or desc(ending) order.