Our blazing fast Grid component built with pure JavaScript


Post by scottlin »

Dear Sirs,

I'm new to Bryntum Grid and trying to integrate it with backend Django framework.

The first step of my test is to put Bryntum Grid component into the Django template structure.

There are several files:

  1. bryntum.html
    • the foundation template defined in django template structure to include bryntum grid component and css.
  2. indexBryntum.html
    • the index page for the client application that extend the bryntum template and execute bryntum_grid.js
    • this index page is rendering by the views.index() function in views.py of django.
  3. bryntum_grid.js
    • is a copy of the \grid-5.6.5-trial\examples\basic\app.module.js
  4. urls.py
    • the django url redirector.
  5. views.py
    • the django views that accepts the request from frontend, access the backend model and provide the response to frontend.

The issue is that I can not create the grid instance with Grid() constructor.

========================================================================

// bryntum_grid.js

import { Grid, DataGenerator } from '../../build/grid.module.js';
import shared from '../_shared/shared.module.js';

new Grid({...
});

========================================================================

The error message is:
Uncaught SyntaxError: Cannot use import statement outside a module (at bryntum_grid.js:3:1)

When I mark out the import statement:

// bryntum_grid.js

//import { Grid, DataGenerator } from '../../build/grid.module.js';
//import shared from '../_shared/shared.module.js';

new Grid({...
});

========================================================================

I got another error message:
bryntum_grid.js:6 Uncaught ReferenceError: Grid is not defined

Please advise how shall I call the Grid() constructor to create a grid instsance.

Please note that the Bryntum grid component and css library have been loaded in the django template structure. See below the backend django messages:

========================================================================
System check identified no issues (0 silenced).
January 25, 2024 - 22:29:13
Django version 4.2, using settings 'tutorial.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

[25/Jan/2024 22:29:28] "GET / HTTP/1.1" 200 9630
[25/Jan/2024 22:29:28] "GET /static/js/components/bryntum_grid.js HTTP/1.1" 200 1323
[25/Jan/2024 22:29:28] "GET /static/js/bryntum/build/grid.stockholm.css HTTP/1.1" 200 230817

[25/Jan/2024 22:29:28] "GET /static/js/bryntum/build/grid.module.js HTTP/1.1" 200 2148051

Following are the details of these files:

  1. bryntum.html
    ========================================================================
    <!--bryntum.html-->
    {% load static %}
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <!-- Importing bryntum grid library and stylesheet -->
    <script type="text/javascript" src="{% static 'js/bryntum/build/grid.module.js' %}"></script>
    <link rel="stylesheet" type="text/css" href="{% static 'js/bryntum/build/grid.stockholm.css' %}" />
    
    {% block title %}<title></title>{% endblock %}
    </head>
    <body>
    {% block content%}
    {% endblock %}
    </body>
    </html>
    ========================================================================
    1. indexBryntum.html
      ========================================================================
      {% extends 'bryntum.html' %}
      {% load static %}
      
      <html>
      <head>
      {% block title %}<title>Welcome to Bryntum</title>{% endblock %}
      </head>
      <body>
      {% block content%}
      <div id="container" style="height: 100%;"></div>
      
      <style>
       .container{
      padding:10px;
       }
      </style>
      
      <script type="text/javascript" src="{% static 'js/components/bryntum_grid.js' %}"></script>
      
      {% endblock %}
      </body>
      </html>
      ========================================================================
      1. bryntum_grid.js
        ========================================================================
        // bryntum_grid.js
        
        import { Grid, DataGenerator } from '../../build/grid.module.js';
        import shared from '../_shared/shared.module.js';
        
        new grid({[
        
        appendTo : 'container',
        
        features : {
            group : false
        },
        
        // Headers will ripple on tap in Material theme
        ripple : {
            delegate : '.b-grid-header'
        },
        
        columns : [
            {
                text   : 'Name',
                field  : 'name',
                flex   : 2,
                editor : {
                    type     : 'textfield',
                    required : true
                }
            }, {
                text  : 'Age',
                field : 'age',
                width : 100,
                type  : 'number'
            }, {
                text  : 'City',
                field : 'city',
                flex  : 1
            }, {
                text  : 'Food',
                field : 'food',
                flex  : 1
            }, {
                text     : 'Color (not sortable)',
                field    : 'color',
                flex     : 1,
                sortable : false,
                renderer({ cellElement, value }) {
                    // renderer that sets text color = text
                    cellElement.style.color = value;
                    return value || '';
                }
            }
        ],
        
        data : DataGenerator.generateData(50)
        });
        ========================================================================
        1. urls.py
          ========================================================================
          # quickstart/urls.py
          
          from django.urls import include, path
          
          from quickstart import views
          
          urlpatterns = [
          	path('', views.index, name='index'),
          ]
          ========================================================================
          1. views.py
            ========================================================================
            # quickstart/views.py
            
            from django.shortcuts import render
            
            def [b]index[/b](request):
               return render(request, 'quickstart/indexBryntum.html')
            ========================================================================

Post by tasnim »

Hi,

Welcome to Bryntum Forums 🎉😊

I'm attaching a simple Django application here (created by reading the Django docs because I'm not a python/django expert ) that renders the Bryntum Grid properly
Here is the screenshot of the working example

