Ag-Grid updating values pushed? - javascript

Made ag-grid, created event for click to add that clicked data (and some manipulations of it) to a second grid.
Values get pushed, when checking row data, but does not display.
How can I make the second table update ? Code below, please & Thank you!!
<div id="myGrid" style="height: 1000px; width:1000px;" class="ag-theme-alpine-dark"></div>
<div id="mySecondGrid" style="height: 100rem;width:100rem;" class="ag-theme-alpine-dark"></div>
<script type="text/javascript" charset="utf-8">
// specify the columns for first table
var columnDefs = [
{headerName: "Col1", field: "Col1"},
{headerName: "Col2", field: "Col2"},
{headerName: "Col3", field: "Col3"}
];
// specify the data for first table
var rowData = [
{ Col1:"25",Col2:"7.25",Col3:"1"},
{ Col1:"5",Col2:"7.5",Col3:"3"},
{ Col1:"13",Col2:"7.75",Col3:"7"},
];
// grid options first table
var gridOptions = {
columnDefs: columnDefs,
rowData: rowData,
rowSelection: 'single',
suppressMovableColumns:true,
};
// lookup the container we want the Grid to use
var eGridDiv = document.querySelector('#myGrid');
// create the grid passing in the div to use together with the columns & data we want to use
new agGrid.Grid(eGridDiv, gridOptions);
//add Listener
gridOptions.api.addEventListener('rowClicked', myRowClickedHandler);
//Variable to hold data for second grid
var a = [ ];
var columnDefs2 = [
{headerName: "TIMESCLICKED", field: "TimesClicked"},
{headerName: "ATT1", field: "Att1"},
{headerName: "ATT2", field: "Att2"}
];
var gridOptions2 = {
columnDefs: columnDefs2,
suppressMovableColumns:true,
rowData: a,
}
var eGridDiv2 = document.querySelector('#mySecondGrid');
new agGrid.Grid( eGridDiv2, gridOptions2);
//should update second grid by passing data from items clicked on in first grid not updating ?
function myRowClickedHandler(event) {
a.push({TimesClicked: 1, Att1: gridOptions.api.getSelectedNodes()[0].data.Col1, Att2: gridOptions.api.getSelectedNodes()[0].data.Col2})
}
</script>

You should set the row data --> gridOptions2.api.setRowData(a) after you pused the object to your 'a' variable.
https://plnkr.co/edit/mPJQpms0X6PlqRRy

Related

Tabulator: how to modify local array when deleting rows?

I'm trying to build an interactive table that could be modified by the user. In my case, the original dataset is a local array of objects.
Tabulator has the buttonCross option to delete rows, but it only affects the table visuals. How can I make it find the matching object the row presents and delete it from the tabledata array?
Here's the code I'm working with:
let tabledata = [{
animal: "hippo",
color: "grey"
},
{
animal: "puma",
color: "black"
},
{
animal: "flamingo",
color: "pink"
}
];
let table = new Tabulator("#example-table", {
data: tabledata,
layout: "fitDataFill",
addRowPos: "bottom",
reactiveData: true,
headerSort: false,
columns: [ //define the table columns
{
title: "animal",
field: "animal",
editor: "input"
},
{
title: "color",
field: "color",
editor: "input"
},
{
formatter: "buttonCross",
width: 40,
align: "center",
cellClick: function (e, cell) {
cell.getRow().delete();
}
},
],
});
Codepen here.
Would really appreciate some tips on how to make this work!
Working example for tabulator
the filter function is used to remove the current item for the collection
filter Filter API.
First filter the object you don't need and then assign it to tabledata.
cellClick: function (e, cell) {
debugger;
var animalToDelete={ animal:cell.getRow().getData().animal,
color:cell.getRow().getData().color
};
var filtered=tabledata.filter(function(x){
debugger;
return x.animal!=animalToDelete.animal
&& x.color!=animalToDelete.color;
});
tabledata=filtered;
cell.getRow().delete();
}
You could also look to use tabulator in Reactive Data Mode
In this mode it will update the table in real time to match the provided data array as it is changed, and vice versa it will update the array to match the table.
To do this set the reactiveData property to true in the tables constructor object.
//create table and assign data
var table = new Tabulator("#example-table", {
reactiveData:true, //enable reactive data
data:tableData, //assign data array
columns:[
{title:"Name", field:"name"},
{title:"Age", field:"age", align:"left", formatter:"progress"},
{title:"Favourite Color", field:"col"},
{title:"Date Of Birth", field:"dob", sorter:"date", align:"center"},
]
});
It will then maintain a link with the initial data source

