Author Topic: Attempting to integrate ParamQuery in Astro without React  (Read 309 times)

Ahmad Hadidi

  • Newbie
  • *
  • Posts: 3
    • View Profile
Attempting to integrate ParamQuery in Astro without React
« on: April 24, 2024, 06:33:22 pm »
Hello everybody,

I've been trying for the past couple of days to integrate ParamQuery into an Astro component with no luck. I have installed pqgrid via npm by packing and doing an npm install after modifying the package.json file. Here's a snippet of my code

First Approach

Code: [Select]
// components/paramquerytest.astro
---

---

<div id="grid_json" style="margin:100px;"></div>

<script>
import pq from "pqgrid";
$(function () {
    var data = [
        { rank: 1, company: 'Exxon Mobil', revenues: 339938.0, profits: 36130.0 },
    ];
    var obj = {
        numberCell:{resizable:true,title:"#",width:30,minWidth:30},
        //editor: {type: 'textbox'},
        title: "ParamQuery Grid with JSON Data",
        resizable:true,
        menuIcon: true,
        scrollModel:{autoFit:true}
    };
    obj.colModel = [
        { title: "Rank", width: 100, dataType: "integer", dataIndx: "rank" },
        { title: "Company", width: 200, dataType: "string", dataIndx: "company" },
        { title: "Revenues ($ millions)", width: 150, dataType: "float", format: '$#,###.00', dataIndx: "revenues" },
        { title: "Profits ($ millions)", width: 150, dataType: "float", format: '$#,###.00', dataIndx: "profits" }
    ];
    obj.dataModel = { data: data };
    $("#grid_json").pqGrid(obj);
});
</script>

in my index.astro file, I regularly import the component and mention it.

I get the error:
Quote
Uncaught SyntaxError: The requested module '/paramquery/index.js' does not provide an export named 'default'

I noticed that if I do console.log(pq) then the console will not show the object

Second approach
If I use the code above in index.js, I also receive nothing but pq's object appears in the console, however with this error:
Code: [Select]
Uncaught TypeError: $(...).pqGrid is not a function
    at HTMLDocument.<anonymous> (index.astro:179:21)
    at c (jquery.min.js:3:7857)
    at Object.fireWith [as resolveWith] (jquery.min.js:3:8658)
    at Function.ready (jquery.min.js:3:3266)
    at HTMLDocument.H (jquery.min.js:3:695)

I'm trying to avoid react, vue, KO, and angular, to keep my work as lightweight as possible.

My theory is that it should work, because I used Highcharts in astro and imported it similar to how I imported 'pqgrid' and it worked.

Any help would be appreciated, thank you very much.
« Last Edit: April 24, 2024, 06:42:32 pm by Ahmad Hadidi »

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6260
    • View Profile
Re: Attempting to integrate ParamQuery in Astro without React
« Reply #1 on: April 24, 2024, 06:53:41 pm »
Are you using any module bundler?

Please let me know if this resolves the error:

Code: [Select]
var pq = require("pqgrid");
« Last Edit: April 24, 2024, 06:57:55 pm by paramvir »

Ahmad Hadidi

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Attempting to integrate ParamQuery in Astro without React
« Reply #2 on: April 24, 2024, 07:00:52 pm »
Hi Paramvir,

Thank you for the quick response. No I don't think Astro uses a module bundler, I'm not sure actually.

The console outputs this message when I changed to var pq = require("pqgrid")

Code: [Select]
index.astro:136 Uncaught ReferenceError: require is not defined
    at index.astro:136:10

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6260
    • View Profile
Re: Attempting to integrate ParamQuery in Astro without React
« Reply #3 on: April 25, 2024, 07:02:16 am »
Not sure about your second approach but if pq object is available in the browser, then you can use pq.grid(...) constructor instead of $.pqGrid(...)

Code: [Select]
pq.grid( "#grid_json", obj )
« Last Edit: April 25, 2024, 07:08:44 am by paramvir »

Ahmad Hadidi

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Attempting to integrate ParamQuery in Astro without React
« Reply #4 on: April 25, 2024, 12:36:17 pm »
Thank you very much, Paramvir for your help. I managed to make it work. Below is my code that shows pqgrid in Astro without JS UI frameworks (e.g. React, Svelte, Solid, etc..).

Code: [Select]
// src/pages/index.astro

---
import Layout from '../layouts/Layout.astro';
import Card from '../components/Card.astro';
---