Screenshot 2024-01-26 111252.png
Screenshot 2024-01-26 111252.png (98.55 KiB) Viewed 446 times

Setting the type to module in the script tag should resolve the issue. But still, I'm attaching the Django app so you can compare yours with it.

To run the Python server, please use the following command

$ py manage.py runserver

And after that please go to this http://127.0.0.1:8000/polls/ URL to see the Grid rendered.

If you have any other questions or concerns, please don't hesitate to contact us. We're here for your help.

Best of luck :),
Tasnim

Attachments
django-bryntum-grid.zip
(24.52 MiB) Downloaded 28 times

Post by scottlin »

Dear Tasnim,

It worked now. Thanks for your timely support!

The remaining issue I have is how to put the following statement into the "bryntum.html" to be shared with one or more applications.

import { Grid, DataGenerator } from '../../js/bryntum/build/grid.module.js';
<!--bryntum.html-->
{% load static %}
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Importing bryntum grid library and stylesheet -->
<link rel="stylesheet" type="text/css" href="{% static 'js/bryntum/build/grid.stockholm.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'js/bryntum/css/main.css' %}" />

{% block title %}<title></title>{% endblock %}
</head>
<body>
	{% block content%}
	{% endblock %}
</body>
</html>

Post by tasnim »

Hi,

According to the app that I've provided it would look something like this

{% load static %}

<html>
    <head>
        <title>Bryntum Grid</title>
        <link rel="stylesheet" type="text/css" href="{% static 'polls/build/grid.stockholm.css' %}" />
        <link rel="stylesheet" type="text/css" href="{% static 'polls/css/main.css' %}" />
    </head>
    <body>
        <div id="container"></div>
        <script type="module">
            import { Grid, DataGenerator } from '{% static "polls/build/grid.module.js" %}';
        </script>
    </body>
</html>

Hope this helps.


Post by scottlin »

Dear Tansim,

It seems the import objects in html can not be shared by other javascript.

See error message below:

Uncaught ReferenceError: Grid is not defined
    at bryntum_grid.js:6:1

I used to have extjs components defined as below, which can be shared by multiple javascripts.

<!--extjs.html-->
{% load static %}
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Importing Extjs library and Extjs stylesheet -->
<script type="text/javascript" src="{% static 'js/ext/build/ext-all.js' %}"></script>
<script type="text/javascript" src="{% static 'js/ext/build/packages/charts/classic/charts.js' %}"></script>
<script type="text/javascript" src="{% static 'js/ext/packages/ux/classic/src/TreePicker.js' %}"></script>

<!--Extjs theme -->
<link rel="stylesheet" type="text/css" href="{% static 'js/ext/build/classic/theme-classic/resources/theme-classic-all.css' %}"/>
<link rel="stylesheet" type="text/css" href="{% static 'js/ext/build/packages/charts/classic/classic/resources/charts-all.css' %}">

{% block title %}<title></title>{% endblock %}
</head>
<body>
	{% block content%}
	{% endblock %}
</body>
</html>

See the code below:

  1. bryntum.html
  2. indexBryntum.html
  3. bryntum_grid.js

It only work with #3, but failed with #1 or #2.

<!--bryntum.html-->
{% load static %}
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- Importing bryntum grid library and stylesheet -->
<!--script type="text/javascript" src="{% static 'js/bryntum/build/grid.module.js' %}"></script-->

<script type="module">
        import { Grid, DataGenerator } from '{% static "js/bryntum/build/grid.module.js" %}';
</script>

<link rel="stylesheet" type="text/css" href="{% static 'js/bryntum/build/grid.stockholm.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'js/bryntum/css/main.css' %}" />

{% block title %}<title></title>{% endblock %}
</head>
<body>
	{% block content%}
	{% endblock %}
</body>
</html>

indexBryntum.html

{% extends 'bryntum.html' %}
{% load static %}

<html>
<head>	
	{% block title %}<title>Welcome to Bryntum</title>{% endblock %}
</head>
<body>
	{% block content%}
		<div id="container" style="height: 100%;"></div>

	<style>
	  .container{
		padding:10px;
	  }
	</style>

	<!--script type="module">
			import { Grid, DataGenerator } from '{% static "js/bryntum/build/grid.module.js" %}';
	</script-->
	
	<script type="module" src="{% static 'js/components/bryntum_grid.js' %}"></script>
	
{% endblock %}
</body>
</html>
// bryntum_grid.js

//import { Grid, DataGenerator } from '../../js/bryntum/build/grid.module.js';
//import shared from '../_shared/shared.module.js';

new Grid({

appendTo : 'container',
...

Post by tasnim »

Hi,

Could you please share a runnable test case (please also add some details on how to run the test case) with us so we can check what's wrong?


Post Reply