I need to get selected record from extjs grid but get
Uncaught TypeError: grid.getSelectionModel is not a function
got this view
Ext.define( 'App.view.Measuries', {
extend: 'Ext.window.Window',
alias : 'widget.measuries',
title: 'Мероприятия',
autoShow: false,
modal: true,
resizable: true,
initComponent: function()
{
this.items = [
{
xtype: 'grid',
alias: 'measuries_grid',
bodyPadding: 8,
defaults: { width: 1000, labelAlign: 'top' },
columns:[
{ header: 'п/п', width: 60, dataIndex: 'm_number', menuDisabled: true, sortable: false},
{ header: 'Мероприятие', width: 340, dataIndex: 'm_text', menuDisabled: true, sortable: false},
{ header: 'Плановая дата выполнения', width: 146, dataIndex: 'date_plan', menuDisabled: true, sortable: false, xtype: 'datecolumn', format:'d.m.Y', align: 'center'},
{ header: 'Фактическая дата выполнения', width: 146, dataIndex: 'date_done', menuDisabled: true, sortable: false, xtype: 'datecolumn', format:'d.m.Y', align: 'center'},
{ header: 'Выполнено', width: 95, dataIndex: 'isdone', menuDisabled: true, sortable: false, align: 'center',
renderer: function ( v )
{
return '<img src="img/' + v.toString().toLowerCase() + '.png">';
}},
{ header: 'Отказ', width: 95, dataIndex: 'faultid', menuDisabled: true, sortable: false},
],
store : 'Measuries'
}
];
and this controller (well a part of it)
editMeasuries: function()
{
var grid = this.getMeasuries();
console.log(grid);
var rec = grid.getSelectionModel().getLastSelected();
/*var sel = grid.getSelectionModel();
var rec = sel.getLastSelected();*/
if ( rec )
{
var view = Ext.widget( 'measuries-edit' );
view.down( 'form' ).loadRecord( rec );
}
},
it seems trouble with "this.getMeasuries()" part. My "alias : 'widget.measuries'" go to 'Ext.window.Window' and getSelecionModel() requires a grid. I've tried to set alias to grid item inside 'Ext.window.Window' but got error too.
How i can change my controller part to get things right?
Sorry for possible mistakes, my english is weak so as extjs.
Set an itemId for the grid and use getComponent on your window to get your grid:
this.items = [
{
itemId: 'myGrid', // add this line
xtype: 'grid',
...
Then in your 'editMeasures` function do like this:
editMeasuries: function()
{
var myWindow = this.getMeasuries();
var myGrid = myWindow.getComponent('myGrid');
...
Instead of using itemId you might want to use references, which is the faster way of handling items in the viewController.
(I am taking the code from #Peter Koltai)
this.items = [{
reference: 'myGrid', // add this line
xtype: 'grid',
...
Then in your 'editMeasures` function do like this (you do not need myWindow, it's just there to demonstrate a different approach):
editMeasuries: function()
{
let myWindow = this.getView(),
myGrid = this.lookup('myGrid');
...
Related
I have an EXT JS 4.2 Grid that has 2 columns that use the renderer to place checkboxes in 1 column and radio buttons in another. How can I auto increment the ID's on these HTML inputs so that I can specifically target them via EXT JS (see columns 'Full' and 'Primary')
// 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', renderer: function() {
var rec = grid4.getStore().getAt(this.rowIndex);
return "<input type='checkbox' id='"+rec+"' />";
}},
{text: 'Primary', dataIndex:'isPrimary', renderer: function() {
return "<input type='radio' />";
}},
],
columnLines: false,
selModel: selModel,
// inline buttons
dockedItems: [{
xtype: 'toolbar',
dock: 'bottom',
ui: 'footer',
layout: {
pack: 'center'
},
items: []
}, {
xtype: 'toolbar',
items: []
}],
width: 600,
height: 300,
frame: true,
title: 'Available Libraries',
iconCls: 'icon-grid',
renderTo: Ext.get('library-grid')
});
UPDATE:
The ID's are now incrementing, thank you!
Now, I have one other question:
I am not seeing the checked:checked flag being set when I check an item in the EXT grid, how would I do something like the code below. I want to check to see if the element is checked
if(document.getElementById("#isFull-"+record['index']+"").checked == true){
var myVar = true;
}
The renderer takes a bunch of arguments automatically, one of which is the rowIndex. You should be able to do this to give the unique ID you want:
{
text: 'Full',
dataIndex:'isFull',
renderer: function(value, meta, record, rowIndex, colIndex)
{
return '<input type="checkbox" id="isFull-' + rowIndex + '" />';
}
}
See more here: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.grid.column.Column-cfg-renderer
I'm pasting my updated code here. Please have a look and let me know what mistake I'm doing here. In the form itself I'm displaying the grid too.
So when I click on the record to edit, I need to populate the form where I can edit the corresponding record details and update into DB.
var grid = new Ext.grid.GridPanel({
frame: true,
store: store,
selModel: sm,
enableHdMenu: false,
stripeRows: true,
autoScroll: true,
trackMouseOver: true,
disableSelection: false,
loadMask: true,
enableColumnMove: false,
height: 350,
width: 960,
title: 'Edit Contractor Details',
columns: [{
header: "Requestor Name",
width: 75,
sortable: true,
dataIndex: 'requestorname'
}, {
header: "Start Date",
width: 85,
sortable: true,
xtype: 'datecolumn',
dataIndex: 'startdate',
dateFormat: 'n/j/Y'
}, {
header: "Supervisor",
width: 75,
sortable: true,
dataIndex: 'supervisor'
}, {
header: "Company",
width: 100,
sortable: true,
dataIndex: 'company'
}, {
header: "Last Name",
width: 75,
sortable: true,
dataIndex: 'lastname'
}, {
header: "First Name",
width: 75,
sortable: true,
dataIndex: 'firstname'
},
sm
],
tbar: [{
iconCls: 'contractor-add',
text: 'Edit Employee',
handler: function(grid, rowIndex, colIndex) {
alert("test");
var editWindow = new Ext.Window({
id: 'id_editWindow',
title: 'Edit',
closable: true,
width: 750,
height: 380,
plain: true,
layout: 'fit',
items: simple,
listeners: {
show: function EditUser(rec) {
var formpanel = Ext.getCmp('formPanel');
formpanel.getForm().loadRecord(rec);
}
}
});
editWindow.show();
}
}],
// customize view config
viewConfig: {
forceFit: true,
enableRowBody: true,
showPreview: true,
getRowClass: function(record, Index) {}
},
// paging bar on the bottom
bbar: new Ext.PagingToolbar({
store: store,
displayInfo: true,
displayMsg: 'Displaying Records {0} - {1} of {2}',
emptyMsg: "No cases to display"
})
});
//Form Creation
var simple = new Ext.form.FormPanel({
standardSubmit: true,
frame: true,
xtype: 'form',
// id: 'tabForm',
id: 'formPanel',
bodyPadding: 5,
width: 1000,
border: false,
bodyBorder: false,
fieldDefaults: {
labelWidth: 75,
msgTarget: 'side'
},
items: {
xtype: 'tabpanel',
id: 'tabpanel',
activeTab: 0,
defaults: {
bodyPadding: 10,
layout: 'anchor'
},
items: [{
title: 'New Contractor Details',
defaultType: 'textfield',
defaults: {
anchor: '100%'
},
items: [{
//Requestor Info
xtype: 'fieldset',
title: 'Requestor Details',
defaults: {
width: 150
},
fieldDefaults: {
msgTarget: 'side',
labelWidth: 300
},
defaultType: 'textfield',
layout: 'column',
labelWidth: 150,
defaults: {
anchor: '100%'
},
items: [{ // column #1
xtype: 'container',
columnWidth: .5,
layout: 'form',
items: [{
xtype: 'textfield',
fieldLabel: 'Requestor Name',
afterLabelTextTpl: required,
labelStyle: 'color:#000000',
name: 'requestorname',
id: 'requestorname',
//hasfocus:true,
anchor: '80%',
allowBlank: false,
listeners: {
afterrender: function(field) {
field.focus(false, 1000);
}
}
}, {
title: 'Manage Contractor Details',
// renderTo: bd,
// activeTab: 0, // first tab initially active
defaults: {
bodyPadding: 10
},
items: [grid]
}]
}],
buttons: [{
text: 'Submit',
formBind: true,
handler: function() {
var requestorname = Ext.getCmp('requestorname').getValue();
var form = Ext.getCmp('formPanel').getForm();
if (form.isValid()) {
//alert('Tests');
Ext.Ajax.request({
url: 'NewHireDetailsInsertion?firstname=' + firstname + '&' + 'middlename=' + middlename + '&' + 'lastname=' + lastname +
'&' + 'suffix=' + suffix + '&' + 'company=' + company + '&' + 'requestorname=' + requestorname + '&'...so on...
method: 'POST',
submitEmptyText: false
});
simple.getForm().getEl().dom.action = 'NewHireDetailsInsertion';
//Ext.Msg.maxWidth =2000;
Ext.Msg.alert('Message', "New Hire information Submitted");
simple.getForm().reset();
} else {
Ext.Msg.alert('Message', "Please enter all required fields");
}
}
}, {
text: 'Reset',
handler: function() {
simple.getForm().reset();
}
}]
}]
}]
});
});
simple.render('mydiv');
You could have provided a bit more sample code, as well as ensured that the code you provided was formatted correctly... As so, the example I'm providing should guide you in the right direction if I understand what you are trying to achieve.
It is very messy to put logic into your views, which is what I see you are doing in the code you have provided. The role of the controller in ExtJS MVC is to observe the events fired by views (or other controllers!) and react accordingly.
You can save yourself a lot of time and headache by establishing reference's (refs) on the Controller for commonly accessed components. In your scenario, both your Grid and Window would make great candidates for references.
Have a look at the Controller documentation for a more thorough explanation of ref capabilities, as well as the Controller's control function: http://docs.sencha.com/extjs/4.1.3/#!/api/Ext.app.Controller
Ext.define('My.controller.Grid', {
extend: 'Ext.app.Controller',
/** List required views, models, stores here */
refs: [
{
name: 'grid',
selector: '',
xtype: 'my-grid' // Defined in the Grid's config i.e., alias: 'widget.my-grid'
},{
name: 'editWindow',
selector: '',
xtype: 'my-edit-window',
autoCreate: true // Automatically create this if it isn't already created
}
],
init:function(app){
/** Observe your Views by their itemId or any valid ComponentQuery selector */
'#my-grid-panel': {
itemdblclick: { fn: this.onGridRecordDoubleClick, scope: this }
},
'#my-edit-window': {
afterrender: { fn: this.editWindowDidRender, scope: this },
close: { fn: this.onWindowClose, scope: this }
}
},
onGridRecordDoubleClick: function(grid, record, element, index, event){
/** save a reference to the selected record on the controller instance */
this.selectedRecord = record;
/** Show the 'Edit' Window if possible */
if(!this.activeWindow)
this.getEditWindow(); // Magic method created by Controller Reference
},
editWindowDidRender: function(view){
/** Get the Form by its itemId */
var myForm = view.down('#my-form');
/** Load the selected record into the form */
myForm.loadRecord(this.selectedRecord);
/** Save a reference to the window, why not? */
this.activeWindow = view;
},
onWindowClose: function(){
/** remove the reference to the window and selected record */
this.activeWindow = undefined;
this.selectedRecord = undefined;
/** Clear selection on the grid */
this.getGrid().getSelectionModel().clearSelections();
this.getGrid().reconfigure();
}
});
I gave it a try but it did not worked.
I am having two grids and a button. Initially the second grid will remain empty and the first grid will have some records.. When I select a few records in the first grid and click on the button, then the second grid should get populated with the only the selected rows of first grid.
Here is my code:
Ext.QuickTips.init();
var getLocalStore = function() {
return Ext.create('Ext.data.ArrayStore', {
model: 'Company',
data: Ext.grid.dummyData
});
};
var getSelectedStore = function() {
return Ext.create('Ext.data.ArrayStore', {
model: 'Company'
});
};
var sm = Ext.create('Ext.selection.CheckboxModel');
var grid1 = Ext.create('Ext.grid.Panel', {
id: 'grid1',
store: getSelectedStore(),
columns: [
{text: "Company", width: 200, dataIndex: 'company'},
{text: "Price", renderer: Ext.util.Format.usMoney, dataIndex: 'price'},
{text: "Change", dataIndex: 'change'},
{text: "% Change", dataIndex: 'pctChange'},
{text: "Last Updated", width: 135, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
],
columnLines: true,
width: 600,
height: 300,
frame: true,
title: 'Framed with Checkbox Selection and Horizontal Scrolling',
iconCls: 'icon-grid',
renderTo: 'grid1'
});
var grid2 = Ext.create('Ext.grid.Panel', {
id: 'grid2',
store: getLocalStore(),
selModel: sm,
columns: [
{text: "Company", width: 200, dataIndex: 'company'},
{text: "Price", renderer: Ext.util.Format.usMoney, dataIndex: 'price'},
{text: "Change", dataIndex: 'change'},
{text: "% Change", dataIndex: 'pctChange'},
{text: "Last Updated", width: 135, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
],
columnLines: true,
width: 600,
height: 300,
frame: true,
title: 'Framed with Checkbox Selection and Horizontal Scrolling',
iconCls: 'icon-grid',
renderTo: 'grid'
});
Ext.widget('button', {
text: 'Click Me',
renderTo: 'btn',
listeners: {
click: function(this1, evnt, eOpts ){
var records = sm.getSelection();
getSelectedStore().loadData(records,true);
grid1.getView().refresh();
/*Ext.each(records, function (record) {
alert(record.get('company'));
});*/
}
}
});
Please let me what's going wrong.
First, you are defining the functions getSelectedStore and getLocalStore which return new store instances when invoked. That way in your click handler you would be grabbing an empty store each time! Lose the function bit and just set variables like this:
var storeToSelectFrom = Ext.create('Ext.data.ArrayStore', {
model: 'Company',
data: someDataToChooseFrom
});
var storeToPutTo = Ext.create('Ext.data.ArrayStore', {
model: 'Company'
});
Then, define your grids using those variables as the stores:
var grid1 = Ext.create('Ext.grid.Panel',{
store: storeToSelectFrom,
selType: 'checkboxmodel'
// rest of your configs
});
var grid2 = Ext.create('Ext.grid.Panel',{
store: storeToPutTo
// rest of your configs
});
Then, create the button with a click handler:
Ext.widget('button', {
handler: function (button, event) {
var selected = grid1.getSelectionModel().getSelection();
grid2.getStore().add(selected);
}
// rest of your configs
});
I am trying to edit an information using editor grid. I have three fields, Interview By (combo), Date (date) and Performance (number), I get the date and the performance column, but the combo is not displaying the value initially. But when I click, then it shows the correct value. I am new to extjs and googled it for a solution, but could not find it. Kindly help me with a solution. Thanks in advance.
MY CODE:
initComponent: function() {
this.createTbar();
this.columns = [
{ xtype:'numbercolumn',
hidden:true,
dataIndex:'interview_id',
hideable:false
},
{ xtype: 'gridcolumn',
dataIndex: 'interview_by_employee_id',
header: 'Interview By',
sortable: true,
width: 290,
editor: {
xtype: 'combo',
store: employee_store,
displayField:'employee_first_name',
valueField: 'employee_id',
hiddenName: 'employee_first_name',
hiddenValue: 'employee_id',
mode: 'remote',
triggerAction: 'all',
forceSelection: true,
allowBlank: false ,
editable: false,
listClass : 'x-combo-list-small',
style: 'font:normal 11px tahoma, arial, helvetica, sans-serif'
},
renderer: function(val){
index = employee_store.findExact('employee_id',val);
if (index != -1){
rs = employee_store.getAt(index).data;
return rs.employee_first_name;
}
}
},
{ xtype: 'gridcolumn',
dataIndex: 'interview_date',
header: 'Date',
sortable: true,
readOnly: true,
width: 100,
editor: {
xtype: 'datefield'
}
},
{ xtype: 'numbercolumn',
header: 'Performance',
format:'0',
sortable: true,
width: 100,
align: 'right',
dataIndex: 'interview_performance',
editor: {
xtype: 'numberfield'
}
}
];
candidate_grid_interview.superclass.initComponent.call(this);
}
and the screen shots,
I faced the same problem and found my solution somewhere. Here is a reduced version of what I'm using. I think the key was the renderer property on the column. If your combo uses remote data, it might be loading its content after the grid is done loading - but I'm not sure it will cause the problem you're describing.
Try this concept:
var myStore = new Ext.data.JsonStore({
autoLoad: false,
...
fields: [
{ name: 'myID', type: 'int' },
{ name: 'myName', type: 'string' }
],
listeners: {
load: function () {
// Load my grid data.
},
scope: this
}
});
var myCombo = new Ext.form.ComboBox({
...
displayField: 'myName',
valueField: 'myID',
store: myStore
});
var grid = new Ext.grid.EditorGridPanel({
store: new Ext.data.ArrayStore({
...
fields: [
...
{ name: 'myID', type: 'int' },
...
]
}),
...
cm: new Ext.grid.ColumnModel({
columns: [
...
{
header: 'Header',
dataIndex: 'myID',
editor: myCombo,
renderer: function (value) {
var record = myCombo.findRecord(myCombo.valueField, value);
return record ? record.get(myCombo.displayField) : myCombo.valueNotFoundText;
}
}]
})
});
myStore.load();
Store loading is asynchronous, so it might be loading itself after rendering the grid. I recommend you render your grid within the store onload event. Also, datatypes can be painfull if you don't pay enough attention. Be sure that your grid store and combo store types match.
I'm trying to put a Grid (Ext.grid.GridPanel) object inside a Tab .
This is the code:
var grid = new Ext.grid.GridPanel({
store: new Ext.data.Store({
autoDestroy: true,
reader: reader,
data: xg.dummyData
}),
colModel: new Ext.grid.ColumnModel({
defaults: {
width: 120,
sortable: true
},
columns: [
{id: 'company', header: 'Company', width: 200, sortable: true, dataIndex: 'company'},
{header: 'Price', renderer: Ext.util.Format.usMoney, dataIndex: 'price'},
{header: 'Change', dataIndex: 'change'},
{header: '% Change', dataIndex: 'pctChange'},
// instead of specifying renderer: Ext.util.Format.dateRenderer('m/d/Y') use xtype
{
header: 'Last Updated', width: 135, dataIndex: 'lastChange',
xtype: 'datecolumn', format: 'M d, Y'
}
]
}),
sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
width: 600,
height: 300,
frame: true,
title: 'Framed with Row Selection and Horizontal Scrolling',
iconCls: 'icon-grid'
});
this.tabs = new Ext.TabPanel({
fullscreen: true,
type: 'dark',
sortable: true,
dockedItems: [
{
xtype:'toolbar',
title:'Hello World'
}],
tabBar: {
ui: 'light',
layout: {
pack: 'left'
}
},
items: [
{
cls:'hello',
id:'tab1',
html : '<a>hello</a>',
title:'Monitor'
}, {
cls:'world',
id:'tab2',
xtype: 'map',
html : '<a>hello world</a>',
title:'Map'
}, {
cls:'world',
id:'tab3',
html : '<a>hello world</a>',
dockedItems:grid
}]
});
When I load the page there's no error, but the grid is not shown.
The grid is not a docked item (that's for toolbars and other things that should stick to one side of a container). If you want the grid to take up the entire tab, just add it directly as an item to the TabPanel and it will become the full tab:
items: [
{
cls:'hello',
id:'tab1',
html : '<a>hello</a>',
title:'Monitor'
}, {
cls:'world',
id:'tab2',
xtype: 'map',
html : '<a>hello world</a>',
title:'Map'
},
grid ]