Select rows Ext.dataview.List based on Store - extjs

Ext.create('Ext.data.Store', {
fields: ['id', 'name'],
storeId: 'modules',
data : [
{"id":"0", "active": "1", "label": "RST", "name":"строка 1"},
{"id":"1", "active": "0", "label": "KAS", "name":"строка 2"},
{"id":"2", "active": "1", "label": "UKR", "name":"строка 3"},
{"id":"3", "active": "0", "label": "KJV", "name":"строка 4"},
]
});
in items:
{
xtype: 'list',
store: 'modules',
mode: 'MULTI',
queryMode: 'local',
fullscreen: true,
margin: '10 10 10 10',
itemTpl: '<div class="contact">{id} <b>{label}</b> {name}</div>'
}
how to select specific rows in a 'List' based on asset index 'active' in Store ?
when loading, the first and third lines should be highlighted (selected)
Can this be done based on XTemplate ?

XTemplate is used for custom display of a row in your list. For the selection you need to use setSelection method on the list in a loop that gets all records with active=="1". Check the following code, also there is a fiddle here showing it in work:
Ext.application({
name: 'Fiddle',
launch: function () {
let store = Ext.create('Ext.data.Store', {
fields: ['id', 'name', 'active', 'label'],
data: [{
"id": "0",
"active": "1",
"label": "RST",
"name": "строка 1"
}, {
"id": "1",
"active": "0",
"label": "KAS",
"name": "строка 2"
}, {
"id": "2",
"active": "1",
"label": "UKR",
"name": "строка 3"
}, {
"id": "3",
"active": "0",
"label": "KJV",
"name": "строка 4"
}, ]
});
let list = Ext.create({
xtype: 'list',
store: store,
mode: 'MULTI',
queryMode: 'local',
fullscreen: true,
margin: '10 10 10 10',
itemTpl: '<div class="contact">{id} <b>{label}</b> {name}</div>',
renderTo: Ext.getBody(),
});
store.each(function (record, id) {
if (record.get('active') == "1") {
list.setSelection(record);
}
});
}
});

Related

How to bind the hidden property of a component to another component in EXTJS

I am wondering how to properly use reference in EXTJS views. More specifically, I would like to bind hidden property of one component to state of other component with given reference. For example, if a view contains a button and a combobox, how to bind hidden property of a button to isSelected state of a combobox.
Here is what I tried, but nothing works:
Ext.create('Ext.Panel',{
renderTo: Ext.getBody(),
width: 400,
height: 400,
viewModel: {},
items:[
{
xtype: 'combobox',
reference: 'myCombo',
label: 'Select',
queryMode: 'local',
name: 'myComb',
store: {
fields: ['value', 'name'],
data : [
{"value":"1", "name":"Name1"},
{"value":"2", "name":"Name2"},
{"value":"3", "name":"Name3"},
{"value":"4", "name":"Name4"},
{"value":"5", "name":"Name5"},
]
},
editable: false,
displayField: 'name',
valueField: 'value',
},
{
xtype: 'button',
text: 'myButton',
bind: {
//hidden: '{myCombo.getValue()?true:false}'
//hidden: (!!myCombo.getValue())
}
}
]
})
Just bind the view model data to combobox value, something like this:
Ext.create('Ext.Panel', {
renderTo: Ext.getBody(),
width: 400,
height: 400,
viewModel: {
data: {
comboboxSelectedValue: '',
}
},
items: [{
xtype: 'combobox',
reference: 'myCombo',
label: 'Select',
queryMode: 'local',
name: 'myComb',
store: {
fields: ['value', 'name'],
data: [{
"value": "",
"name": "Unselected"
}, {
"value": "1",
"name": "Name1"
}, {
"value": "2",
"name": "Name2"
}, {
"value": "3",
"name": "Name3"
}, {
"value": "4",
"name": "Name4"
}, {
"value": "5",
"name": "Name5"
}, ]
},
editable: false,
displayField: 'name',
valueField: 'value',
bind: {
value: '{comboboxSelectedValue}'
}
}, {
xtype: 'button',
text: 'myButton',
bind: {
hidden: '{!comboboxSelectedValue}'
}
}]
})

How to load the values in combobox from database

