Sencha Touch - List Creation - How to apply a class to the final item in the list

I am currently working on a list creation process using the following format:

        var listTpl = new Ext.XTemplate(
        '<div class="menuListItemContainer">',
            '<h1>MENU</h1>',
            '<tpl for=".">',
                    '<div class="menuListItem">',
                        '{title}',
                    '</div>',
            '</tpl>',
        '</div>',
        '<div class="x-clear"></div>'
    );

In my Json file, the {title} values are being populated. However, I am looking for a way to add a specific class to the last item in the list for styling purposes. Any thoughts on how to accomplish this?

Would appreciate any suggestions!

Answer №1

Although I cannot run this code myself, I believe the following code snippet may be helpful:

let menuTemplate = new Ext.XTemplate(
   '<div class="menuListItemContainer">',
      '<h1>MENU</h1>',
      '<tpl for=".">',
         '<div class="menuListItem {[xindex === xcount ? "extraClass" : ""]}">',
            '{title}',
         '</div>',
      '</tpl>',
   '</div>',
   '<div class="x-clear"></div>'
);

Answer №2

Although this post is dated, I wanted to share my solution for Sencha Touch 2:

The simplest approach is to utilize scss:

(...) - within the nesting of your list-item class, insert the following:

&:first-child { ... implement whatever you require here or consider using a mixin }

&:last-child { ... implement whatever you require here or consider using a mixin } Enjoy ;o)

Answer №3

One approach is to keep your Ext.List definition clean and dynamically add a class to an item template every time the list is updated or rendered.

var onUpdateOrLoad = function(cmp, index, element, e)
{

    var styleClass;
    for (var i = 0; i < cmp.store.data.length; i++)
    {
           if(i%3==0)
            {
            styleClass= 'mm-box-colored-red'; //applied to every third item                          
            }
        Ext.get(cmp.getNode(i)).addCls(styleClass);
    }
}

Incorporate the following code at the end:

   myList.on('update', onUpdateOrLoad);

OR

myList.on('afterrender', onUpdateOrLoad);

Choose based on your requirement

Here is an example of how I utilize this:

A list where the first and last items are rounded, while the rest have angular edges to create a grouped tableView effect.

    var styleClass;
    for (var i = 0; i < cmp.store.data.length; i++)
    {   
        if(i==0)
        {           
            styleClass = 'mm-box-rouded-up';  // apply to the first item in the list
        }else if(i==cmp.store.data.length-1)
        {
            styleClass= 'mm-box-rounded-down';  // apply to the last item in the list
        }
        else{
            styleClass= 'mm-box-rounded-no';
        }
    }

torList.on('update', onUpdateOrLoad);

I hope this explanation is clear and beneficial to you. Thank you

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

Add a new class to an element located in a separate div using JavaScript

