EXT JS Grid - Auto-increment rendered inputs - extjs

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

Related

proper method to get selected record from extjs grid

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');
...

Getting the Editable form on click of a Edit Button using ExtJs 3.4

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();
}
});

Assigning values to a model dynamically

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
});

Extjs table/grid auto expand to full

How do I go about making the gridpanel/table in extjs auto expand based on the window size?
Thanks
I added layout :'fit'.. i didn't change much
var listView = Ext.create('Ext.grid.Panel', {
store: mystore,
multiSelect: true,
title:'Notifications for ' + jsonServiceId + ' <i>(0 items selected)</i>',
viewConfig: {
emptyText: 'No images to display'
},
//reserveScrollOffset: true,
renderTo: containerEl,
layout:'fit',
columns: [{
text: 'Sell',
flex: 10,
dataIndex: 'Sell',
renderer: function(value, metaData, record) {
// alert(record.get('Sell'));
}
},{
text: 'Class',
flex: 10,
dataIndex: 'ClassName'
},
{
text: 'Last Changed',
flex: 20,
dataIndex: 'LastChangedAt',
xtype: 'datecolumn',
format: 'd-M-Y h:i a'
}
]
});
It's a layout thing. You should add layout: 'fit' to the parent container. It will make your grid expand to the size of parent container.
Just as here in example:
http://dev.sencha.com/deploy/ext-4.0.7-gpl/examples/layout-browser/layout-browser.html

Grid inside Tab

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 ]

Resources