<Layout title="Welcome to Astro">
<main>
<svg
class="astro-a"
width="495"
height="623"
viewBox="0 0 495 623"
fill="none"
xmlns="http://www.w3.org/2000/svg"
aria-hidden="true"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="M167.19 364.254C83.4786 364.254 0 404.819 0 404.819C0 404.819 141.781 19.4876 142.087 18.7291C146.434 7.33701 153.027 0 162.289 0H332.441C341.703 0 348.574 7.33701 352.643 18.7291C352.92 19.5022 494.716 404.819 494.716 404.819C494.716 404.819 426.67 364.254 327.525 364.254L264.41 169.408C262.047 159.985 255.147 153.581 247.358 153.581C239.569 153.581 232.669 159.985 230.306 169.408L167.19 364.254ZM160.869 530.172C160.877 530.18 160.885 530.187 160.894 530.195L160.867 530.181C160.868 530.178 160.868 530.175 160.869 530.172ZM136.218 411.348C124.476 450.467 132.698 504.458 160.869 530.172C160.997 529.696 161.125 529.242 161.248 528.804C161.502 527.907 161.737 527.073 161.917 526.233C165.446 509.895 178.754 499.52 195.577 500.01C211.969 500.487 220.67 508.765 223.202 527.254C224.141 534.12 224.23 541.131 224.319 548.105C224.328 548.834 224.337 549.563 224.347 550.291C224.563 566.098 228.657 580.707 237.264 593.914C245.413 606.426 256.108 615.943 270.749 622.478C270.593 621.952 270.463 621.508 270.35 621.126C270.045 620.086 269.872 619.499 269.685 618.911C258.909 585.935 266.668 563.266 295.344 543.933C298.254 541.971 301.187 540.041 304.12 538.112C310.591 533.854 317.059 529.599 323.279 525.007C345.88 508.329 360.09 486.327 363.431 457.844C364.805 446.148 363.781 434.657 359.848 423.275C358.176 424.287 356.587 425.295 355.042 426.275C351.744 428.366 348.647 430.33 345.382 431.934C303.466 452.507 259.152 455.053 214.03 448.245C184.802 443.834 156.584 436.019 136.218 411.348Z"
fill="url(#paint0_linear_1805_24383)"></path>
<defs>
<linearGradient
id="paint0_linear_1805_24383"
x1="247.358"
y1="0"
x2="247.358"
y2="622.479"
gradientUnits="userSpaceOnUse"
>
<stop stop-opacity="0.9"></stop>
<stop offset="1" stop-opacity="0.2"></stop>
</linearGradient>
</defs>
</svg>
<h1>Welcome to <span class="text-gradient">Astro</span></h1>
<div id="grid_json" style="margin:100px;"></div>
<!--<Paramquery/>-->
<ul role="list" class="link-card-grid">
<Card
href="https://docs.astro.build/"
title="Documentation"
body="Learn how Astro works and explore the official API docs."
/>
<Card
href="https://astro.build/integrations/"
title="Integrations"
body="Supercharge your project with new frameworks and libraries."
/>
<Card
href="https://astro.build/themes/"
title="Themes"
body="Explore a galaxy of community-built starter themes."
/>
<Card
href="https://astro.build/chat/"
title="Community"
body="Come say hi to our amazing Discord community. ❤️"
/>
</ul>
</main>
</Layout>

<style>
/* some css for the webpage */
</style>
<script>
import pq from 'pqgrid';
console.log({pq});
document.addEventListener("DOMContentLoaded", function(event) {
    //JSON data (array of objects) can be defined locally
    //or might be a response from an AJAX call from web server/service.
    var data = [
        { rank: 1, company: 'Exxon Mobil', revenues: 339938.0, profits: 36130.0 },
        { rank: 2, company: 'Wal-Mart Stores', revenues: 315654.0, profits: 11231.0 },
    ];
                 
    //array of columns.
    var colModel = [
        {
            title: "Rank", //title of column.
            width: 100, //initial width of column
            dataType: "integer", //data type of column
            dataIndx: "rank" //should match one of the keys in row data.
        },
        {
            title: "Company",
            width: 200,
            dataType: "string",
            dataIndx: "company"
        },
        {
            title: "Revenues ($ millions)",
            width: 150,
            dataType: "float",
            align: "right",
            dataIndx: "revenues"
        },
        {
            title: "Profits ($ millions)",
            width: 150,
            dataType: "float",
            align: "right",
            dataIndx: "profits"
        }
    ];
 
    //main object to be passed to pqGrid constructor.   
    var obj = {
        width: 700, //width of grid
        height: 400, //height of grid
        colModel: colModel,
        dataModel: {data: data}
    };   
     
    pq.grid("#grid_json", obj );
 
});
</script>

<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="/public/css/pqgrid.dev.css" />
<link rel="stylesheet" href="/public/css/pqgrid.ui.dev.css" />
<script type="text/javascript" src="/public/js/pqgrid.dev.js" ></script>
<script type="text/javascript" src="/public/js/pqgrid.min.js" ></script>

Looking back, I remember I had a similar code structure like the one above (C.f. https://paramquery.com/pro/tutorial#topic-firstjsgrid) but it was in an astro component not in the index astro file, I think that was the main issue.. Now I need to figure-out how I can add pqgrid as a separate astro component in "src/components/pqgrid-component.astro" for example.