How do I populate combo box values instead of hard coding from the database into the store of combo box
{
xtype: 'fieldset',
title: 'Dress Types',
items: [
{
xtype: 'combobox',
displayField: "displayName",
valueField: "displayName",
emptyText: 'Select Type',
store: {
fields: ["id", "displayName"],
data: [
{ "id": "1", "displayName": "Kurtha" },
{ "id": "2", "displayName": "Denim Top" },
{ "id": "3", "displayName": "Western T shirt" },
{ "id": "4", "displayName": "Sleeveless" }
]
},
name: 'dresses',
margin: '15px',
allowBlank: false,
forceSelection: true,
}
]
}
Thanks in advance
There are ways by which you can get this. You need to create one store in your js from your input data and then assign it to the comboBox.
Given example
var combstore = Ext.create('Ext.data.Store', {
autoLoad: true,
fields: [{
name: 'value',
mapping: "ITEMID",
type: 'string'
}, {
name: 'name',
mapping: "TITLE",
type: 'string'
}],
proxy: new Ext.data.HttpProxy({
type: 'ajax',
actionMethods: {
read: "POST"
},
url: Your URL,
headers: {
'Accept': 'application/json; charset=utf-8'
},
reader: {
type: 'json',
rootProperty: 'data'
},
autoLoad: true
})
});
Now in your comboBox you can call this combstore to your store.
store :combstore
In the variable combostore we are creating one data store by using Ext.data.Store and placing values in field. Then in proxy method calling url and mapping the values from field.
Read the doc for better understanding Doc
Check the below code.
Ext.create('Ext.form.ComboBox', {
valueField: "displayName",
emptyText: 'Select Type',
store: Ext.create('Ext.data.Store', {
fields: ["id", "displayName"],
proxy: {
url: 'data1.json',
reader: {
type: 'json',
rootProperty: 'data'
}
},
autoLoad: true
}),
name: 'dresses',
margin: '15px',
allowBlank: false,
forceSelection: true,
renderTo: Ext.getBody()
});
I assumed my service returing data like below
{
"data": [{
"id": "1",
"displayName": "Kurtha"
}, {
"id": "2",
"displayName": "Denim Top"
}, {
"id": "3",
"displayName": "Western T shirt"
}, {
"id": "4",
"displayName": "Sleeveless"
}]
}

angular ui-grid grouping from object

I am using ui-grid v3.0.0-rc.22 in my application.
I use grouping in my grid.
I have data as follow
[{
"_id": "559dfaa671c2bd2c0a00002f",
"title": "dfds",
"sku": "fdsf",
"price": "34535",
"desc": "dsfsdf",
"main_image": "NOPQR09345.png",
"added_by": "558a526b977459300a00002b",
"user": {
"_id": "558a526b977459300a00002b",
"name": "Merchant",
}
}, {
"_id": "559e0a0f71c2bd2c0a000031",
"title": "dfdxf",
"sku": "esdf",
"price": "345",
"desc": "xcfgvxdvf",
"main_image": "NOPQR09345.png",
"added_by": "558a526b977459300a00002b",
"user": {
"_id": "558a526b977459300a00002b",
"name": "Merchant",
}
}, {
"_id": "559e11084a3df01808000029",
"title": "Product 1",
"sku": "KJH",
"price": "12508",
"desc": "This istest",
"main_image": "NOPQR09345.png",
"added_by": "558a6ade977459300a00002c",
"user": {
"_id": "558a6ade977459300a00002c",
"name": "Merchant",
}
}]
I use grouping on user._id and i want to display name in grouping header. For that i use columnDefs as Follow.
[
{name: 'Merchant Name', field: 'user._id', grouping: {groupPriority: 0},
cellTemplate: '<span style="padding:15px;">{{row.entity.user.name}}</span>'},
{name: 'Name', field: 'title', enableCellEdit: true},
{name: 'SKU', field: 'sku'},
{name: 'Category', field: 'pro_category.name'},
{name: 'Price', field: 'price', treeAggregationType: uiGridGroupingConstants.aggregation.SUM}
]
The problem is that it show username in grouped column but not showing in groupHeader rows as follow
How can i do that. Any help greatly appreciated.
This is little tricky, since the grouped row does not have any actual entity associated with it. I think you can probably use the first row in the group's children to get the group header.
$scope.gridOptions = {
enableFiltering: true,
treeRowHeaderAlwaysVisible: false,
columnDefs:[
{name: 'Merchant Name', field: 'user._id', grouping: {groupPriority: 0},
cellTemplate: '<span style="padding:15px;">{{grid.appScope.name(grid, row)}}</span>'},
{name: 'Name', field: 'title', enableCellEdit: true},
{name: 'SKU', field: 'sku'},
{name: 'Category', field: 'pro_category.name'},
{name: 'Price', field: 'price', treeAggregationType: uiGridGroupingConstants.aggregation.SUM}
],
onRegisterApi: function( gridApi ) {
$scope.gridApi = gridApi;
}
};
$scope.name = function(grid,row,col)
{
if(row.groupHeader)
{
var entity = row.treeNode.children[0].row.entity;
return entity.user.name;
}
else
{
return row.entity.user.name;
}
return "SAmple";
}
Here is a plnkr http://plnkr.co/edit/N6I78SWkLK8wzZjHm8ds?p=preview

