How to concatenate fields in Tabulator, while retaining header search? - concatenation

We need to be able to concatenate multiple fields into a single Tabulator column. This in of itself is easy enough, but we want to also be able to use the column search function to search for ANY of the values that have been inserted.
Using the mutate function of course breaks Tabulators ability to distinguish one original value from any other.
We've got around this by having Tabulator modify our data source dynamically (dynamically regenerating the datasource query) but this is of course not the ideal solution.
I'm wondering if anyone else has run into this and found a built-in solution?
EG: Column "NAME" contains the concatenation of "firstname, lastname". We need to be able to use the column filter to search either the firstname or lastname, but through mutation/concatenation, we are now forced to search firstname first instead of lastname.

You could use a custom filter function to perform the search. In the below example, I have used the header filter, but it could be an external search input as well. My example has firstName and lastName columns concatenated as Lastname, Firstname in the name column. By using the headerFilterFunc column definition property, you could return search results that match either first name or last name:
const dataSet1 = [
{ firstName: 'Billy', lastName: 'Bob', age: '12', gender: 'male' },
{ firstName: 'Mary', lastName: 'May', age: '1', gender: 'female' },
{ firstName: 'Christine', lastName: 'Lobowski', age: '42', gender: 'female' },
{ firstName: 'Brendon', lastName: 'Philips', age: '25', gender: 'male' },
{ firstName: 'Margret', lastName: 'Marmajuke', age: '76', gender: 'female' }
]
const table = new Tabulator('#table', {
data: dataSet1,
columns: [
{
title: 'Name',
field: 'name',
mutator: (value, data) => data.lastName + ', ' + data.firstName,
headerFilter: 'input',
headerFilterFunc: (headerValue, rowValue, rowData, filterParams) => {
let filterValue = headerValue.toUpperCase()
let firstName = rowData.firstName.toUpperCase()
let lastName = rowData.lastName.toUpperCase()
return firstName.includes(filterValue) || lastName.includes(filterValue)
}
},
{ title: 'Age', field: 'age' },
{ title: 'Gender', field: 'gender' }
]
})
<link href="https://unpkg.com/tabulator-tables#5.1.8/dist/css/tabulator.min.css" rel="stylesheet">
<script type="text/javascript" src="https://unpkg.com/tabulator-tables#5.1.8/dist/js/tabulator.min.js"></script>
<div id="table"></div>

Related

Column not showing data using Gridjs while integrating with React js

currently I am using Gridjs to display my table in my Reactjs application.
This is the code, the Name, and Email is displaying correctly, but the created_at is not, giving me empty. How can I display the data? thank you..
You need to define a custom id for your column if you're using a JSON input.
Example:
const grid = new Grid({
columns: [{
id: 'name',
name: 'Name'
}, {
id: 'email',
name: 'Email'
}, {
id: 'phoneNumber',
name: 'Phone Number'
}],
data: [
{ name: 'John', email: 'john#example.com', phoneNumber: '(353) 01 222 3333' },
{ name: 'Mark', email: 'mark#gmail.com', phoneNumber: '(01) 22 888 4444' },
]
});
See https://gridjs.io/docs/examples/import-json/

Finding a property value in an array of object based on another property value

I have the following array
const arrayOfObject = [{
name: 'Mike',
age: '38',
}, {
name: 'Russell',
age: '42',
}, {
name: 'John',
age: '26',
}, ];
and I want to find the age of 'Russell' for instance.
If I use .find() I manage to get the whole object
const employeeAge = arrayOfObject.find((item) => item.name === 'Russell')
Is it possible to do it in one line?

Adding rowspan or column span functionality at the table while generating data-table using (React) material table

