Loading/saving multiple rows of JSGrid - mysql

I would like to use JSGrid as a way to both load data from a MySQL database, as well as save (new or edited) multiple rows of data via CakePHP 3. However, I'm unsure of how to do that, or whether that's possible.
For loading, I'd like to get the data from two connected tables if say I want to edit data. In CakePHP 3, I'd retrieve the data using a find and then setting it to the view, but I'm unsure how to go from there to put it into the JSGrid upon loading the view.
For saving similarly, I would save to two different tables. In CakePHP 3, normally for multiple tables, I'd use patch entity to save to the various tables, and AJAX to save things on another page without actually being on that page (the two add pages of the each of the tables). But I'm not sure if you can do that with multiples of data.
Eg.
$.post(".../users/add.json"
given the fields for users, will do what the add page would normally do, and patch entity with associated tables would save to the various associated tables, but only one at a time.
Update:
My config/routes.php:
Router::scope('/', function (RouteBuilder $routes) {
$routes->extensions(['json', 'xml']);
$routes->resources('Instrument');
}
My view:
<div id="jsGrid"></div>
<script>
var resources = [];
var instruments = [
{Name: "", Id: 0},
{Name: "Clarinet", Id: 1},
{Name: "Guitar", Id: 2},
{Name: "Piano", Id: 3},
{Name: "Violin", Id: 4},
];
$("#jsGrid").jsGrid({
controller: {
loadData: function (filter) {
return $.ajax({
type: "GET",
url: "<?= $basepath ?>/instrument/view/",
data: filter
});
},
insertItem: function (item) {
return $.ajax({
type: "POST",
url: "<?= $basepath ?>/instrument/add",
data: item
});
},
updateItem: function (item) {
return $.ajax({
type: "PUT",
url: "<?= $basepath ?>/instrument/edit",
data: item
});
},
deleteItem: function (item) {
return $.ajax({
type: "DELETE",
url: "<?= $basepath ?>/instrument/delete",
data: item
});
}
},
width: "100%",
height: "400px",
inserting: true,
editing: true,
sorting: true,
paging: true,
data: resources,
fields: [
{
name: "Instrument",
type: "select",
items: instruments,
valueField: "Id",
textField: "Name",
validate: "required",
width: 175
},
{name: "Comment", type: "text", width: 150},
{type: "control"}
]
});
</script>
<?= $basepath ?> is a variable set in my AppController that means regardless of the project name, I can still access URLs.
In the view, when I try to save a new line, the line is added, but the contents are blank. When I try to edit a line, I get a "Please wait", and then nothing gets saved, and I can still edit the fields. Also, in the console, I receive the following error:
http://localhost/<?= $basepath ?>/instrument/edit Failed to load resource: net::ERR_CONNECTION_RESET

Related

ExtJS REST store: sync() doesn't remove records on the server

I use ExtJS 4.2.2. A store for browsing and managing folders is defined like this:
Ext.define('hds.store.CaseFolders', {
extend: 'Ext.data.Store',
requires: [
'app.model.Folder',
'Ext.data.proxy.Rest',
'Ext.data.reader.Json',
'Ext.data.writer.Json'
],
constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
autoLoad: false,
model: 'app.model.Folder',
storeId: 'Folders',
proxy: {
type: 'rest',
url: '/api/folders.json',
reader: {
type: 'json',
messageProperty: 'message',
root: 'data'
},
writer: {
type: 'json',
writeAllFields: false,
root: 'data'
}
}
}, cfg)]);
}
});
It uses this model:
Ext.define('app.model.Folder', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.Field'
],
fields: [
{
name: 'name',
type: 'string'
},
{
name: 'parentId',
type: 'int'
},
{
name: 'type',
type: 'string'
}
]
});
Folders can be added, renamed and removed.
When changing the name of the folder, for example and using store.sync(), the record is properly synced to the server with a PUT containing id and name attributes.
However, when deleting the folder, store.sync() doesn't send any request to the server. A callback function was added to sync, but it is not called at all.
It seems that the REST store (proxy?) "thinks" that there is nothing to sync to the server.
Why is it like that?
Found the reason for the problem that I had:
The store is filtered to show content of the folder to be deleted.
When wanting to delete current folder, its record is filtered away.
Adding a line to remove filters before removing the parent folder record solved the problem.