EXTJS4.1 : TreePanel and Cell Editing (Unexpected behavior of Cell Editing plugin)

I have a Tree Panel with two columns "Tree" and "Description".
I render tree and Description column from a TreeStore, The Description column is made editable using CellEditing plugin.
When i edit any cell under Description column, its corresponding Tree node's name changes to that of first tree node's name.
Here is my code,
var store = Ext.create('Ext.data.TreeStore',{
autoLoad : false,
autoSync : true,
proxy : {
type : 'ajax',
url : 'data/tree.json',
},
fields: [
{name: 'optionName', type: 'string'}, {name: 'description', type: 'string'}
]
}
);
Ext.override(Ext.data.AbstractStore,{
indexOf: Ext.emptyFn
});
Ext.define('AEV.view.TreePanel', {
extend: 'Ext.tree.Panel',
title: 'Simple Tree',
width: 700,
height: 400,
store: store,
rootVisible: false,
disableSelection:true,
viewConfig : {
emptyText : '<center><span class="empty-table">No data!</span></center>',
singleSelect : true,
stripeRows : true
},
selType: 'cellmodel',
plugins: [
Ext.create('Ext.grid.plugin.CellEditing', {
clicksToEdit:1
})
],
columns:[
{
xtype: 'treecolumn',
text: 'Tree',
dataIndex: 'optionName',
flex: 1,
sortable: false
},
{
text: 'Description',
dataIndex: 'description',
flex: 2,
sortable: false,
editor: {
xtype: 'textfield',
allowBlank: true
}
}
],
renderTo: Ext.getBody()
});
My tree.json is
{
"children": [
{
"optionName": "ABC",
"expanded": true,
checked: true,
"children": [
{
"optionName": "DEF",
checked: true,
"leaf": true,
"description" : ""
},
{
"optionName": "GHI",
checked: true,
"leaf": true,
"description" : ""
}
], "description" : ""
},
{
"optionName": "JKL",
"expanded": true,
checked: true,
"children": [
{
"optionName": "MNO",
checked: true,
"leaf": true,
"description" : ""
}
],
"description" : ""
},
{
"optionName": "PQR",
checked: true,
"expanded": true,
"children": [
{
"optionName": "STU",
checked: true,
"expanded": true,
"children": [
{
"optionName": "VW",
checked: true,
"leaf": true,
"description" : ""
},
{
"optionName": "XYZ",
checked: true,
"leaf": true,
"description" : ""
}
],
"description" : ""
}
],
"description" : ""
}
]
}
I have also overridden AbstractStore's indexOf config, which i saw as bug in http://www.sencha.com/forum/showthread.php?131602-4.0.0-Cellediting-on-TreeGrid
I don't want the tree node names to change, as i edit any other column data.
Help would be really appreciated. Thanks in advance.

ExtJS grouping grid cannot load JSON data

