Make gridpanel checkbox readonly by default - extjs

I have a gridpanel with few items and checkboxes. i want a specific item in the gridpanel to be checked and readonly by default (the checked value cannot be changed by user). How should i do that? My code is as follows:
var MyCheckboxModel = Ext.create('Ext.selection.CheckboxModel', {
mode : 'SIMPLE',
listeners : {
selectionchange : function(selectionModel) {
}
},
});
var userCheckboxContainersStore = Ext.create('Ext.data.Store', {
storeId: 'userCheckboxStore',
fields: ['data'],
data: [
{ data: 'Item 1'},
{ data: 'Item 2' },
{ data: 'Item 3'},
{ data: 'Item 4'},
{ data: 'Item 5'},
]
});
var userCheckboxGridPanel = Ext.create('Ext.grid.Panel', {
layout : {
type : 'vbox',
align : 'stretch'
},
defaults : {
border : 0,
bodyStyle : {
backgroundColor : 'transparent'
}
},
store: CheckboxContainersStore,
selModel: MyCheckboxModel,
title: 'Item List',
columns: [
{ dataIndex: 'data'},
]
});
I want 'Item 1' to be checked by default and readonly. Any suggestions would be appreciated

You need something like this, here you can try a fiddle
You can't work with readonly true, but instead you can work with selections of the grid, and make sure the item1 will never be unselected.
Ext.application({
name: 'Fiddle',
launch: function () {
var MyCheckboxModel = Ext.create('Ext.selection.CheckboxModel', {
mode: 'SIMPLE',
listeners: {
selectionchange: function (selectionModel) {}
},
});
var userCheckboxContainersStore = Ext.create('Ext.data.Store', {
storeId: 'userCheckboxStore',
fields: ['data'],
data: [{
data: 'Item 1'
}, {
data: 'Item 2'
}, {
data: 'Item 3'
}, {
data: 'Item 4'
}, {
data: 'Item 5'
},
]
});
var userCheckboxGridPanel = Ext.create('Ext.grid.Panel', {
layout: {
type: 'vbox',
align: 'stretch'
},
defaults: {
border: 0,
bodyStyle: {
backgroundColor: 'transparent'
}
},
store: userCheckboxContainersStore,
selModel: MyCheckboxModel,
title: 'Item List',
columns: [{
dataIndex: 'data'
}],
listeners: {
render: function (gridPanel) {
gridPanel.setSelection(userCheckboxContainersStore.getAt(0));
gridPanel.on('itemclick', function (panel, selected) {
if (selected.id === userCheckboxContainersStore.getAt(0).id) {
var newSelection = gridPanel.getSelection();
if (newSelection.indexOf(selected) === -1)
newSelection.push(selected);
gridPanel.setSelection(newSelection);
}
});
}
},
renderTo: Ext.getBody()
});
}
});

Another way of implementation would be like this.
var MyCheckboxModel = Ext.create('Ext.selection.CheckboxModel', {
mode: 'SIMPLE',
checkOnly: true,
listeners: {
beforedeselect: function(grid, record, index, eOpts) {
if (record.get('data') == "Item 1") {
return false;
}
}
}
});
var userCheckboxContainersStore = Ext.create('Ext.data.Store', {
storeId: 'userCheckboxStore',
fields: ['data'],
data: [{
data: 'Item 1'
}, {
data: 'Item 2'
}, {
data: 'Item 3'
}, {
data: 'Item 4'
}, {
data: 'Item 5'
},
]
});
var userCheckboxGridPanel = Ext.create('Ext.grid.Panel', {
layout: {
type: 'vbox',
align: 'stretch'
},
defaults: {
border: 0,
bodyStyle: {
backgroundColor: 'transparent'
}
},
listeners: {
'viewready': function(grid) {
grid.selModel.doSelect(grid.store.data.items[0]);
}
},
store: userCheckboxContainersStore,
selModel: MyCheckboxModel,
title: 'Item List',
columns: [{
dataIndex: 'data'
}],
renderTo: Ext.getBody()
});
Updated Fiddle: http://jsfiddle.net/y61yo7sx/1/

Related

extJS 5.0 bind different store based on tab selection

