ExtJS - how to loop over a store and get a total count of each number of items found in a certain column

Viewed 211

I currently have a store with a list of people and it contains certain details about them (ie. name, position, age).

My goal is to have two fields at the top of my page - one which gets the total count of students and the other gets the total count of teachers.

What is the best way to do this?

        {
            xtype: 'numberfield',
            name: 'studentcount',
            fieldLabel: 'Student Count',
            minValue: 0,
            readOnly: true,
        },
        {
            xtype: 'numberfield',
            name: 'teachercount',
            fieldLabel: 'Teacher Count',
            minValue: 0,
            readOnly: true
        }
    columns:[
            { text: 'Name', dataIndex: 'name', editor:'textfield'},
            // position is either 'student' or 'teacher'
            { text: 'Position', dataIndex: 'position', editor:'textfield'},
            { text: 'Age', dataIndex: 'age', editor:'textfield'},
        ]
        let store = Ext.create('schoolstore',{

            listeners: {
                'update': function(store, record){

                }
            }
        })
**Name - Position - Age**

John, Teacher, 44
Andrew, Teacher, 40
Stan, Student, 15
Jane, Student, 14
Alice, Student, 16
Mark, Teacher, 50

I'm new to using ExtJS so any help at all is appreciated, I'm not even sure if using the update listener is the correct way to begin?

I have looked elsewhere including the Sencha forums (which seem to be down at the moment?) but I can't seem to get a specific answer to my issue.

1 Answers

This may work for you.

let array = [
   { name: 'John', position: 'teacher', age:'44'},
   { name: 'Andrew', position: 'teacher', age:'40'},
   { name: 'Stan', position: 'student', age:'15'},
   { name: 'Jane', position: 'student', age:'14'},
   { name: 'Alice', position: 'student', age:'16'},
   { name: 'Mark', position: 'teacher', age:'50'},
];

function arrayFilter(array, query) {
  return array.filter(element => {
    return (
      element.position
        .toString()
        .toLowerCase()
        .indexOf(query.toString().toLowerCase()) !== -1
    );
  });
};

let studentsCount = arrayFilter(array, 'student').length;
let teachersCount = arrayFilter(array, 'teacher').length;

console.log(studentsCount);
console.log(teachersCount);

Related