How to get html button reference nested in a gridpanel in ExtJS - extjs

<table cellspacing="0" cellpadding="0" border="0" class="x-btn-wrap x-btn x-btn-text-icon" id="ext-comp-1169" style="width: auto;">
Profile
In this code, how can we get the reference to the button element?

In this particular case, Ext.getCmp('ext-comp-1169') would do the trick.
However, you should manually assign more sensible id values to components by using the id config option - otherwise you get auto-assigned ids.

This is the code I am using now.
function renderLinkBtn(val, p, record) {
var contentId = Ext.id();
createLinkButton.defer(1, this, [val, contentId, record]);
return('<div id="' + contentId + '"></div>');
};
function createLinkButton(value, id, record) {
var actnBtn = new Ext.Toolbar.SplitButton({
text: 'Action',
icon: '../images/icon-btn-action.png',
menu: new Ext.menu.Menu({
items: [
{text: 'Item 1'},
{text: 'Item 2'}
]
})
}).render(document.body, id);
}
var grid1 = new xg.GridPanel({
store: new Ext.data.Store({
reader: reader,
data: xg.dummyData
}),
cm: new xg.ColumnModel({
defaults: {
width: 20,
sortable: false
},
columns: [
{id:'id', header: "Link", renderer:renderLinkBtn, width: 25, align:'center', dataIndex:'id'},
{id:'company',header: "Company", width: 40, dataIndex: 'company'},
{header: "Price", renderer: Ext.util.Format.usMoney, dataIndex: 'price'},
{header: "Change", dataIndex: 'change'},
{header: "% Change", dataIndex: 'pctChange'},
{header: "Last Updated", renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
]
}),
sm: new Ext.grid.RowSelectionModel({
singleSelect: false,
listeners: {
rowselect: function(sm, row, rec) {
//grid1.getView().refreshRow(rec)
/* var btnElement = Ext.getDom(contentIdArray[row].btnEl.id);
btnElement.disabled = true;
Ext.Msg.alert(btnElement);*/
},
rowdeselect:function(sm, row, rec) {
/* var btnElement = Ext.getDom(contentIdArray[row].btnEl.id);
btnElement.disabled = false;*/
}
}
}),
viewConfig: {
forceFit:true
},
width: 600,
height: 300,
animCollapse: false,
title: 'Grid Panel with Button Renderer',
iconCls: 'icon-grid',
renderTo: document.body
});

First you should get a reference to your button Component. This can be saved in your DOM structure somewhere or you can get a reference to it with Ext.getCmp('myId') if you know the ID of your component.
When you have the Component simply fetch it's Ext.Element and search down the dom tree for a element like this
var myButton = Ext.getCmp('ext-comp-1169');
var buttonElement = myButton.getEl().child('button');

Related

Highlight grid row on edit

I am on ExtJs3.2.
There is a gridpanel with a column having a textField as an editor.
On change of value in the textfield - i need the corresponding row to be highlighted.
How do I get the 'owning' row index of the text field?
columns: [
...........
{header: 'Revenues',dataIndex: 'percentage',
editor: new Ext.form.TextField({
listeners: {
'change' : function (field, newValue, oldValue) {
if(oldValue!=newValue){
.......
//How do I get the row index?
Ext.fly(grid.getView().getRow(row)).addClass('yellow-row');
}
}
}
Is there any other way to achieve this?
Could you please try below?
var grid = Ext.grid.GridPanel({
store: yourStore,
id: 'myGrid',
colModel: new Ext.grid.ColumnModel({
defaults: {
width: 120,
sortable: true
},
columns: [
{id: 'Company', header: 'Company', width: 120, sortable: true, dataIndex: 'company'},
{header: 'Change', dataIndex: 'change'},
{
header: 'Revenue',
dataIndex: 'percentage',
editor: new Ext.form.TextField({
listeners: {
'change': function(field, newValue, oldValue) {
if (oldValue != newValue) {
var sel = Ext.getCmp('myGrid').getSelectionModel().getSelected();
// if you select more than one record use getSelections instead of getSelected
console.log(sel);
}
}
}
})
}
]
}),
sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
width: 500,
height: 300,
frame: true,
title: 'My Grid'
});

Add Ext.Button to ExtJS columnmodel

I'm creating an Ext.grid.GridPanel. I am trying to add column with xtype: button to the column model. I am not sure, if I can do that though. Below is my code and also this is a link to jsfiddle http://jsfiddle.net/bXUtQ/
I am using extjs 3.4
Ext.onReady(function () {
var myData = [[ 'Lisa', "lisa#simpsons.com", "555-111-1224"],
[ 'Bart', "bart#simpsons.com", "555-222-1234"],
[ 'Homer', "home#simpsons.com", "555-222-1244"],
[ 'Marge', "marge#simpsons.com", "555-222-1254"]];
var store = new Ext.data.ArrayStore({
fields:[ {
name: 'name'
},
{
name: 'email'
},
{
name: 'phone'
}],
data: myData
});
var grid = new Ext.grid.GridPanel({
renderTo: 'grid-container',
columns:[ {
header: 'Name',
dataIndex: 'name'
},
{
header: 'Email',
dataIndex: 'email'
},
{
header: 'Phone',
dataIndex: 'phone'
},
{
header: 'action',
xtype: 'actioncolumn',
iconCls: 'delete-icon'
text: 'Delete',
name: 'deleteBtn',
handler: function(grid, rowIndex, colIndex, item, e) {
alert('deleted');
}
},
//////////////////////////////
//I cannot add this column
{
header: 'action',
xtype: 'button',
text: 'update',
name: 'btnSubmit'
}
],
store: store,
frame: true,
height: 240,
width: 500,
title: 'Framed with Row Selection',
iconCls: 'icon-grid',
sm: new Ext.grid.RowSelectionModel({
singleSelect: true
})
});
});
You can't use a button as a column just like that. We're using the following UX to achieve just what you're asking. Unfortunately this is for ExtJS 4.1:
http://www.sencha.com/forum/showthread.php?148064-Component-Column-Components-in-Grid-Cells
You can try to Grid RowActions.
Will you try this as your actioncolumn
{
xtype: 'actioncolumn',
width: 50,
items:
[
{
icon: 'app/resources/images/cog_edit.png', // Use a URL in the icon config
tooltip: 'Edit',
handler: function (grid, rowIndex, colIndex) {
var rec = grid.getStore().getAt(rowIndex);
alert("Edit " + rec.get('name'));
}
},
{
icon: 'app/resources/images/delete.png',
tooltip: 'Delete',
handler: function (grid, rowIndex, colIndex) {
var rec = grid.getStore().getAt(rowIndex);
alert("Terminate " + rec.get('name'));
}
}
]
}
Or You can try this Plugin for actioncolumnbutton

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

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 ]

How do I query my ExtJS grid to see which CheckboxSelectionModel() checkboxes are checked?

I'm trying to get an ExtJS grid working that has checkboxes from which I can get an array of rows/ids so I know which rows have been checked.
I've used this example from Sencha to get the following grid to display correctly with the selection checkboxes, but it doesn't show how to get the information from the grid which rows have been checked, e.g. I will have a button that has a handler function and inside this I need to write something like:
var rowIdsChecks = grid.getRowIdsChecked();
How do I get the information out of the grid which rows are currently checked?
var myData = [
[4, 'This is a whole bunch of text that is going to be word-wrapped inside this column.', 0.24, '2010-11-17 08:31:12'],
[16, 'Computer2', 0.28, '2010-11-14 08:31:12'],
[5, 'Network1', 0.02, '2010-11-12 08:31:12'],
[1, 'Network2', 0.01, '2010-11-11 08:31:12'],
[12, 'Other', 0.42, '2010-11-04 08:31:12']
];
var myReader = new Ext.data.ArrayReader({}, [{
name: 'id',
type: 'int'
}, {
name: 'object',
type: 'object'
}, {
name: 'status',
type: 'float'
}, {
name: 'lastChange',
type: 'date',
dateFormat: 'Y-m-d H:i:s'
}]);
var sm = new Ext.grid.CheckboxSelectionModel();
var grid = new Ext.grid.GridPanel({
region: 'center',
style: 'margin: 10px',
store: new Ext.data.Store({
data: myData,
reader: myReader
}),
cm: new Ext.grid.ColumnModel({
defaults: {
width: 120,
sortable: true
},
columns: [
sm,
{
header: 'ID',
width: 50,
sortable: true,
dataIndex: 'id',
hidden: false
},
{
header: 'Object',
width: 120,
sortable: true,
dataIndex: 'object',
renderer: columnWrap
}, {
header: 'Status',
width: 90,
sortable: true,
dataIndex: 'status'
},
{
header: 'Last Updated',
width: 120,
sortable: true,
renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s'),
dataIndex: 'lastChange'
}]
}),
sm: sm,
viewConfig: {
forceFit: true
},
title: 'Computer Information',
width: 500,
autoHeight: true,
frame: true,
listeners: {
'rowdblclick': function(grid, index, rec){
var id = grid.getSelectionModel().getSelected().json[0];
go_to_page('edit_item', 'id=' + id);
}
}
});
Solution:
Thanks #jujule, this code works:
Ext.select('span#internal_link_001').on('click', function() {
var selections = grid.getSelectionModel().getSelections();
console.log(selections);
});
and then you have the ids like this:
The CheckboxSelectionModel is responsible of tracking and managing selections.
Just use its getSelections() method to get an array of selected records :
grid.getSelectionModel().getSelections()
If the grid has been loaded say a , then use.
<script type="text/javascript" language="javascript">
function ValidateCheckBoxChecked() {
var count = 0;
var counter = 0;
var ChkBoxValue;
var checkboxList = document.getElementById("divGridData").getElementsByTagName("input");
for (var i = 0; i < checkboxList.length; i++) {
if (checkboxList[i].checked) {
counter++;
}
}
return counter; //this will return the number of checkBoxed checked.
}
</script>

Resources