How can I filter a react-table based on the condition that the row is selected - react-table

I have a button in my table component which when clicked I'd like to apply a filter on the data to look at the rows and if the isSelected property is true then display only those rows. I have a custom filter which I attach to the ID column ...
const selectedRowFilter = (rows, ids, filterValue) => {
if(filterValue) {
return rows.filter((row) => {
return row.isSelected;
});
}
return rows;
};
const columns = React.useMemo(
() => [
{
Header: 'ID',
accessor: 'id',
filter: 'selectedRowFilter',
}, etc...
I then call the filter programatically on click of a button using setFilter ...
setFilter('id', true);
The issue I have is when the table is reloaded when I edit the data for example the filter is re applied but the row.isSelected values are at this point undefined. Any help appreciated. This could be an issue due to the useRowSelected plugin having to be after the useFilters plugin.
https://codesandbox.io/s/new-breeze-uzhv8

Related

How to implement sorting in EXTJS Grid

I am facing a problem in extjs grid . I have two grid column one is name and another is displayName but I want to sorting by name in both column means I displayName as well as name. When user apply sort in both column parameters pass by grid is sort= name in both case.
[
{
header: 'Name',
dataIndex: 'name',
shortable: true
},
{
header: 'DisplyName',
dataIndex: 'displayName',
shortable: true
}
]
IMO, you can do that in beforesort event handle function of grid's store.
Like this:
// here is the key point
listeners: {
beforesort: function(st, sorters) {
console.log(sorters, st);
if (sorters.length == 1 && sorters[0].property == 'displayName') {
sorters[0].property = 'name';
}
}
}
// here is the key point end
a live example

How to find the selected row index of a grid in Dojo

I am relatively new to Dojo. I am using a DataGrid, where in I have a textbox in one of the columns. I want to know the row index of the grid when the user enters some data in the textinput of that particular row.
'name' : 'Partner Column Name',
'field' : 'text',
'width' : '25%',
'field' : 'partnerColumnName',
'text-align': 'center',
'editable' : true,
'type' : dojox.grid.cells._Widget,
'formatter' : function()
{
return new dijit.form.TextBox({style:"width:100%", id: "textBox_"+counter++, onChange: function ()
{
// Here I want to know the row index of the grid.
}
Can someone help me in this respect.
Thanks,
Nirmal Kumar Bhogadi
When viewing the code of the datagrid (dojox/grid/cells/_Base to be precise) I noticed that the formatter callback gets two parameters:
The original value
The row index (inRowIndex)
So based on that you can easily retrieve the row index, for example:
formatter: function(myValue, rowIndex) {
// Do something with rowIndex
}
I think you need the whole selected row data of your grid? This is what I use in my project:
var grid = dijitRegistry.byId('yourGridId');
if (!grid || !grid.selection || grid.selection.getSelected()) {
console.log(grid.selection.getSelected());
}

ExtJS4 - Iterate through Store in Grid

I want to iterate through store and make grid columns dynamically. When I use columns.push method in the onLoad event of Store, I got this error: "headerCtCfg is undefined". This is my code:
Ext.define('App.view.UserList' ,{
extend: 'Ext.grid.Panel',
alias : 'widget.userlist',
store: 'Users',
initComponent: function() {
var store = Ext.getStore( this.store );
store.on('load', function () {
var columns = this.columns = [];
columns.push( {header: 'H', dataIndex: '0'} );
Thank you in Advance
Try to add this.callParent(arguments) before doing anything else in your initComponent function. This way view gets completely created and you will be able to access columns.
Also I suggest to look at reconfigure method of tableview to change store and columns on the fly.
Update: Try to use Ext.apply instead of columns.push:
Ext.apply(this, {
columns: [ list of your columns ]
});
Update2: load() is async. So you might consider creating grid first with dummy column, then load store and replace it with new data.

How to add a row hyperlink for an extJS Grid?

Can someone please throw some light on how to go about rendering an hyperlink in the cells of a particular column in ExtJS?
I have tried binding the column to a render function in my JS, from which I send back the html:
SELECT
However, with this, the problem is that, once I hit the controller through the link, the navigation is successful, but subsequent navigations to the data-grid show up only empty records.
The records get fetched from the DB successfully through the Spring MVC controller, I have checked.
Please note that this happens only once I use the row hyperlink in the extJS grid to navigate away from the grid. If I come to the grid, and navigate elsewhere and again come back to the grid, the data is displayed fine.
The problem only occurs in case of navigating away from the grid, using the hyperlink rendered in one/any of the cells.
Thanks for your help!
This is for ExtJS 4 and 5.
Use a renderer to make the contents look like a link:
renderer: function (value) {
return ''+value+'';
}
Then use the undocumented, dynamically generated View event cellclick to process the click:
viewConfig: {
listeners: {
cellclick: function (view, cell, cellIndex, record, row, rowIndex, e) {
var linkClicked = (e.target.tagName == 'A');
var clickedDataIndex =
view.panel.headerCt.getHeaderAtIndex(cellIndex).dataIndex;
if (linkClicked && clickedDataIndex == '...') {
alert(record.get('id'));
}
}
}
}
Try something like this:
Ext.define('Names', {
extend: 'Ext.data.Model',
fields: [
{ type: 'string', name: 'Id' },
{ type: 'string', name: 'Link' },
{ type: 'string', name: 'Name' }
]
});
var grid = Ext.create('Ext.grid.Panel', {
store: store,
columns: [
{
text: 'Id',
dataIndex: 'Id'
},
{
text: 'Name',
dataIndex: 'Name',
renderer: function (val, meta, record) {
return '' + val + '';
}
}
...
...
...
However my thanks to - ExtJS Data Grid Column renderer to have multiple values
Instead of using an anchor tag, I would probably use plain cell content styled to look like an anchor (using basic CSS) and handle the cellclick event of the GridPanel to handle the action. This avoids dealing with the anchor's default click behavior reloading the page (which is what I'm assuming is happening).
I created a renderer so it looked like you were clicking on it.
aRenderer: function (val, metaData, record, rowIndex, colIndex, store){
// Using CellClick to invoke
return "<a>View</a>";
},
But I used a cell event to manage the click.
cellclick: {
fn: function (o, idx, column, e) {
if (column == 1) // Doesn't prevent the user from moving the column
{
var store = o.getStore();
var record = store.getAt(idx);
// Do work
}
}
}
For these purposes I use CellActions or RowActions plugin depending on what I actually need and handle cell click through it.
If you want something that looks like an anchor, use <span> instead and do what #bmoeskau suggested.
You can use 'renderer' function to include any HTML you want into cell.
Thanks guys for your response.
AFter debugging the extJS-all.js script, I found the issue to be on the server side.
In my Spring MVC controller, I was setting the model to the session, which in the use-case I mentioned earlier, used to reset the "totalProperty" of Ext.data.XmlStore to 0, and hence subsequent hits to the grid, used to display empty records.
This is because, ext-JS grid, checks the "totalProperty" value, before it even iterates through the records from the dataStore. In my case, the dataStore had data, but the size was reset to null, and hence the issue showed up.
Thanks to all again for your inputs!

How to update a Dojo Grid cell value using a TooltipDialog (and DropDownButton)

I have a dojo grid which is using some editable dijit form fields. All is well, until I try ot implement an country (multi) select cell as an Tooltip Dialog; i.e., show a drop down button which opens the tooltip dialog populated with a checkbox array to select one or more country. Once checked and clicked OK, the cell should update with a list of selected countries. Obviously I'll take care of updating the server via the store later on.
I've implemented a country select tooltip dialog which works fine like so:
dojo.provide("CountrySelector");
dojo.declare(
"CountrySelector",
[dijit.form.DropDownButton],
{
label: 'Countries',
dropDown: new dijit.TooltipDialog({ execute: function() {
console.log("EXECUTE : ", arguments[0]);
this.value = arguments[0].country;
}, href:'/cm/ui/countries' }),
postCreate: function() {
this.inherited(arguments);
this.label = this.value;
dojo.connect(this.dropDown, 'onClose', function() { console.log('close'); });
console.log("CountrySelect post create", this);
},
}
);
And the grid cell is typed as:
{ name: 'Countries', field: 'targeting.countries', editable: true, hidden: false, type:dojox.grid.cells._Widget, widgetClass: CountrySelector },
All is working fine but I can't figure out how to update cell's content and store once the widget is executed. As well, I don't seem to have the row id of the updated row.
Any ideas?
Thanks,
Harel
//Layout:
gridLayout: {rows: [{name: 'Coll Name',field: 'colField', type: dojox.grid.cells.ComboBox, editable:'true', width:'8%',options: [], alwaysEditing:false}]}
//Grid Store:
this.gridStore = new dojo.data.ItemFileReadStore({data: {items: data}});
//
var setOptions = function(items, request){
this.gridLayout.rows[0].options.push('Val 1','Val 2');
this.gridLayout.rows[0].values.push('1','2');
dojo.connect(this.gridLayout.rows[0].type.prototype.widgetClass.prototype, "onChange",this, "_onComboChange");
}
this.gridStore.fetch({onComplete: dojo.hitch(this,setOptions)});
_onComboChange: function (selectedOption) {
console.info("_onComboChange: ",selectedOption);
},
// If you need to populate combos with different values you can use onItem
var getArray = function(item, request){
// populate one by one
// attach an event to each combo
}
this.gridStore.fetch({onItem: dojo.hitch(this,getArray)});
This is what i used to update my grid
var idx = yourGrid.getItemIndex(item);
if (idx >- 1) {
yourGrid.updateRow(idx);
}
More detail
every row is identified by its identifier
yourGrid.store.fetchItemByIdentity({
identity: <yourIdentity>,
onItem: function(item){
// Update your attributes in the store depending on the server response
// yourGrid.store.setValue(item, <attribute>,<value>);
var idx = yourGrid.getItemIndex(item);
if (idx >- 1) {
yourGrid.updateRow(idx);
}
}
});
I didn't set up a test with your code but you should be able to do it by just creating a method named getValue in your widget that returns the value.
Take a look at the other examples (like dojox.grid.cells.ComboBox) to get an idea of what getValue should look like.

Resources