I am not able to load data in ExtJS grouping grid.
I have following code for ExtJS grouping.js.
Ext.onReady(function() {
Ext.QuickTips.init();
var xg = Ext.grid;
var documentType = "";
var fileName = "";
var size = "";
var fileGPath = "";
// shared reader
var reader = new Ext.data.ArrayReader({}, [{
name: 'filename'
}, {
name: 'size'
}, {
name: 'author'
}, {
name: 'date',
type: 'date',
dateFormat: 'Y-m-d H:i:s'
}, {
name: 'action'
}, {
name: 'document'
}
]);
var store = new Ext.data.GroupingStore({
reader: reader,
url: 'DocumentListService.jsp',
root: 'Documents',
sortInfo: {
field: 'filename',
direction: "ASC"
},
fields: [{
name: 'filename',
mapping: 'FileName'
}, {
name: 'size',
mapping: 'Size'
}, {
name: 'author',
mapping: 'Author'
}, {
name: 'date',
mapping: 'Date'
}, {
name: 'action',
mapping: ''
}, {
name: 'document',
mapping: 'Document'
}],
groupField: 'document'
});
var grid = new xg.GridPanel({
id: 'myGridId',
store: store,
columns: [{
id: 'filename',
header: "File Name",
width: 60,
sortable: true,
dataIndex: 'filename'
}, {
header: "Size",
width: 20,
sortable: true,
dataIndex: 'size'
}, {
header: "Author",
width: 20,
sortable: true,
dataIndex: 'author'
}, {
header: "Date",
width: 20,
sortable: true,
dataIndex: 'date'
}, {
header: "Action",
width: 20,
sortable: true,
dataIndex: 'action'
}, {
header: "Document",
width: 20,
sortable: true,
dataIndex: 'document',
hidden: 'true'
}],
view: new Ext.grid.GroupingView({
forceFit: true,
groupTextTpl: '{text} ({[values.rs[0].get("filename")=="" ? "0" : values.rs.length]} {[values.rs.length > 1 ? "Documents" : "Document"]}) <img src="images/image_add.png" alt="Upload Document" style="border:none;margin-left:390px;"/>'
}),
frame: true,
width: 700,
height: 450,
collapsible: true,
animCollapse: false,
title: 'Document Library',
iconCls: 'icon-grid',
renderTo: 'docUpload'
});
var fp = new Ext.FormPanel({
renderTo: 'fi-form',
fileUpload: true,
width: 500,
frame: true,
title: 'File Upload Form',
autoHeight: true,
bodyStyle: 'padding: 10px 10px 0 10px;',
labelWidth: 50,
defaults: {
anchor: '95%',
allowBlank: false,
msgTarget: 'side'
},
items: [{
xtype: 'fileuploadfield',
id: 'form-file',
emptyText: 'Select a File to import',
fieldLabel: 'File',
name: 'file',
buttonCfg: {
text: '',
iconCls: 'upload-icon'
},
listeners: {
'fileselected': function(v) {
if (fp.getForm().isValid()) {
fp.getForm().submit({
url: 'FileUpload.jsp',
waitMsg: 'Uploading your file...',
success: function(fp, jsonObj) {
fileName = jsonObj.result.fileName;
size = jsonObj.result.size;
fileGPath = jsonObj.result.fileGPath;
documentType = (document.getElementById("hdnId").value).replace("Document: ", "");
addDataToGrid(fileName, fileGPath, size, documentType);
Ext.getCmp('myGridId').getStore().loadData(xg.dummyData);
msg('Success', 'Successfully uploded on the server');
},
failure: function(fp, o) {
msg('Failure', 'Failed to upload');
}
});
}
}
}
}]
});
});
And I have following as json string in DocumentListService.jsp:
{
"DocumentList": [{
"DocumentGroup": "Invoice",
"isDocumentBundle": true,
"isWritable": true,
"Documents": [{
"FileName": "ABC.doc",
"Size": "123",
"Author": "xyz",
"Date": "\\/Date(1238025600000)\\/",
"FileGPath": "xyz doc",
"Document": "Invoice"
}, {
"FileName": "ABC.doc",
"Size": "123",
"Author": "xyz",
"Date": "\\/Date(1238025600000)\\/",
"FileGPath": "abc doc",
"Document": "Invoice"
}]
}, {
"DocumentGroup": "SOP",
"isDocumentBundle": true,
"isWritable": true,
"Documents": [{
"FileName": "ABC.doc",
"Size": "123",
"Author": "xyz",
"Date": "\\/Date(1238025600000)\\/",
"FileGPath": "xyz doc",
"Document": "SOP"
}]
}]
}
Still I am not able to load data.
Well your problem is the store reader, which has set as root "Documents" but the Json received has the root DocumentList. So the problem is mapping the json data to your store.
EDIT
The problem is that you are using incorectly the grouping store. The data does not need to be grouped when you are getting it from the backend.
Here is my suggestion
Json sent from back-end just a list of documents in which you have the type:
{
"Documents": [{
"Type": "invoice",
"FileName": "ABC.doc",
"Size": "123",
"Author": "xyz",
"Date": "\/Date(1238025600000)\/",
"FileGPath": "xyz doc",
"Document": "Invoice"
}, {
"Type": "invoice",
"FileName": "ABC.doc",
"Size": "123",
"Author": "xyz",
"Date": "\/Date(1238025600000)\/",
"FileGPath": "abc doc",
"Document": "Invoice"
}, {
"Type": "SOP",
"FileName": "ABC.doc",
"Size": "123",
"Author": "xyz",
"Date": "\/Date(1238025600000)\/",
"FileGPath": "xyz doc",
"Document": "SOP"
}]
}
And for the grouping store just use groupField: "Type" , And the reader should have the root Documents. This will simplify your load but it means you have to duplicate the extra data you need for each group and attach it to each document.
Is just a suggestion.

Resources