Looking for some advice on how I can dynamically load data from a different datastore based on a tab that is selected.
Both stores have identical column names so loading the dataIndex should not need to be changed.
When the "Current" tab is selected I would like to bind the store "current"
When the "Completed" tab is selected I would like to bind the store "completed"
A sample of my code is below:
viewModel: {
formulas: {
activeStore: function(get) {
var active = get('active');
return this.getStore(active == 'aTab' ? 'a' : 'b');
}
},
data: {
active: 'aTab'
},
stores: {
a: 'Change',
b: 'ChangeArchive',
}
},
{
xtype: 'tabpanel',
id: 'changetabs',
tabBarHeaderPosition: 1,
headerPosition: 'top',
plain: true,
width: 480,
height: 30,
items: [{
title: 'Current',
itemId: 'aTab'
},
{
title: 'Completed',
itemId: 'bTab'
}
],
listeners: {
tabchange: function(tabPanel, newTab) {
tabPanel.ownerCt.getViewModel().set('active', newTab.getItemId());
}
}
},
And the Grid
{
region: 'west',
xtype: 'grid',
bind: {store: '{activeStore}'},
viewConfig: {
markDirty: false
},
id: 'ActionList',
columns: {
items: [
{ text: 'Action', dataIndex: 'action_id', width: 300},
{ text: 'Status', dataIndex: 'status', width: 180},
]
},
listeners: {
select: 'onSelectAction'
}
}
You should do it using a formula:
Ext.define('Foo', {
extend: 'Ext.data.Model'
});
Ext.application({
name : 'Fiddle',
launch : function() {
new Ext.container.Viewport({
layout: 'border',
viewModel: {
formulas: {
activeStore: function(get) {
var active = get('active');
return this.getStore(active == 'aTab' ? 'a' : 'b');
}
},
data: {
active: 'aTab'
},
stores: {
a: {
model: 'Foo',
data: [{
foo: 1
}]
},
b: {
model: 'Foo',
data: [{
foo: 2
}]
}
}
},
items: [{
region: 'west',
xtype: 'grid',
width: 200,
bind: '{activeStore}',
columns: [{
dataIndex: 'foo'
}]
}, {
region: 'center',
xtype: 'tabpanel',
items: [{
title: 'A',
itemId: 'aTab'
}, {
title: 'B',
itemId: 'bTab'
}],
listeners: {
tabchange: function(tabPanel, newTab) {
tabPanel.ownerCt.getViewModel().set('active', newTab.getItemId());
}
}
}]
});
}
});
Fiddle.

How to create a Form for each row in a grid panel:extjs

How do i create a different form for each row in the Grid...
i have a grid like ..
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [
{ text: 'Name', dataIndex: 'name' },
{ text: 'Email', dataIndex: 'email', flex: 1 },
{ text: 'Phone', dataIndex: 'phone' }
],
height: 200,
width: 400,
renderTo: Ext.getBody()
});
Your question doesn't explain your problem very well. Please update your topic and question. As I understood from your question, yes, you can. There are couple of ways. One of them is putting a form into a grid cell using grid column renderer. Another way is using editor in grid column. The second way is easy, but it's not proper way. If you want the second way also, I can add it. So, I'll add an efficient one. Please check the code below and fiddle:
https://fiddle.sencha.com/#fiddle/11i5
Ext.define('UploadForm', {
extend: 'Ext.form.Panel',
width: 200,
frame: true,
items: [{
xtype: 'filefield',
name: 'photo',
msgTarget: 'side',
allowBlank: false,
buttonText: 'Select'
}],
buttons: [{
text: 'Upload',
handler: function() {
var form = this.up('form').getForm();
if(form.isValid()){
form.submit({
url: 'photo-upload.php',
waitMsg: 'Uploading your photo...',
success: function(fp, o) {
Ext.Msg.alert('Success', 'Your photo "' + o.result.file + '" has been uploaded.');
}
});
}
}
}],
initComponent: function () {
if (this.delayedRenderTo) {
this.delayRender();
}
this.callParent();
},
delayRender: function () {
Ext.TaskManager.start({
scope: this,
interval: 200,
run: function () {
var container = Ext.fly(this.delayedRenderTo);
if (container) {
this.render(container);
return false;
} else {
return true;
}
}
});
}
});
var store = Ext.create('Ext.data.Store', {
fields: ['Name', 'Phone', 'Email', 'filePath'],
data: [{
Name: 'Rick',
Phone: '23122123',
Email: 'something#mail.com',
filePath: '/home'
}, {
Name: 'Jane',
Phone: '32132183',
Email: 'some#thing.com',
filePath: '/home'
}]
});
var renderTree = function(value, meta, record) {
var me = this;
var container_id = Ext.id(),
container = '<div id="' + container_id + '"></div>';
Ext.create('UploadForm', {
delayedRenderTo: container_id
});
return container;
}
Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: store,
columns: [
{ text: 'Name', dataIndex: 'Name' },
{ text: 'Email', dataIndex: 'Email' },
{ text: 'Phone', dataIndex: 'Phone' },
{ text: 'Upload',
dataIndex: 'filePath',
width: 300,
renderer: renderTree
}
],
renderTo: Ext.getBody()
});
P.s. Its based from Render dynamic components in ExtJS 4 GridPanel Column with Ext.create

ExtJS5 Web Desktop Sample: Unable to fire event from controller

