Enhancing XTemplate in ExtJS 4.2.1 with dynamic data refresh from store

Here's a situation that's quite unique...

A DataView linked to a store is causing me some trouble. In the XTemplate, I want to display the quantity of a specific type of JSON record. Each record has a 'type' property with a value. For example:

data: [
 { type: 'W' },
 { type: 'A' },
 { type: 'W' },
 { type: 'W' }
]

This data consists of 3 JSON objects of type 'W' and 1 JSON object of type 'A'. The count of 'W' should be shown in an HTML element with the class 'total-pending'; 'A', in an HTML element with the class 'total-approved.'

The XTemplate code snippet:

tpl: Ext.create('Ext.XTemplate', 
        '<div style="text-align:center">Leave Requests</div>',
        '<table cellpadding="0" class="table-wrap">',
            '<tr>',
                '<td class="x-leave-request approved"><span class="total-approved">0</span>Approved</td>',
                '<td class="x-leave-request pending"><span class="total-pending">0</span>Pending</td>'denied">0</span>Denied</td>',
            '</tr>',
        '</table>'
    )

I've done some research on templates and stores, but I'm struggling to update those counts. I attempted to use a function as a callback for store.on('load', function{}) to change the content of the HTML element like Ext.get(Ext.query('span.total-pending')[0]).update(3) but it didn't work.. Any suggestions?

Answer №1

Maybe this tip will be helpful for your request, give it a try and share the results with us.

Create a function to calculate the total values of W and A, then call the appropriate function within the XTemplate using this.function()

tpl: Ext.create('Ext.XTemplate', 
    '<div style="text-align:center">Leave Requests</div>',
    '<table cellpadding="0" class="table-wrap">',
        '<tr>',
            '<td class="x-leave-request approved"><span class="total-approved">{totalw:this.getTotalw()}</span>Approved</td>',
            '<td class="x-leave-request pending"><span class="total-pending">{totala:this.getTotala()}</span>Pending</td>'denied">0</span>Denied</td>',
        '</tr>',
    '</table>'
)

Answer №2

After receiving a helpful hint from Oguz, I successfully tackled the problem at hand. My approach involved creating an Ext.util.Hashmap instance with pairs representing status type and total count (e.g. <'W', 10>). I then executed the following code snippet:

<tpl for=".">
 {[this.updateStatusTotal(values.type)]}
</tpl>

...

updateStatusTotal : function(type) {
 var me = this,
     map = me.map,
     total = map.get(type);

 map.put(type, ++total);
}

Further along in the code, where the <td> elements are located, I called:

{[this.getStatusTotal('W')]}

Task accomplished :)

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

Having trouble running nodemon on my Windows 10 machine

Recently, I started diving into Node.js and managed to run my first node app successfully, albeit without using nodemon. To remedy this, I globally installed nodemon by running npm install -g nodemon, which went smoothly. However, upon executing nodemon in ...

SDK for generating templates with JavaScript/jQuery

I am in the process of developing an SDK in JavaScript/jQuery that can generate templates based on user input, such as profile templates and dialog templates. These templates require data from an AJAX call to be created. User input should include certain ...

Is it possible for me to create an If statement that can verify the current index within a Map?

