Having trouble populating Extjs Grid with JSON data

In this simple grid, I am attempting to display three columns - Subject ID, Subject Name, and Subject Short Name by obtaining JSON data. The data is stored in a variable called myData in JSON format, which has been received from the server. Despite pasting the data into myData, I am unable to see it displayed on the grid.

var myData = {"subjectStoreRoot":[{"subjectName":"Chemistry","subjectId":"Chem01","subjectShortName":"Chemistry"},{"subjectName":"Mathematics","subjectId":"Math01","subjectShortName":"Maths"},{"subjectName":"English","subjectId":"Eng01","subjectShortName":"Eng"},{"subjectName":"Science","subjectId":"Sci01","subjectShortName":"Sci"},{"subjectName":"Social Studies","subjectId":"SS01","subjectShortName":"Social"},{"subjectName":"Kannada","subjectId":"Kan01","subjectShortName":"Kan"}]};

store =  new Ext.data.ArrayStore({

data:Ext.decode(myData),
autoDestroy : true,
autoLoad:true,
storeId: 'subjectsGridStoreId',
root: 'subjectStoreRoot',
idProperty: 'subjectId',
fields: [

{name: 'subjectId' ,type:'string'},
{name: 'subjectName' ,type:'string'},
{name: 'subjectShortName' ,type:'string'}

]
});

grid = new Ext.grid.GridPanel({
store: store,
stripeRows:true,
scrollable:true,
columnLines:true,

columns: [
{
id       :'subjectId',
header   : 'Subject Id', 
width    : 250, 
sortable : true, 
dataIndex: 'subjectId'
},{
id       :'subjectName',
header   : 'Subject Name', 
width    : 250, 
sortable : true, 
dataIndex: 'subjectName'
},{
id       :'subjectShortName',
header   : 'Subject Short Name', 
width    : 250, 
sortable : true, 
dataIndex: 'subjectShortName'
}
],

height: 300,

autoScroll:true,
title: 'List of all Subjects',

stateful: true,
stateId: 'grid'
});

I would appreciate any guidance on why the data is not showing up on the grid.

Answer №1

When working on this task, it's important to keep a few key points in mind:

  1. Instead of using Ext.data.ArrayStore, opt for an Ext.data.JsonStore. The provided data is not just a simple array of arrays (more details about Ext.data.ArrayStore can be found here).
  2. Avoid utilizing Ext.decode.

To address the mentioned issues, I have prepared a solution on jsFiddle along with the corrected code snippet below:

var myData = {
    "subjectStoreRoot": [{
        "subjectName": "Chemistry",
        "subjectId": "Chem01",
        "subjectShortName": "Chemistry"
    }, {
        "subjectName": "Mathematics",
        "subjectId": "Math01",
        "subjectShortName": "Maths"
    }, {
        "subjectName": "English",
        "subjectId": "Eng01",
        "subjectShortName": "Eng"
    }, {
        "subjectName": "Science",
        "subjectId": "Sci01",
        "subjectShortName": "Sci"
    }, {
        "subjectName": "Social Studies",
        "subjectId": "SS01",
        "subjectShortName": "Social"
    }, {
        "subjectName": "Kannada",
        "subjectId": "Kan01",
        "subjectShortName": "Kan"
    }]
};

store = new Ext.data.JsonStore({
    data: myData.subjectStoreRoot,
    autoDestroy: true,
    autoLoad: true,
    storeId: 'subjectsGridStoreId',
    // reader configs
    root: 'subjectStoreRoot',
    //  
    idProperty: 'subjectId',
    fields: [
    {
        name: 'subjectId',
        type: 'string'
    },
    {
        name: 'subjectName',
        type: 'string'
    },
    {
        name: 'subjectShortName',
        type: 'string'
    }]
});

grid = new Ext.grid.GridPanel({
    renderTo: Ext.getBody(),
    store: store,
    stripeRows: true,
    scrollable: true,
    columnLines: true,
    columns: [{
        id: 'subjectId',
        header: 'Subject Id',
        width: 250,
        sortable: true,
        dataIndex: 'subjectId'
    }, {
        id: 'subjectName',
        header: 'Subject Name',
        width: 250,
        sortable: true,
        dataIndex: 'subjectName'
    }, {
        id: 'subjectShortName',
        header: 'Subject Short Name',
        width: 250,
        sortable: true,
        dataIndex: 'subjectShortName'
    }],
    //   autoExpandColumn: 'subjectName',
    height: 300,
    //  width: 350,
    autoScroll: true,
    title: 'List of all Subjects',
    // config options for stateful behavior
    stateful: true,
    stateId: 'grid'
});

Answer №2

To easily replace an arrayStore, you can opt for either store or jsonStore. Next, ensure to assign the configuration "data" as myData.subjectStoreRoot.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Displaying an HTML button above JavaScript code

Hello, I am currently working on a project that involves displaying the Earth and I am in need of assistance with positioning some buttons on the screen. Currently, I am facing an issue where the buttons appear either above or below the JavaScript code. I ...