I have been working with web desktop example in ExtJS5. The example has static data and no events. I wanted to implement a click event on 'Remove Something' button on grid window. Here is my modified code:
Ext.define('SampleApp.view.main.GridWindow', {
extend: 'Ext.ux.desktop.Module',
requires: [
'Ext.data.ArrayStore',
'Ext.util.Format',
'Ext.grid.Panel',
'Ext.grid.RowNumberer'
],
init: function () {
this.launcher = {
text: 'Grid Window',
iconCls: 'icon-grid'
};
},
controller: 'gridwindow',
createWindow: function () {
var desktop = this.app.getDesktop();
var win = desktop.getWindow('grid-win');
if (!win) {
win = desktop.createWindow({
id: 'grid-win',
title: 'Grid Window',
width: 740,
height: 480,
iconCls: 'icon-grid',
animCollapse: false,
constrainHeader: true,
layout: 'fit',
items: [
{
border: false,
xtype: 'grid',
store: mock.view.main.GridWindow.getDummyData(),
columns: [{ xtype: 'rownumberer', sortable: false, text: "S.N.", width: 70 }, {
id: 'topic',
text: "Topic",
dataIndex: 'gridTopic',
flex: 1,
align: 'center'
}, {
text: "Author",
dataIndex: 'gridAuthor',
flex: 1,
align: 'center',
sortable: true
}, {
text: "Replies",
dataIndex: 'gridReplies',
align: 'center',
flex: 1,
sortable: true
}, {
id: 'last',
text: "Last Post",
dataIndex: 'gridLastPost',
flex: 1,
align: 'center',
sortable: true
}]
}
],
tbar: [{
text: 'Add Something',
tooltip: 'Add a new row',
iconCls: 'add'
}, '-', {
text: 'Options',
tooltip: 'Modify options',
iconCls: 'option'
}, '-', {
text: 'Remove Something',
tooltip: 'Remove the selected item',
iconCls: 'remove',
listeners: {
click: 'onDeleteClick'
}
}]
});
}
return win;
},
statics: {
getDummyData: function () {
var _store = Ext.create('Ext.data.Store', {
fields: [
{ name: 'gridId', type: 'int' },
{ name: 'gridTopic', type: 'string' },
{ name: 'gridAuthor', type: 'string' },
{ name: 'gridReplies', type: 'string' },
{
name: 'gridLastPost', type: 'date', convert: function (value) {
var _date = new Date(value);
return Ext.Date.format(_date, "Y-m-d H:i:s");
}
}
]
});
var _responseText;
Ext.Ajax.request({
async: false,
url: 'http://localhost/sampleapp/getusers',
method: 'GET',
success: function (resp) {
_responseText = Ext.decode(resp.responseText);
_store.loadData(_responseText);
}
});
return _store;
}
}
});
I am unable to handle 'onDeleteClick' event inside the controller. Here is the controller definition:
Ext.define('SampleApp.view.main.GridWindowController', {
extend: 'Ext.app.ViewController',
alias: 'controller.gridwindow',
onDeleteClick: function (button, evt) {
alert('Clicked');
}
});
Can someone point out the mistake. How can this event be handled?
Ext.ux.desktop.Module does not accept controller config option.
Use your controller on an Ext.Component — in your case either the grid or the window.

extjs getselectedcell() but not using Ext.grid.CellSelectionModel

I hv an editorGridPanel and it's selmodel=new Ext.grid.CheckboxSelectionModel(), but I want to get the selected column by using getSelectedCell, is it possible?
Here is working sample: http://jsfiddle.net/qxpfJ/1/
function cellRenderer(value, column, record, row, col){
return Ext.String.format('<span key="{0}_{1}_{2}_{3}">{4}</span>',
record.data.id, column.column.dataIndex, row, col, value);
}
Ext.create('Ext.grid.Panel', {
title: 'Countries',
selType : 'cellmodel',
renderTo: Ext.getBody(),
store: Ext.create('Ext.data.Store', {
fields:['id', 'cName'],
data:{'items':[ { id: 1, cName: 'Australia' },
{ id: 2, cName:'Germany' },
{ id: 3, cName:'Russia' },
{ id: 4, cName:'United States' }]},
proxy: {
type: 'memory',
reader: { type: 'json', root: 'items' }
}
}),
columns: [{ text: 'id', dataIndex: 'id', renderer: cellRenderer },
{ text: 'Country', dataIndex: 'cName', flex: 1, renderer: cellRenderer }],
listeners:{
selectionchange: function( me, selected, eOpts ){
var sel = Ext.query('.x-grid-cell-selected span');
if(sel[0]){
var data = sel[0].getAttribute('key').split('_');
container.update( Ext.String.format(
'id={0};<br/>column="{1}";<br/>rowIndex={2};<br/>colIndex={3};',
data[0], data[1], data[2], data[3]));
}
}
}
});
var container = Ext.create('Ext.container.Container', {
renderTo: Ext.getBody()
});
EDIT
And here is same sample, but for checkboxmodel: http://jsfiddle.net/qxpfJ/2/
Ext.create('Ext.grid.Panel', {
title: 'Countries',
selType : 'checkboxmodel',
renderTo: Ext.getBody(),
store: Ext.create('Ext.data.Store', {
fields:['id', 'cName'],
data:{'items':[ { id: 1, cName: 'Australia' },
{ id: 2, cName:'Germany' },
{ id: 3, cName:'Russia' },
{ id: 4, cName:'United States' }]},
proxy: {
type: 'memory',
reader: { type: 'json', root: 'items' }
}
}),
columns: [{ text: 'id', dataIndex: 'id' },
{ text: 'Country', dataIndex: 'cName', flex: 1 }],
listeners:{
cellclick: function( me, td, cellIndex, record, tr, rowIndex, e, eOpts ){
container.update( Ext.String.format(
'id={0};<br/>rowIndex={1};<br/>cellIndex={2};',
record.data.id, rowIndex, cellIndex));
}
}
});
var container = Ext.create('Ext.container.Container', {
renderTo: Ext.getBody()
});

