Datatable component does not refresh after new data is added


Topic: Datatable component does not refresh after new data is added

alana_vue asked 4 years ago

Hello, I am using the datatable 2 component, the data is being passed through an API. The data passed to the datatable is not static so that i will increase as the user add more data to the database. I have found 2 issues: 1.-) If the table columns are not predefined, the component will not render any column.
2.) When new data is added to the table (new rows), the component is not acting reactively and the component is not displaying the new information Expected behavior When new row data is added, the datatable must render the new data Actual behavior when new row data is added nothing happen Resources (screenshots, code snippets etc.)


Mikołaj Smoleński staff commented 4 years ago

Hi there,

Have you tried using a refresh property in this case? Here are some examples of using datatables with external API: https://mdbootstrap.com/docs/vue/tables/datatables/#external-api

Best regards


alana_vue commented 4 years ago

Hello!!! Thanks for your prompt reply. Yes, I am using the refresh property in the datatable. I am following the examples from the documentation as per the link you mentioned. Here the properties I am using. Thank you


Mikołaj Smoleński staff commented 4 years ago

There is also an option to add a reactive property for reloading data systematically in the given intervals (for example "time="2000"). Does that also not fix the problem?

Best regards


alana_vue commented 4 years ago

I am using reactive following the example on the website but nothing happens. here below are all the options I am using. The workaround I found is to use :key to update any dummy variable and trigger the refresh.

/>

alana_vue commented 4 years ago

I am noticing I'm getting the following error: Error in mounted hook: "TypeError: Cannot read property 'field' of undefined" I saw in an old post about this issue being corrected but apparently it's not


Mikołaj Smoleński staff commented 4 years ago

Can you send the options again? I don't see them. Best regards


alana_vue commented 4 years ago

Hello,Here the options: mdb-datatable-2 v-model="table"

    :key="componentKey"
    striped
    bordered
    small
    maxHeight="1000px"
    scrollY
    materialInputs
    class='h-auto d-inline-block'
    responsive
    :display="30"
    reactive
    :time="7000"
    selectable
    @selected="selected =setSelection($event)"
    :order="order"

Marcin Luczak staff answered 4 years ago

Hi @alana_vue,

I've created a local JSON server to check your options and everything seems to be working fine. Every time I update a file in the local server, data is updated and presented into Datatable on fetch interval. Does your API return properly formatted data? It should return a JSON in this format:

{
    "columns" : [
        {
            "your columns data"
        }
    ],
    "rows" : [
        {
            "your rows data"
        }
    ]
}

Regards, Marcin


alana_vue commented 4 years ago

Hi Marcin, Thanks for your reply. The API is returning a JSON with exactly the same format as you described. The only error I am noticing is the following:

vue.runtime.esm.js?2b0e:1888 TypeError: Cannot read property 'find' of undefined at VueComponent.maxHeight (index.js?91c9:20) at Watcher.get (vue.runtime.esm.js?2b0e:4479) at Watcher.evaluate (vue.runtime.esm.js?2b0e:4584) at Proxy.computedGetter (vue.runtime.esm.js?2b0e:4836) at Proxy.a (index.js?91c9:20) at VueComponent.Vue._render (vue.runtime.esm.js?2b0e:3548) at VueComponent.updateComponent (vue.runtime.esm.js?2b0e:4066) at Watcher.get (vue.runtime.esm.js?2b0e:4479) at Watcher.run (vue.runtime.esm.js?2b0e:4554) at flushSchedulerQueue (vue.runtime.esm.js?2b0e:4310)


alana_vue commented 4 years ago

Thank you very much! this solves my issue


Marcin Luczak staff answered 4 years ago

Now I've noticed that you are binding your data with v-model which means that you have to fetch your data into data() section of your Vue app and use it as a variable to Datatable. In this case, none of reactive and :time won't work as those properties are exclusive for binding external data with value. If is that your case you can try to implement my snippet: https://mdbootstrap.com/snippets/vue/marcin-luczak/2964086, showing how to fix the problem with null field in your columns. You have to explicitly add any column to the datatable and in your fetch function remove it from the table.

Otherwise, if you are binding your API directly to the Datatable component, please bind `value="https://your_API".


Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Resolved

Specification of the issue
  • User: Free
  • Premium support: No
  • Technology: MDB Vue
  • MDB Version: 6.7.2
  • Device: PC
  • Browser: Chrome
  • OS: Windowns10
  • Provided sample code: No
  • Provided link: No
Tags