$(document).ready(function() { $('.btn').click(function() { $(this).parent().addClass('show'); }); }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div clas ...

Refreshing a Node.js server page upon receiving a JSON update

My web application serves as a monitoring interface for tracking changes in "objects" processed by the computer, specifically when they exceed a certain threshold. The Node Js server is running on the same machine and is responsible for displaying data in ...

Issue with Vue3: The imported module is not defined

Update: I recently downgraded vue-cli to version 4.5.19 and now the code is functioning properly. It seems like there might be an issue related to vue-cli or its dependencies. I encountered a problem while working on a simple vue.js project using vue-cli. ...

Stop Bootstrap 5 Accordion from collapsing

I have successfully implemented the Accordion component in Bootstrap 5, and it is working perfectly! However, I am facing an issue with the size of the collapse button in the accordion, which is too big. I want to add an additional link to it for some ext ...

Creating a unique 3D model through specified coordinates with the use of THREE.js

I possess an array consisting of (x,y,z) vertices coordinates as well as a facets array that stores the indexes of vertices (taken from the first array) for each facet. Each facet may have a varying number of vertices. Is there a way to construct a unique ...

The process of transferring a file from a client to a server and then from one server to another

Within my web application, I am currently working on implementing a file upload feature. I have successfully utilized axios to send file requests from the client to the server's API, including proper form data appending and necessary headers. However, ...

After upgrading to react native version 0.73, the user interface freezes and becomes unresponsive when working with react-native-maps

After upgrading my app to the newest version of react native 0.73.x, I encountered an issue where the UI on iOS starts freezing and becoming unresponsive in production. The main screen loads react-native-maps with numerous markers, and this was not a pro ...

Updating an Angular directive with dynamically added ng-module and ng-change attributes

Can someone check if I'm on the right track? Preamble: I've created a Typeahead class that fetches data and keeps it within itself. The class has the following structure: input: stores the search text. list: stores the results. change: a funct ...

Navigating through the sidebar on Next.js

I am currently exploring how to utilize next.js routes for dynamically populating a sidebar component with route instructions. The goal is to have the main div display the relevant component when a sidebar option is clicked. While I've come across tu ...

The Next JS build process exclusively creates server-side pages

I have noticed that some of my pages like /contact, /about, and /rules are server-side generated even though I am not using getServerSideProps(). Since these pages only contain static images and text without any API requests, I want them to be treated as s ...

Testing Angular 2: Ensuring Component Properly Calls Router with Accurate Arguments

I'm facing an issue with a child component that includes a button element with 'routerlink' attribute for navigation upon clicking. The button does not have a '(click)' handler, only the routerlink. Currently, I am writing a unit ...

Retrieving time zone using offset with javascript

I am looking for a way to schedule time-based events on my server operating in UTC time. For the user interface, I need to input 2 parameters: The local time when the scheduled event should trigger The timezone offset Instead of displaying timezone n ...

Utilize Javascript/Jquery to categorize JSON data based on the days of the week (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)

A function is provided below that retrieves data for a chart. The output produced by this function is structured as follows: [Object { date=Date, value=112, volume=1469}, Object { date=Date, value=124, volume=539}, Object { date=Date, value=114, vo ...

the value contained in a variable can be accessed using the keyword "THIS"

I recently developed a snippet of code that adds a method to a primitive data type. The objective was to add 10 to a specified number. Initially, I had my doubts about its success and wrote this+10. Surprisingly, the output was 15, which turned out to be ...

Explain what a particular kind of list is (not an object)

I am in possession of: - a specific interface called IMyType - several classes that implement this interface: MyType1, MyType2, MyType3 Is there a way for me to create a list that only allows types of IMyType? var myList = new List<Type> {typeof (M ...

What is the best way to retrieve a Rails variable that is restricted to a specific partial?

As a newcomer to Ruby on Rails, I find myself struggling to grasp the larger concept. Any assistance you can offer would be greatly appreciated. Within my application.html.haml file, I utilize =yield to pull content from ranked.html.haml. Currently, this ...

Unable to locate node module when using Npm.require

I've been attempting to utilize the Npm.require method in order to access the ldapjs module for client authentication. Unfortunately, I'm encountering the following error message: var ldap = Npm.require('ldapjs'); Error: Cannot find ...

Modifying the appearance of a Component within a NavLink

I'm currently working on a navbar using NavLink from React-Router-Dom. It's fine to use the 'isActive' prop to style the active Link, but I'm stuck on how to style the subelements inside it. For more specific details, please take a ...

Exploring the source code of NPM public and private packages within the node_modules directory

As someone who is new to javascript development, I want to create a private npm package that cannot be accessed by users. However, I have noticed that I can still view the code of other npm packages labeled as closed-source by entering their node_modules s ...

AngularJS chatbox widget for interactive communication

Currently, I am in the process of developing the back-end for a web application utilizing angularJS. One of the key features is allowing users to communicate with each other through a pop-up chat box similar to those found in Gmail or Facebook. My goal is ...