EXTJS 4 Json nested data in grid panel

This topic has been discussed several times on the web but all subjects none helped me solve my problem.
My javascript code receives the JSON nested data. All JSON data Level 1 data are transcribed in the grid panel but all child data none.
I have tried so many ways but impossible.
That's why I'm asking you to help me please.
My JSON:
{
"success":true,
"error":false,
"redirectUrl":null,
"fund":[{
"cat_id":1,
"catname":"Europe OE Japan Large-Cap Equity",
"region":{
"region_id":2,
"region_name":"JAPAN"
}
},{
"cat_id":2,
"catname":"Europe OE Europe Large-Cap Growth Equity",
"region":{
"region_id":1,
"region_name":"EUROPE"
}
}]
}
MY model:
var Recommended = new function() {
this.DataModel = function() {
Ext.define('Fund', {
extend: 'Ext.data.Model',
fields: [{
name: 'catname',
type: 'string'
},{
name: 'cat_id',
type: 'int'
}],
proxy :{
type: 'rest',
url: application +'directory/function',
reader: {
type: 'json',
root: 'fund'
}
},
associations: [{
type: 'hasOne',
model: 'Region',
primaryKey: 'region_id',
name:'region'
}]
});
Ext.define('Region', {
extend: 'Ext.data.Model',
fields: [{
name: 'region_name',
type: 'string'
},{
name: 'region_id',
type: 'int'
}]
});
}
My Store & grid Panel:
this.JSONstore = function() {
var storeRecommended;
storeRecommended = Ext.create('Ext.data.Store', {
model: 'Fund',
autoLoad:true,
groupField: 'region_name'
});
var colModel =
[
{
text: 'REGION',
width: 200,
dataIndex: 'region_name',
name:'region_name',
mapping:'region.region_name'
},{
text: 'MORNINGSTAR',
width: 300,
dataIndex: 'catname',
name:'catname'
}
];
var groupingFeature = Ext.create('Ext.grid.feature.Grouping',{
groupHeaderTpl: 'Classe: {name} ({rows.length} Item{[values.rows.length > 1 ? "s" : ""]})',
hideGroupedHeader: false
});
var grid = Ext.create('Ext.grid.Panel', {
renderTo: 'recommendedlists',
collapsible: true,
iconCls: 'icon-grid',
frame: true,
store: storeRecommended,
width: 1200,
height: 400,
title: 'Test',
resizable: true,
features: [groupingFeature],
columns: colModel,
fbar : ['->', {
text:'Clear Grouping',
iconCls: 'icon-clear-group',
handler : function(){
groupingFeature.disable();
}
}]
});
}
this.initControlsOnload = function() {
Recommended.DataModel();
Recommended.JSONstore();
}
} // close Recommended function
The problem is your store bound to the grid knows nothing about Regions. It stores Funds. So you can't ask for a column to map to a data property that's not in the store.
The store is flat list of Fund records. And sure an individual Fund itself might know about the Region it belongs to, but the store containing a list of funds does not.
What to do?
What needs to happen is flattening out of your data structure on the client side. Why? Because the grid is flat. If you had multiple regions per fund - then we would be talking about a different solution.
How to do that?
If you control the server side of this app then add a Region field to the Fund object, then your data set is simple, straight forward and more importantly flat. If you can't (or don't want to) change the server side, then you can change the client side Model mapping. Essentially you would change your Fund model to something like this:
Ext.define('Fund', {
extend: 'Ext.data.Model',
fields: [
{ name: 'catname', type: 'string' },
{ name: 'cat_id', type: 'int' },
{ name: 'region_name', type: 'string',
mapping: 'region.region_name'},
{ name: 'region_id', type: 'int',
mapping: 'region.region_id'}
]
....
});
You see what we did there? We flattened the Region data into the Fund record. And now your store will have no problems accessing Region name data by name.
Good Luck,
Dmitry.

Load static JSON data from URL for ExtJS grid panel

