Wednesday 5 September 2018

JSON store does not return data correctly

I've had this problem for awhile now, but I keep narrowing down the issue and I think I have main problem figured out now. While my JSONstore calls to the PHP correctly, and firebug does give me the correct data back from the database, it will not display in my grid.

I have a grid with only 2 columns a date, and an entry from a log. When I take out the log entry, the grid will display the date just fine, and when I just create an array with static data from the log then it will display fine in the grid as well. I think my JSONstore just isn't parsing the data correctly from the mysql tables.
My code is:
  var logStore = new Ext.data.JsonStore({
    autoLoad: true,
    url: 'inc/interface/config.php?list=messages',
    root: 'dates',
    idProperty: 'ID',
    fields: ['ID', 'ReceivedAt', 'Message'],
    listeners: {
                loadexception: function() {
                    console.log('load failed -- arguments: %o', arguments);
                }
        }
}); 

  var logGrid = new Ext.grid.GridPanel({
        region: 'center',
        store: logStore,
        colModel: new Ext.grid.ColumnModel({
            columns: [{
                id: 'received',
                header: 'Received',
                dataIndex: 'ReceivedAt',
                width: 250
            },{
                id: 'message',
                header: 'Logs',
                dataIndex: 'Message',
                width: 750
            }]
        })
    });

An example of the log entry it's pulling is:
| Apr 9 00:00:02 dh1 dhcpd: Added new forward map from nfxxxxich-PC.wifi-uhs.osu to 10.xxx.xx.248 |
| Apr 9 00:00:02 dh1 dhcpd: added reverse map from 248.xxxx.xxx.10.in-addr.arpa. to nxxxxxh-PC.wifi-uhs.osu |
| Apr 9 00:00:02 dh1 dhcpd: DHCPREQUEST for 10.xxx.xxx.248 from 00:x:5c:x:8c:xx (nxxxxh-PC) via 10.xxx.xxx.254 |
| Apr 9 00:00:02 dh1 dhcpd: DHCPACK on 10.X.XX.248 to 00:X:5c:X8c:XX (nXXXich-PC) via 10.193.XX.XXX |
| Apr 9 00:00:02 dh1 dhcpd: Added new forward map from XX.wifi-uhs.XXX to 10.X.X.242 |
| Apr 9 00:00:02 dh1 dhcpd: added reverse map from 242.X.193.X.in-addr.arpa. to X.wifi-uhs.XXX |
| Apr 9 00:00:02 dh1 dhcpd: DHCPREQUEST for 10.X.X.242 from 00:X:ce:X21:63 (elena) via 10.X.X.254 |
| Apr 9 00:00:02 dh1 dhcpd: DHCPACK on 10.193.XXX.XX to 00:X:ce:X:21:63 (elena) via 10.X.X.254 |
| Apr 9 00:00:02 dh1 dhcpd: Added new forward map from P305-XXX.wifi-uhs.XXX to 10.193.X.X |
| Apr 9 00:00:02 dh1 dhcpd: added reverse map from 21.241.X.X.in-addr.arpa. to P305-XXXX.wifi-uhs.XXX |

There are various different characters and tabs between the date, dh1, and the log entry itself. Could there be an issue with escaping these characters? I'm just not quite sure how it's done. Any help would be appreciated.

You haven't specified the type and format in fields. And I'm guessing that your php json implementation doesn't generate a javascript Date object.
So... try returning an unix timestamp and use this as fields:
fields: [
  {name: 'ID', type: 'int'},
  {name: 'ReceivedAt', type: 'date', dateFormat: 'Y-m-d H:i:s'},
  'Message'
]

Optionally you could also specify a dateFormat next to the type: 'date'. See the documentation for more info: http://www.sencha.com/deploy/dev/docs/?class=Ext.data.Field

0 comments:

Post a Comment