Adding an empty row to a grid

I am trying to add rows to my grid.
I saw an example in the docs:
onAddRouteClick: function(){
// Create a model instance
var rec = new KitchenSink.model.grid.Plant({
buying_vendor_id: 12,
country_code: '1',
route: 0
});
this.getStore().insert(0, rec);
this.cellEditing.startEditByPosition({
row: 0,
column: 0
});
}
this.getStore().insert(0, rec);
this.cellEditing.startEditByPosition({
row: 0,
column: 0
});
}
But i cant seem to make it work in my code.
This is my grid:
onBtnRoutesSearchClick: function(button, e, options){
var me = this;
var v_url = 'GetRoutes.jsp?' + Ext.urlEncode({'route_id': routeID, 'route_country_code' : routeCountryCode , 'route_vendor_id' : routeVendorID});
var newTab = Ext.create('Ext.panel.Panel', {
id: 'routes_pannel',
title: 'Routes',
autoScroll: true,
layout: {
type: 'fit'
},
closable: true,
dockedItems: [
{
xtype: 'toolbar',
dock: 'top',
items: [
{
xtype: 'button',
id: 'buttonResetBid',
icon: 'images/Plus.png',
text: 'Add Row',
listeners: {
click: {
fn: me.onAddRouteClick,
scope: me
}
}
}
]
}
],
items: [{
id: 'routes_grid',
xtype: 'gridpanel',
autoShow: false,
autoScroll: true,
store: Ext.create('Ext.data.Store', {
fields:[
{name: 'buying_vendor_id', type: 'int', sortType: 'asInt'},
{name: 'country_code', type: 'int', sortType: 'asInt'},
{name: 'route', type: 'int', sortType: 'asInt'}
],
proxy: {
type: 'ajax',
timeout: 120000,
url: v_url,
reader: {
type: 'json',
root: 'data',
successProperty: 'success'
}
},
autoLoad: true
}),
columns: [
{
xtype: 'gridcolumn',
dataIndex: 'buying_vendor_id',
width: 100,
text: 'Buying Vendor'
},
{
xtype: 'gridcolumn',
dataIndex: 'country_code',
width: 100,
text: 'Country Code'
},
{
xtype: 'gridcolumn',
dataIndex: 'route',
width: 80,
text: 'Route'
}
],
}]
});
var panel = Ext.getCmp("MainTabPanelID");
panel.add(newTab).show();
1.Create your model
Ext.define('Product', {
extend: 'Ext.data.Model',
fields:
[
{ name: 'ProductID' },
{ name: 'ProductName' },
{ name: 'UnitPrice' },
{ name: 'UnitsInStock' }
]
});
2.create your rowEditing
var rEditor = Ext.create('Ext.grid.plugin.RowEditing', {
clicksToEdit: 2,
listeners:
{
edit: function (editor, e) { });
}
});
3.get Store and create your grid
var grid = Ext.create('Ext.grid.Panel', {
store: store,
plugins: [rEditor],
title: 'Products',
columns:
[
],
dockedItems:
[
{
xtype: 'toolbar',
dock: 'top',
items:
[
{
xtype: 'button',
text: 'Yeni',
listeners:
{
click:
{
fn: function () {
store.insert(0, new Product());
rEditor.startEdit(0, 0);
}
}
}
}
]
}
],
width: 450,
renderTo: Ext.getElementById('hede')
});
So you are trying to add a record to store right?
OK, lets look at the Store API
http://docs.sencha.com/extjs/4.1.3/#!/api/Ext.data.Store-method-add
Sample usage:
myStore.add({some: 'data'}, {some: 'other data'});
The best practice is to also create a Model class . Read the component guides on grid and the data package .

Resources