I want to get the value of the selected cell in my grid. I have the following codes:
methods:
createPlaylist: function(record){
var scope = this;
var srecords
= getShowImages.getSelectionModel().getSelection();
console.log(srecords);
}
I use the console.log to check if I am getting any value.
grid view:
Ext.ns('dlti.view.widget');
Ext.define('dlti.view.widget.ShowImages' ,{
extend: 'Ext.grid.Panel',
id: 'dlti-grid-images',
alias: 'widget.ShowImages',
forceFit: true,
stripeRows: true,
selType: 'checkboxmodel',
multiSelect: true,
autosync: true,
height: 250,
width: 470,
store: new dlti.store.UploadStore(),
columns: [
{
text: 'Images',
dataIndex: 'imagepath',
renderer: function renderIcon(val) {
return '<img src="' + val + '"width="100px" height="100px" align="middle">';
},
},
{
text: 'Filename',
dataIndex: 'filename',
renderer: function renderDescTarget(val, p, record) {
var desc = '';
desc = '<p style="color:#000;font-size:12px;">' + val + '</p>';
return desc;
}
}
]
});
You need to add a Select listener such as;
selModel: Ext.create('Ext.selection.CellModel', {
allowDeselect: true,
mode: 'SINGLE',
listeners: {
select: {
fn: me.onCellModelSelect,
scope: me
}
}
})
Following that, the function to alert the value of the cell should be something like follows;
onCellModelSelect: function(cellmodel, record, row, column, eOpts) {
/*an if statement is required to parse the column chosen as
record.get() only accepts string values of the column name so
if the columns are named "first", "second", "third", the
following if statement applies*/
var columnName;
if(column === 0){
columnName= 'first';
}else if(column === 1){
columnName= 'Second';
}else if(column === 2){
columnName= 'Third';
}
Ext.Msg.alert('Cell Value',record.get(columnName));
}
});
Hope it works for you. If you want me to send you the whole page of code for this, PM me! <3
You have to listen to gridpanel's cellclick event. In your view, you have to add the following on your gridpanel's config:
listeners : {
cellclick : function(view, cell, cellIndex, record, row, rowIndex, e) {
//handle event
}
}
The grid has a cellclick event you can use:
Fired when table cell is clicked.
EDIT:
In your grid you can add a listener that will be fired each time a cell is clicked (you can add it to the controller instead), like:
listeners : {
cellclick : function(view, cell, cellIndex, record, row, rowIndex, e) {
console.log(cell);
}
}
Related
I have an Ext.grid.Panel which shows several columns. One is a combo box with a static predefined set of selectable values.
On some data entries (columns) I want to disable the combo box. The disabling depends on another column value.
How do I do that?
More background:
After rendering column's cell data in a combo box I am not able to back reference the column's data model within the afterrender-listener.
If I am able to re-reference the data object then I can make a decision on allowing to make a selection from the combo box. Some columns (data tuples) allow selecting a different value from the combo box, others will not.
Background:
Panel's cell editing is turned on. I have one column which is a dropdown with determined values taken from the data received.
The store I am able to reference withing the afterrender method is not the store holding all the table data. It just holds the static Big-Medium-Low data of the column. But I need the store of the row; or better the correct row of data which ist stored in that more global store.
Ext.define( 'MyTable',{
extend: 'Ext.grid.Panel',
xtype:'myXtypeName',
mixins: {
field: 'Ext.form.field.Field'
},
plugins: [
Ext.create('Ext.grid.plugin.CellEditing', { clicksToEdit: 1} )
],
store: Ext.create( 'Ext.data.Store', {
data: [],
autoLoad: false,
} ),
columns: [
{
header: '#',
dataIndex: 'id',
},
{
header: 'Severity',
dataIndex:'correctionSeverity',
sortable: false,
renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {
var myTable = Ext.ComponentQuery.query('myXtypeName');
myTable[0].severityChangeable[ record.id ] = record.data.correctionType == 'Changed';
return value;
},
editor: Ext.create( 'Ext.form.field.ComboBox', {
queryMode: 'remote',
store: Ext.create( 'Ext.data.Store', {
fields: ['id', 'text'],
data: [ { id:'entry0', text:'Big' },
{ id:'entry1', text:'Medium' },
{ id:'entry2', text:'Low' } ],
} ),
valueField: 'id',
displayField: 'text',
editable: false,
forceSelection: true,
listeners: {
afterrender: function(combo, eOpts) {
// how to access the underlaying entry (row data) instead of just the column?
// by another value of the
var rowData = ???;
if ( rowData['preventColumnDropdown'] == true ) {
combo.disable();
}
},
change: function(combo, newValue, oldValue, eOpts) {
// do something
},
}
}),
}
],
initComponent: function() {
this.callParent(arguments);
this.initField();
},
setValue: function(value) {
this.store.loadData( value );
},
});
I hope my point and problem is understandable. Please let me know if not so...
I am running ExtJS 6.0.0 classic.
The afterrender event is the wrong event to use, because the same combobox instance is reused for all rows. Therefore, at combobox instantiation time, the row data is not available.
You may want to use the beforeedit event on your CellEditing plugin to modify the editor before the start of a cell edit:
Ext.create('Ext.grid.plugin.CellEditing', {
clicksToEdit: 1,
listeners: {
beforeedit: function(editor, context, eOpts) {
// Only if the combo column is being edited:
if(context.column.dataIndex == "myComboDataIndex") {
// Disable combobox depending on:
editor.setDisabled(!context.record.get("AllowComboDataEdit"));
// or just return false to stop edit altogether:
// return context.record.get("AllowComboDataEdit");
}
}
}
});
I am using Extjs3.2 nested grid with rowExpander.
In my case everything working fine..
but whenever i click on nth row in child grid the parent grid's nth row is also getting selected.
How to Stop this...Please Help??
My Code
var expander = new Ext.ux.grid.RowExpander({
expandOnDblClick : false,
tpl : new Ext.Template('<div id="NestedGridRow-{id}"></div>'),
renderer: function(v, p, record) {
if (record.get('cmaStatus') == 'G') {
p.cellAttr = 'rowspan="2"';
return '<div class="x-grid3-row-expander"></div>';
}
},
});
expander.on('expand', expandedRow);
function expandedRow(obj, record, body, rowIndex){
//absId parameter for the http request to get the absence details.
//Use Id to give each grid a unique identifier. The Id is used in the row expander tpl.
//and in the grid.render("ID") method.
var row = "NestedGridRow-" + record.get("id");
var id2 = "mygrid-" +record.get("id");
sapid_para = record.get('sapid');
//Create the nested grid.
var gridX = new Ext.grid.GridPanel({
id:'nestedGrid',
store: storenested,
//stripeRows: true,
columns: [
{
header : "CMA Date",
width : 120,
sortable : true,
dataIndex : 'cmaDate',
},
{
header : "Source Model",
width : 120,
sortable : true,
dataIndex : 'sourceModel',
},
{
header : "Remarks",
width : 390,
sortable : true,
dataIndex : 'remarks',
}],
height: 120,
id: id2,
plugins : [editor],
renderTo: row,
stripeRows:true,
listeners: {
render: function(gridX) {
gridX.getView().el.select('.x-grid3-header').setStyle('display', 'none');
},
rowclick : function(grid,rowIndex,e) {
alert(rowIndex);
}
},
});
gridX.render(row);
//Ext.getCmp('grid_lineage').getStore().load({params:{start:0, limit:10}});
storenested.load({params:{start:0, limit:10}});
Do you have the same instance of rowSelectionModel on both grids?
EDIT:
Try to add to both of grids definition:
selModel: new Ext.grid.RowSelectionModel({singeSelect:true}),
I need to display a 'Delete' link against each row in a editorgridpanel.
How do I create this link;as it is not mapped to any particular column in the store?
I tried the following but it does not display the link against the added records:
var sampleRecord = new Ext.data.Record.create([
{mapping: 'organizationId',name:'organizationId', type:'int'},
{mapping: 'name',name:'name', type:'string'},
{mapping: 'address',name:'address' , type:'int'}
]);
var s_grid= new Ext.grid.EditorGridPanel ({
.........
columns: [
{header: 'id', width: 120, sortable: false, dataIndex: 'organizationId'},
{header: 'name',width: 120, sortable: false, dataIndex: 'name'},
{header: 'address', sortable: false,width: 45, dataIndex: 'address'},
{header: '',width: 50, sortable: false, renderer:this.delRenderer }
],
.....
,
delRenderer:function (val, meta, rec, rowIdx) {
var tempStr="<div onclick=\"javascript:Ext.getCmp('" +"s_grid" + "').deAllocate(" + rowIdx + ");\" class='pointer'><span style='color:#0000FF'><u>Delete</u></span></div>";
return tempStr ;
},
deAllocate:function (rowIdx ) {
Ext.getCmp('s_grid').getStore().removeAt(rowIdx);
Ext.getCmp('s_grid').getView().refresh();
}
});
Help is appreciated.
You'd be better off using an ActionColumn. Anyway, you can also wrap a solution around custom element (link...) with the cellclick event. Here's an example showing both methods:
var grid = new Ext.grid.GridPanel({
renderTo: Ext.getBody()
,height: 300
,store: new Ext.data.JsonStore({
fields: ['id', 'name']
,data: [
{id: 1, name: 'Foo'}
,{id: 2, name: 'Bar'}
,{id: 3, name: 'Baz'}
]
})
,columns: [
{dataIndex: 'name'}
,{
xtype: 'actioncolumn'
,icon: 'http://images.agoramedia.com/everydayhealth/gcms/icon_delete_16px.gif'
,handler: function(grid, row) {
grid.store.removeAt(row);
}
}
,{
renderer: function(value, md, record, row, col, store) {
return '<a class="delete-link" href="#delete-record-' + record.id + '">Delete</a>';
}
}
]
,listeners: {
cellclick: function(grid, row, col, e) {
var el = e.getTarget('a.delete-link');
if (el) {
e.preventDefault();
grid.store.removeAt(row);
}
}
}
});
var lastId = 3;
setInterval(function() {
var store = grid.store,
record = new store.recordType({id: ++lastId, name: 'New record #' + lastId}, lastId);
store.add(record);
}, 3000);
Update
And just because I may be completely off topic on your question, I think your code is not working because when you call this:
renderer: this.delRenderer
Your not in a scope where this points to your grid (since it has not even been created at this point...). What you want to do is rather something like this:
var s_grid = new Ext.grid.EditorGridPanel ({
...
columns: [..., {
...
renderer: delRenderer
}]
});
function delRenderer() {
// FYI, `this` will be the column here
...
}
Or put the function inline in the grid definition...
You can change model by adding delete field :
{
name: 'delete',
convert: function () {
return 'delete';
}
You can then add extra column in your grid and check for link click by 'cellclick' event of the grid with some modifications.Here is the working example :
Working Fiddle.
Have a nice day :-)
The dataIndex config is required on your column, so just provide any field there (e.g. the id) and in your renderer function just ignore the value argument (as you are already doing).
I have an EXT grid that has an additional checkbox and radio in the last 2 columns using the renderer. When I check an item in the grid and view the source I am not seeing the checked:checked designation in the HTML so I am having a hard time targeting the element.
When a row is selected I want to be able to check to see if the checkbox is checked, I am doing something like this:
if (document.getElementById("#isFull-"+record['index']+"").checked == true){
var myVar = true;
}
Is there another way I can target this element to see if it is checked?
Here is the full code:
Ext.onReady(function () {
Ext.QuickTips.init();
// Data store
var data = Ext.create('Ext.data.JsonStore', {
autoLoad: true,
fields: ['name', 'market', 'expertise', 'id', 'isFull', 'isPrimary'],
proxy: {
type: 'ajax',
url: '/opsLibrary/getLibraryJson'
}
});
// Selection model
var selModel = Ext.create('Ext.selection.CheckboxModel', {
columns: [{
xtype: 'checkcolumn',
text: 'Active',
dataIndex: 'id'
}],
listeners: {
selectionchange: function (value, meta, record, rowIndex, colIndex) {
var selectedRecords = grid4.getSelectionModel().getSelection();
var selectedParams = [];
// Clear hidden input
$('#selected-libraries').empty();
var record = null;
var myVar = null;
var myVar2 = null;
for (var i = 0, len = selectedRecords.length; i < len; i++) {
record = selectedRecords[i];
// Is full library checked?
myVar = !Ext.fly("isFull-" + data.indexOf(record)).dom.checked;
// Build data object
selectedParams.push({
id: record.getId(),
full: myVar
});
}
// JSON encode object and set hidden input
$('#selected-libraries').val(JSON.stringify(selectedParams));
}
}
});
// Render library grid
var grid4 = Ext.create('Ext.grid.Panel', {
xtype: 'gridpanel',
id: 'button-grid',
store: data,
columns: [{
text: "Library",
width: 170,
sortable: true,
dataIndex: 'name'
}, {
text: "Market",
width: 125,
sortable: true,
dataIndex: 'market'
}, {
text: "Expertise",
width: 125,
sortable: true,
dataIndex: 'expertise'
}, {
text: 'Full',
dataIndex: 'isFull',
width: 72,
renderer: function (value, meta, record, rowIndex, colIndex) {
return '<center><input type="checkbox" onclick="var s = Ext.getCmp(\'button-grid\').store; s.getAt(s.findExact(\'id\',\'' + record.get('id') + '\')).set(\'isFull\', this.value)"'
}
}, {
text: 'Primary',
dataIndex: 'isPrimary',
width: 72,
renderer: function (value, meta, record, rowIndex, colIndex) {
return '<center><input type="radio" id="isPrimary-' + rowIndex + '"/></center>';
}
}, ],
columnLines: false,
selModel: selModel,
width: 600,
height: 300,
frame: true,
title: 'Available Libraries',
iconCls: 'icon-grid',
renderTo: Ext.get('library-grid')
});
});
UPDATED SELECTION MODEL:
// Selection model
var selModel = Ext.create('Ext.selection.CheckboxModel', {
columns: [{
xtype: 'checkcolumn',
text: 'Active',
dataIndex: 'id'
}],
listeners: {
selectionchange: function (value, meta, record, rowIndex, colIndex) {
var selectedRecords = grid4.getSelectionModel().getSelection();
var LastSelectedRecords = grid4.getSelectionModel().getLastSelected();
var selectedParams = [];
// If user unselected ID then make sure Full & Primary boxes cleared
if (grid4.getSelectionModel().getSelection() == "") {
// Get row ID
Ext.fly('isFull-' + LastSelectedRecords['index']).dom.checked = false;
Ext.fly('isPrimary-' + LastSelectedRecords['index']).dom.checked = false;
}
// Clear input and reset vars
$('#selected-libraries').empty();
var record = null;
var myVar = null;
var myVar2 = null;
// Loop through selected records
for (var i = 0, len = selectedRecords.length; i < len; i++) {
record = selectedRecords[i];
// Is full library checked?
myVar = record.get('isFull');
// Is this primary library?
myVar2 = record.get('isPrimary');
// Build data object
selectedParams.push({
id: record.getId(),
full: myVar,
primary: myVar2
});
}
// JSON encode object and set hidden input
$('#selected-libraries').val(JSON.stringify(selectedParams));
console.log(JSON.stringify(selectedParams));
}
}
});
Let's try a different approach. What if you set your check columns to do something like this:
{
text: 'Full',
dataIndex:'isFull',
width: 70,
renderer: function (value, meta, record, rowIndex, colIndex) {
return '<center><input type="checkbox" onclick="var s = Ext.getCmp(\'button-grid\').store; s.getAt(s.findExact(\'id\',\'' + record.get('id') + '\')).set(\'isFull\', this.value)" /></center>';
}
},
This should set the value directly into the record.
Data in records is accessed using the get() function, not object notation:
if (document.getElementById("isFull-"+record.get('index')).checked == true)
{
var myVar = true;
}
A couple other points... You are checking a truth value using == true, which will return true for any "truthie". You'll want to use === if you want to check that it equals true.
Also, you may want to consider Ext.fly() to get your element, it's more Ext-friendly:
if (Ext.fly("isFull-"+record.get('index')).dom.checked === true)
{
var myVar = true;
}
For simplicity, you can even just do this:
var myVar = Ext.fly("isFull-"+record.get('index')).dom.checked;
EDIT:
I was under the impression that you had index as one of your store fields, but I just noticed that it is not. You will want to use store.indexOf(record) to get the index you need:
var myVar = Ext.fly("isFull-" + data.indexOf(record)).dom.checked;
Maybe, you can use one listener on grid view:
Sencha api [4.2]: Listeners on grid/columns
listeners: {
click: {
element: 'el', //bind to the underlying el property on the panel
fn: function(){
console.log('click el');
}
},
I want that the CheckboxModel appear all checked when grid is rendered:
This is my code:
sm = Ext.create('Ext.selection.CheckboxModel', {
listeners: {
selectionchange: function (sm, selections) {
// Must refresh the view after every selection
sm.view.refresh();
}
}
})
The grid:
{
xtype: 'gridpanel',
title: 'gridTitle',
selModel: sm,
store: my_store,
columns: {
items:[
..
]
}
}
You could use afterrender listeners of the grid to select all the rows :
listeners:{
afterrender:function( thisObj, eOpts ){
var sm=thisObj.getSelectionModel();
sm.selectAll(true);
}
},
afterrender may not work, try afterlayout instead:
// in your grid
listeners: {
afterlayout : function (thisObj, eOpts) {
thisObj.getSelectionModel().selectAll();
}
},
// ...