Guide applies to: modern

1.Create the Modern Grid class

Create a new file app/desktop/src/view/TunesGrid.js with the following code. Note the xtype.

  1. Ext.define('ModernTunes.view.TunesGrid', {
  2. extend: 'Ext.grid.Grid',
  3. xtype: 'tunesgrid',
  4. cls: 'tunes-grid'
  5. });

2.In MainView.js, add an Instance of TunesGrid

  • Replace the html placeholder we had for the dataview in the items array for Grid with the xtype assigned to the TuneGrid class, called tunesgrid.
  1. xtype: 'tunesgrid',
  • bind its store property to the '{tunes}' setting from the MainView’s viewModel
  1. bind: {
  2. store: '{tunes}'
  3. }

3.Add Columns to the Grid

  • Return to edit app/desktop/src/view/TunesGrid.js and add the following after the xtype setting:
  1. defaults: {
  2. height: 54
  3. },
  4. columns: [{
  5. text: 'Artist',
  6. dataIndex: 'artist',
  7. flex: 1
  8. }, {
  9. text: 'Title',
  10. dataIndex: 'title',
  11. flex: 1
  12. }, {
  13. text: 'Release Date',
  14. dataIndex: 'release_date',
  15. flex: .5
  16. }, {
  17. text: 'Thumbnail',
  18. dataIndex: 'image',
  19. tpl: '<div class="grid-image" style="background-image:url(\'{image}\')"></div>',
  20. cell: {
  21. encodeHtml: false
  22. }
  23. }],
  • The dataIndex config on each cell is referencing a field in the feed to populate that column, accessible due to the proxy.

Since you are adding these column and cell types, their source classes need to be added to the requires array to be included in the build:

  1. requires: [
  2. 'Ext.grid.column.Column',
  3. 'Ext.grid.cell.*'
  4. ],

When you are finished, save your work and refresh the browser (running as a modern app). You should see the grid when clicking the second tab.

4.Using Tpl and Cell Configs to Render an Image to a Column

  • The first 3 columns are straightforward displays of content in the column of a grid

  • Cells are the defining element for content in a grid rendered from the Modern Toolkit

  • On that fourth cell we are using a tpl config to inject HTML and CSS on the data available to the dataIndex property so that the image field’s value can be used to render an image to the Thumbnail column

    1. cell: {
    2. encodeHtml: false
    3. },
  • The above config allows for the execution of HTML in the cell to display an image rather than the literal text of the img tag code being displayed.

5.Add a Style sheet for the Grid

These styles will tweak the grid to provide proper sizing and consistent spacing for the images in the Thumbnails column, among other things.

Again, make sure you create the stylesheet the same as the class file and save it in the same directory with an .scss extension, app/desktop/src/view/TunesGrid.scss.

To recognize the classes being used, see the cls config on the grid (tunes-grid) class and the class settings in the template column (grid-image).

  1. .tunes-grid {
  2. background-color: #eeeeee;
  3. transition: filter 250ms;
  4. .grid-image {
  5. width: 100%;
  6. height: 30px;
  7. background-size: cover;
  8. background-repeat: no-repeat;
  9. }
  10. }
  11. .blur .tunes-grid {
  12. filter: blur(5px) grayscale(80%);
  13. }

6.The code

The file TunesGrid.js should have ended up looking something like this:

  1. Ext.define('ModernTunes.view.TunesGrid', {
  2. extend: 'Ext.grid.Grid',
  3. xtype: 'tunesgrid',
  4. cls: 'tunes-grid',
  5. requires: [
  6. 'Ext.grid.column.Column',
  7. 'Ext.grid.cell.*'
  8. ],
  9. defaults: {
  10. height: 54
  11. },
  12. columns: [{
  13. text: 'Artist',
  14. dataIndex: 'artist',
  15. flex: 1
  16. }, {
  17. text: 'Title',
  18. dataIndex: 'title',
  19. flex: 1
  20. }, {
  21. text: 'Release Date',
  22. dataIndex: 'release_date',
  23. flex: .5
  24. }, {
  25. text: 'Thumbnail',
  26. dataIndex: 'image',
  27. tpl: '<div class="grid-image" style="background-image:url(\'{image}\')"></div>',
  28. cell: {
  29. encodeHtml: false
  30. }
  31. }]
  32. });

When you are finished, save your work and refresh the browser. You should see the grid when clicking the second tab.

Lab: Tunes - Show the Modern Grid - 图1

  • This snapshot uses the mobile simulator in the Chrome browser in order to render the phone pic