Updating Columns Dynamically - Alloy UI

I'm trying to change columns dynamically in my Alloy UI DataTable - depending on what button is selected, columns are changed depending on which data is returned.
My columns get updated, however the actual data is never included in the table. When I don't define any columns both the columns and data are returned - I of course want control of how my columns are displayed and want to set their attributes
Below is my code:
var dataTable = new Y.DataTable({ //Defining Datatable with no columns preset
editEvent: 'dblclick',
plugins: [{
cfg: {
highlightRange: false
}]
});
button.on(
'click', //On Click...
function() {
var category = $(this).attr("id"); //value retrieved from id of button selected
dataSource = new Y.DataSource.IO({source: '/searchMyData
dataSource.sendRequest({
dataType: 'json',
on: {
success: function(e) {
response = e.data.responseText;
setColumnNames(category); //Set the Columns...
data = Y.JSON.parse(response);
dataTable.set('data', data);//Then the Data
dataTable.render('#my-container');
},
failure: function() {
alert(e.error.message);
}
}
});
function setColumnNames(tabName){ //Defining Columns
var columns1 = [
{ key: 'id', label: 'ID', width: '70px' },
{ key: 'name', label: 'Name', width: '70px' }
];
var columns2 = [
{ key: 'id', label: 'ID', width: '70px' },
{ key: 'addr', label: 'Address', width: '70px' }
];
switch (category) {
case "person":
dataTable.set('columns', columns1);
break;
case "address":
dataTable.set('columns', columns2);
break;
default:
console.log('');
}
There's no issue with the data returning from the ajax request, only when it comes to loading it to the table with a new set of columns defined. I've tried the reset() method on both columns and data on each click, but no luck.
It turns out the keys returned from my request were being capitalized and included underscores (just how they're defined in the database) - I've also noticed defining the columns key is case sensitive. If I changed a single character from lower case to upper than the column would not display data.

Does ng-grid supports cell filter (angular filter) by row

ng-gird colDefs (column Definition) allows you to define a filter (angular filter) for each column.
In my use case, I need to use filter per row. This means, data in some rows will have number format and other might have percentage.
Does ng-grid supports filter per row? Please note, this is not filtering rows, this is applying same display format to the cells of a row.
This is now (as of 2014) possible natively with the current ngGrid:
columnDefs: [
{
field: 'name',
cellFilter: 'lowercase'
}
]
Or even with the filter arguments:
columnDefs: [
{
field: 'name',
cellFilter: 'currency:"GPB"'
}
]
You can do this by using a cellTemplate and a filter.
The gridOptions with custom cellTemplate:
var statusTpl = '<div style="padding-top: 10px;padding-left: 5px" ng-bind="row.getProperty(col.field) | percentage:200"></div>';
$scope.gridOptions = {
data: 'myData',
columnDefs: [{
field: 'name'
}, {
field: 'status',
cellTemplate: statusTpl
}]
};
And a very simple Filter (which, in this example, calculates floored percentage from 100%=200):
app.filter('percentage', function() {
return function(input, max) {
if (isNaN(input)) {
return input;
}
return Math.floor((input * 100) / max) + '%';
};
});
You can set the 100% value in the second parameter of the filter in the custom cellTemplate.
If you need a more precise with adjustable decimals just look around you will surely find some.
Doh! Nearly forgot the Plunker to this

Extjs: only show checkbox when another field has a value

i have a grid with a checkboxcolumn, all works fine but i would like to only show the checkbox if another field has a certain value. I work with version 3.3.1 but i guess that an example from another version would get me started.
If not possible, disabling the checkbox would also be fine.
Do i have to do that in a renderer or a listener and how ?
var checkColumn = new Ext.grid.CheckColumn({
header: 'Checklist OK ?',
dataIndex: 'checklist_ok',
width: 20,
align: 'center'
});
cmDiverse = new Ext.grid.ColumnModel({
defaults: {"sortable": true, "menuDisabled":false, "align":"right"},
store: storeDiverse,
columns: [
{"id":"id", "header": "id", "hidden": true, "dataIndex": "id", "width": 20},
checkColumn,
...
gridDiverse = new Ext.ux.grid.livegrid.EditorGridPanel({
id : "gridDiverse",
enableDragDrop : false,
loadMask : true,
clicksToEdit : 1,
layout :'anchor',
cm : cmDiverse,
....
You can extend your Ext.ux.grid.livegrid.EditorGridPanel like this:
Ext.extend(Ext.ux.grid.livegrid.EditorGridPanel,{
constructor:function(config){
config = Ext.apply({
cm: this.createColumnModel()
},config);
},
createColumnModel: function(){
PUT YOUR LOGIC HERE AND RETURN AN ARRAY OF COLUMNS...
}
})
Found it myself, added the following renderer to checkColumn
renderer : function(v, p, record){
var type3m = record.get('type3m');
if ((['6M','11e']).indexOf(String(type3m)) != -1){ //if the field type3m contains certain values
p.css += ' x-grid3-check-col-td';
return '<div class="x-grid3-check-col'+(v?'-on':'')+' x-grid3-cc-'+this.id+'"> </div>';
}
}

DataTables add object data that is not a column to row?

I can't figure this one out, their API is very confusing.
I have a table that displays tabular data of geometric features, so I want to have the coordinates saved in the row data.
And I need a way to retrieve this coordinates, for example, when a user clicks on the row.
Example of what I'm trying to achieve :
const dt = $('table').DataTable({
columns : [
{data : "NAME"},
{data : "COLOR"}
]
})
dt.row.add({
COLOR : "Red",
NAME : "Point1",
//Invalid parameter error
geometry : {
type : "point",
coordinates : [1,1]
}
})
I think it wouldn't be ideal storing in a hidden column, because the coordinates can get quite large for polygon types.
I'm very new to DataTables and am very confused with the API, any suggestion on how to better organize/execute my concept is more than welcome.
Seems like what you have should work. I put together a little example for you:
http://live.datatables.net/rikofefu/1/edit
It adds a row with the extra geometry object. Select the row then click the Show selected data button. The console will display the full row and just the geometry object.
I'm using the Select extension to get the selected row.
HTML
<table>
<thead>
<tr>NAME</tr>
<tr>COLOR</tr>
<tr></tr>
</thead>
</table>
JS
const dt = $('table').DataTable({
columns: [
{data: null},
{data: null},
{data: null}
],
// This makes the row clickable, where you can get data later
"createdRow": function (row, data, dataIndex) {
var tds = $(row).find("td");
$.each(tds, function (key, value) {
$(value).attr("onclick", "see_the_data(this)");
}
},
// this hides the coordinate column from view
"columnDefs": [
{
"targets": 2,
"visible": false
}
]
});
dt.row.add({
COLOR: "Red",
NAME: "Point1",
//Invalid parameter error
geometry: {
type: "point",
coordinates: [1, 1]
}
});
function see_the_data(e) {
console.log("the data is ", e); // you can then go further as you need
}

Categories

Resources