iamyuu / gridjs-svelte

A Svelte wrapper component for Grid.js
https://npm.im/gridjs-svelte
MIT License
57 stars 13 forks source link

How to dynamically append data to a table? #1

Closed atresnjo closed 3 years ago

atresnjo commented 3 years ago

I'm trying to append data dynamically to a table, how can I accomplish this? The table isn't updating.

<script>
  import Grid from "gridjs-svelte"

  let data = [
    { name: "John", email: "john@example.com" },
    { name: "Mark", email: "mark@gmail.com" },
  ]

    const add = () => {
        const item = {name: 'test', email: 'test'}
        data = data.slice(0).concat(item)
        console.log(data)
    }
</script>

<button on:click={add}>
    add
</button>
<Grid data={data} />

<style global>
  @import "https://cdn.jsdelivr.net/npm/gridjs/dist/theme/mermaid.min.css";
</style>

https://svelte.dev/repl/3a3035a8858d4c60b1683497b1a8c559?version=3.35.0

Thanks! :)

atresnjo commented 3 years ago

Solved it myself.

Feels hacky though, so feel free to suggest another way.

instance.updateConfig({ data }).forceRender();