I am generating dataTable using material-table plugin in ReactJS. I couldn't find any direct way or option to generate rowspan or column span at dataTable using the plugin. Is there any way to do it ?
Here is a sample screenshot of what table might look like but will be shown via dataTable
The explanation of what you are trying to do is a little bit unclear.
If you want to add something like a button on each row in a col span you can define it in the "columns" prop of your Table like so:
columns={[
{ title: 'Update', field: '', render: rowData => <button onClick={() => doSomethingWithId(rowData.id)} className="myTableButtonStyle" /> },
{ title: 'Name', field: 'name' },
....
]}
This will add an update button which will call on the doSomethingWithId() function passing the id of the line as a parameter.
Is it what you are looking for? Else would you mind explaining a little bit more what you want?
EDIT
Here is what i obtain
with the following code
<MaterialTable
data={[
{ name: '', nationality: 'British', address: '', country: 'England' },
{ name: 'Noor', nationality: 'American', address: 'California', country: 'US' },
{ name: '', nationality: 'Chinese', address: '', country: 'China' },
{ name: '', nationality: '', address: '', country: '' },
]}
columns={[
{ title: 'Name', field: 'name' },
{ title: 'Nationality', field: 'nationality' },
{ title: 'Address', field: 'address' },
{ title: 'Country', field: 'country' },
]}
options={{
rowStyle: {
height: '25px',
},
}}
title="Display Data"
/>
You don't need to add rows or colspan to achieve this render, just giving the good dataSet and the Columns definition will do.
In your data props you need to give a list of objects with all the datas you need.
You then define which datas are displayed where with the columns props. if you want a more personalize render in one column (ex: you want to display the country in bold) you can define it by giving a render in the columns object like so:
{title: 'Country', field: '', render: rowData => <strong>{rowData.country}</strong>
for the empty row to have the same size as others, use props options on your table.
Does this help you?

How to filter all the fields in react?

How to filter all the fields in react?
I have written a code which only filters specified fields.
let filteredContacts=this.props.contacts.filter(
(contact)=>{
return contact.name.indexOf(this.state.search)!==-1;
}
);
the above code only returns filtered names...but i want to filter name,age,city work.
this is the data file:
export default function(){
return [{
key: 1,
name: 'Steve',
city: 'Paris',
}, {
key: 2,
name: 'Tim',
city: 'London',
}, {
key: 3,
name: 'Stella',
city: 'Bankok',
}, {
key: 4,
name: 'John',
city: 'Paris',
}];
}
i want to create a search bar which which filters all the fields suppose if i search john then the list of john should be displayed and if i search paris then all the entries which have paris should be displayed..So the code which i have written only searches for name as i have specified name..i want to search the inputed data to search all the fields.
The filter criteria can be the indexOf on Object.values() like below but it will only give a complete match
var data = [{
key: 1,
name: 'Steve',
city: 'Paris',
}, {
key: 2,
name: 'Tim',
city: 'London',
}, {
key: 3,
name: 'Stella',
city: 'Bankok',
}, {
key: 4,
name: 'John',
city: 'Paris',
}];
var value = 'Paris'
var newData = data.filter((obj) => {
return Object.values(obj).indexOf(value) > -1
})
console.log(newData)
Well, I fastly tried, do a loop on your keys. Maybe there's a more elegant way.
let filteredContacts=this.props.contacts.filter(
(contact)=>{
var found = false;
Object.keys(contact).map(function(key, index) {
if (contact[key].indexOf(this.state.search) >= 0)
found = true;
}
return found;
}
);

OrderBy with enumerable property in AngularJS

I have an object array where every object can have an array of properties, like this
[
{
name: 'Big house',
properties: [{name: 'Area',value: '400'}, {name: 'Year',value: '1950'}]
},
{
name: 'Small house',
properties: [{name: 'Area',value: '400'}, {name: 'Year',value: '1950'}]
},
{
name: 'Green house',
properties: [{name: 'Area',value: '40'}, {name: 'Year',value: '2008'}]
},
{
name: 'Red house',
properties: [{name: 'Area',value: '250'}, {name: 'Year',value: '1999'}]
},
];
Now I'd like to order this list by one of the properties, say Area, using a filter in ng-repeat. Is that possible?
I've been play around with this in plunker (http://plnkr.co/edit/GEgLxv5zJyW0ZBTtJR5S?p=preview) but cannot figure out how to do.
Thanks in advance
This will work if the area is always the first property:
<tr ng-repeat="house in houses | orderBy:'properties[0].value'">
Also make sure that the area is defined as an integer, right now they are strings. So 250 will be sorted before 40.
So remove the quotes from the numbers:
{
name: 'Area',
value: 400
}
, {
name: 'Year',
value: '1950'
}

Resources