Tips for transitioning frontend JS to Angular 2 for seamless integration with a PHP MVC backend system

I currently have a robust PHP MVC web application that utilizes jQuery for click events and modal dialog rendering. To align with up-to-date frontend technologies, I am looking to revamp the JavaScript code to function within Angular 2. However, I am faced ...

What is the best way to prevent an element from reaching the border of the screen?

As a JavaScript beginner, I am working on creating a simple game. My objective is to prevent the player (20px x 20px) box from causing the screen to scroll. I want a fixed screen where the player cannot go beyond the edges of the screen. Below are my previ ...

Utilizing the Authorization Header in WebSocket within a React Electron Application

Struggling to establish a connection with a secure websocket that requires Bearer Auth via Header. Despite popular advice, it seems setting headers for WebSockets is not straightforward. How can I achieve this in a React Electron App? Currently using the & ...

Switch the text style when hovering, then switch it back upon second hovering

Searching for a method to modify the font of individual letters when hovering over them. The goal is for the letters to revert back to their original font after a second hover (not on the first hover-off). Any assistance would be greatly valued! ...

Is there a way to retrieve the Incoming Message object in Angular using HttpClient?

From my Angular file, I am sending this request to the server. Is there a way to access it from my typescript file using a HttpClient object? IncomingMessage { ... // Headers details omitted for brevity url: '/teradata/databases/view/djfg', ...

Can OpenLayers library be integrated into Vue CLI 3?

I am currently trying to integrate Openlayers with vue-cli-3, but it seems like I am missing something in the process. Initially, I installed Vue CLI using the following command: npm install @vue/cli -g Next, I added additional dependencies, specifically ...

Creating Location-Specific Customer Data using AngularJS similar to Google Analytics

Looking to create a user registration map similar to Google Analytics without actually using Google Analytics. Can anyone provide assistance with this task? I am utilizing MVC, C#, Sql Server-2014, AngularJS, and jQuery for this project. Despite my efforts ...

Steps to simulate a TouchEvent programmatically using TypeScript

Is there a way to manually trigger the touch event in TypeScript? In JavaScript, it was possible but I am struggling to achieve the same in TypeScript. For example: let touchStart: TouchEvent = document.createEvent('TouchEvent'); touchStart.i ...

Utilizing a promise instead of making a jQuery ajax request

A scenario I am facing involves a function that is set to execute jquery.ajax and return it as a promise for future processing. However, in certain cases, the function possesses enough information to proceed synchronously without triggering the ajax call. ...

Connecting to the Wordpress server using RestKit

Trying to send a JSON to a local Wordpress server, with the following mapping: [RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/html"]; RKObjectMapping *orderEntryMapping = [RKObjectMapping requestMapping]; [orderE ...

React Application Issue 'Error: React is not defined'

I've been working on developing an app using react, but for some reason, it's not functioning properly and I'm struggling to pinpoint the issue. The code compiles without errors using babelify, however, it throws an exception during executio ...

Transform a collection of hierarchical strings into JSON using C#

I have a series of strings that represent hierarchical information and are separated by hyphens (3 hyphens indicate separation). My goal is to convert this list into a JSON format so that I can link it to a tree control component. I am in search of a C# s ...

Exploring React: Opening a new page without rendering the SideBar component

I currently have an application with a sidebar that navigates to three different pages, all of which include a navigation bar and the sidebar. However, for the next page I want to exclude the sidebar but still include the navigation bar. How can I configur ...

"Troubleshooting: Click counter in HTML/Javascript unable to function

My latest HTML project is inspired by the "cookie clicker" game, and I'm working on it for a friend. So far, I've managed to get the click counter to function to some extent. Essentially, when you click on the image, the number at the bottom sho ...

Troubleshooting ng-click not functioning within ng-repeat with database integration in MEAN Stack

//app.js var blogApp = angular.module('BlogApp', []); blogApp.controller('BlogController', function($scope, $http){ $scope.createPost = createPost; $scope.deletePost = deletePost; function init(){ getAllPosts(); } init(); ...

XMLHttpRequest Failing to Retrieve Data - Error Code 202

Currently, I am immersed in a project that involves using a webservice (specifically, VB.Net and Javascript). While debugging the code, I encountered an issue with the XmlHttpRequest.status returning as 202. Upon comparison with my previous webservice proj ...

I'm trying to set it up so that an image pops up when I hover over text. I've tried incorporating a few different JavaScripts, but I still can

I'm struggling to display an image on my website. I have the necessary code parts, but it's not working as expected. function showImage() { $('.img').addClass('display'); } function hideImage() { $('.img'). ...

Effort to update Div element with Ajax fails to function

Looking for some assistance here. I'm attempting to update a database's entries using Ajax after submitting a form. The entries are displayed within a div. I've tried the following code to refresh only that specific div, but it doesn't ...

Ways to retrieve dropdown values

My HTML code is as follows: <select class="height_selected"> <option>Select Height</option> <option ng-model="userHeight" ng-init="1" value="1">123 cm</option> <option ng-model="userHeight" ng-init="2" v ...