I tried to implement a grid panel using data from a JSON file which is located on the same domain as the Javascript file containing the ExtJs code. I'm using ExtJs 3.4.
The grid shows up but without any data in it. Firebug shows an error message that tells me that there is an error in the first line of the JSON file ("not well formed"). I have validated the JSON file and everything is ok.
Here is my code:
Ext.onReady(function () {
var myStore = new Ext.data.JsonStore({
url: 'data.json',
root: 'rows',
autoLoad: true,
fields: [{
name: 'person',
type: 'string'
}, {
name: 'product',
type: 'string'
}]
});
var grid = new Ext.grid.GridPanel({
id: 'gridPanel',
title: 'Grid example',
width: 250,
height: 250,
renderTo: 'grid-example',
store: myStore,
columns: [{
header: 'Person',
dataIndex: 'person'
}, {
header: 'Product',
dataIndex: 'product'
}]
});
});
My JSON data is:
{
"rows": [{
"person": "Jamie Avins",
"product": "Ladder"
}, {
"person": "Ed Spencer",
"product": "Spanner"
}]
}
Do you have any ideas what's wrong? Can somebody give me some hints?
Thanks in advance!
Seha
Use:
reader: {
type: 'json',
root: 'rows'
}
in your JsonStore and validate your json response.
Hope it will work for you.
I tested your code and the exact JSON string cut and pasted from your example. It worked just fine using Grails to return the string (not reading from a file). Firebug did not complain. Looks like the file itself could be the issue, maybe a hidden character is creeping in.

Ext JS 4 using JSON in proxy reader will not load records

I am trying to use Ext JS 4 and was playing with one of the grid examples and wanted to use JSON rather than XML. No mater how I code the JSON data, I get no records to load.
Here is my code:
Ext.define('Plant', {
extend: 'Ext.data.Model',
fields: [{
name: 'common',
type: 'string'
}, {
name: 'botanical',
type: 'string'
}, {
name: 'light'
}, ]
});
// Create the Data Store.
var store = Ext.create('Ext.data.Store', {
// Destroy the store if the grid is destroyed.
autoDestroy: true,
model: 'Plant',
proxy: {
type: 'ajax',
url: 'plants.json',
reader: {
type: 'json',
root: 'records'
}
},
sorters: [{
property: 'common',
direction: 'ASC'
}]
});
Here is my data:
{
"records": [
{
"common": "Bloodroot",
"botanical": "Sanguinaria canadensis",
"light": "Mostly Shady"
}, {
"common": "test",
"botanical": "I do not know",
"light": "Mostly Shady"
}
]
}
The XML reader works great, but we want to use JSON.
Thanks in advance
Have a look at this thread! You need to check your url path to plants.json, path starts from 'index.html' or some other similar starting point , and not the .js file where store is located. I tested your code and it works fine, also use autoLoad:true in your Ext.data.Store, i dont see your grid code so.. Cheers!

Simple JSON example with senchaTouch

I'm trying to achieve a simple JSON operation:
I want to write on my page a text coming from a file in JSON format.
The file is like this: (data.json)
{
"id": "0",
"name":"myname"
}
The script is getting the JSON:(main.js)
Ext.setup({
onReady: function() {
Ext.regModel('Person', {
fields: [
{name: 'id', type: 'string'},
{name: 'name', type: 'string'}
]
});
//I want the name to be written on the page
var itemTemplate = new Ext.XTemplate(
'<tpl for=".">',
'{name}',
'</tpl>');
// I get and decode the Json from data.json
var jsonStore = new Ext.data.Store({
model: "Person",
proxy: {
type: 'ajax',
url: 'data.json',
reader: {
type: 'json'
}
},
autoLoad: true
});
// The panel should get the stored Result and display it
var jsonPanel = new Ext.Panel ({
title: "json",
fullscreen: true,
items: [
{
xtype: 'list',
store: jsonStore,
itemTpl:itemTemplate,
}
]
});
}
});
The index.html file calls all the files above and sencha-touch.js and .css.
I just don't manage to see anything written on the page.
If someone can give me a clue about what i am doing wrong it would help a lot.
Try to put your JSON objects in array notation, like this:
[{
"id": "0",
"name":"myname"
}, {
"id": "1",
"name":"myname2"
}]
As far as I can remember (at least that was the case in ST1, I did not try with ST2), the Ajax reader cannot be used to access local files. You need to have your json data delivered through a web server