I have the following TypeScript code snippet: export default class SingleNews extends React.Component<INews, {}> { public render(): React.ReactElement<INews> { return ( <> {this.props.featured ...

What is the correct way to integrate the Ant Design library with Next.js for seamless server-side rendering?

I duplicated the official Next.js example using Ant Design at this link: https://github.com/vercel/next.js/tree/canary/examples/with-ant-design After cloning, I proceeded with npm install to install all dependencies. Then, I ran npm run dev to check if ev ...

What is the proper way to update data in reactjs?

I previously had code that successfully updated interval data in the browser and locale without any issues. class Main extends Component { constructor(props) { super(props); this.state = {data: []} } componentWillMount() { fetch('fi ...

Iterate over a collection of HTML elements to assign a specific class to one element and a different class to the remaining elements

Forgive me if this is a silly question, but I have a function named selectFace(face); The idea is that when an item is clicked, it should add one class to that item and another class to all the other items. This is what I currently have: HTML <div c ...

Unable to update a single object within an array using the spread operator

I am currently working on updating an object within an array and have encountered some issues. In my initial code, I successfully updated a specific property of the object inside the array like this: var equipment = this.equipments.find((e) => e.id === ...

Using triple nested for loops in Python to iterate through a JSON object

I am trying to extract the latitude and longitude data from a Python object. Here is a snippet of the object: { "Siri": { "ServiceDelivery": { "ResponseTimestamp": "2014-08-09T15:32:13.078-04:00", "VehicleMonitoringDelivery": [ { "VehicleAct ...

Generate a byte array in JavaScript

How can a byte array be created from a file in JavaScript (or Typescript) to send to a C# WebApi service and be consumable by the C# byte array method parameter? Could you provide an example of the JavaScript code? The context here is an Angular 4+ applic ...

Tips for referencing a string in JavaScript

I am trying to use the showmodal method, but I keep getting an error when passing a string. It works fine with integers, but how can I pass a string in JavaScript? <script> var table = ' <table id="example" class="table table-striped " w ...

Switch between playing and pausing the mp3 audio in the Next application by using the toggle

I am currently working on my website and I have been trying to add a button to the bottom of the page that can play or pause a song using useSound library. The song starts playing when I click it for the first time, however, I am facing difficulty in stopp ...

Tips for transforming a date into a time ago representation

Can someone help me with converting a date field into a "timeago" format using jquery.timeago.js? $("time.timeago").timeago(); var userSpan = document.createElement("span"); userSpan.setAttribute("class", "text-muted"); userSpan.appendChild(document.crea ...

Obtain the printed value of a button via PHP and manipulate it using JavaScript

I have a dynamic table that displays results from a database. <table id="table" class="table datatable-responsive"> <thead> <tr class="bg-teal"> <th>#</th> <th>Date & Time</th& ...

org.codehaus.jackson.map.JsonMappingException: Unable to locate a serializer for the specific class org.apache.struts.action.ActionServletWrapper

Exploring the potential workload required for migrating our older Struts 1.x web application to AngularJS + Java RESTful Web Service is on my agenda. Admittedly, Struts 1.x doesn't seamlessly integrate with AngularJS. One key challenge I've enco ...

Can the animation be looped using setInterval()?

Once the images finish sliding in the animation, they continue to slide right endlessly. I've attempted using a setTimeout function to move the images back left and restart the animation, but this causes the setInterval animation to stop. Is there a w ...

The Vue router-view is mistakenly loading the parent component instead of displaying its own content

Here is a simple route configuration: { path: '/', component: Home, }, This route configuration sets the path to the home page and loads the Home component when the path is '/'. However, I am encountering an issue where the Po ...

Combining AddClass and RemoveClass Functions in Mootools Event Handlers

I am currently in the process of creating a CSS animation, and one aspect involves changing the class name of the body at specific intervals. Since I am relatively new to Mootools (and JavaScript in general), my approach has been to add/remove classes to ...

What is the correct way to set up a click event listener for a radio button?

Currently, I am in the process of dynamically generating HTML radio buttons. Each button is assigned an Id stored in a variable. My goal is to add a click event handler to these radio buttons using the assigned Id. However, I am encountering an issue where ...

Tips for utilizing the useState Hook in NextJs to manage several dropdown menus efficiently:

Currently, I am in the process of designing an admin panel that includes a sidebar menu. I have successfully implemented a dropdown menu using the useState hook, but it is not functioning exactly as I had envisioned. My goal is to have the if statement onl ...

Learning the process of reading a string using cJSON

I've been trying to work with strings using cJSON library, but I'm facing some issues. Here's the code snippet: void myfile() { cJSON* type = NULL; char text1[]="{\n\"name\": \"Jack (\\\"Bee\&bsol ...