Auto-Refreshing Tables

It would be awesome to have the ability to auto-refresh a table on a page. We have a table that a lot of our employees use to update customer records, and everybody has to constantly F5 to make sure they have the latest and greatest before updating old information.


1 Like

Hey Andrew, what is the code for #view_1111?

Tks!

Voted!

Doing JSĀ location.hash = location.hash + "#" is much faster than F5

An auto-refreshing page would be great! Ā ... particularly if there was a setting to control exactly how often it should refresh.

1 Like

AGREEEDDDD!!!!!

Hi to all,

Good news!! Iā€™ve got exactly what you want. Iā€™ve coded a library called KTL that does this without a single line of code. All you have to do is add AUTOREFRESH=60 to the tableā€™s title to get an update every minute.

If youā€™re interested, itā€™s open-source (free). All you have to do is copy/paste the .js and .css files to your app in the Builder.

Get KTL here: https://github.com/cortexrd/Knack-Toolkit-Library

Enjoy!
Normand D.
Cortex R&D Inc.

Normand, any way of you posting the code here and is there away to implement your code without having to add ā€œAUTOREFRESH=60ā€ in the tableā€™s title?

Thank you in advanceā€¦ JON

Hi,

Try below code.

$(document).on(ā€˜knack-view-render.view_157ā€™, function (event, view, records) {
AutoRefresh(ā€˜view_157ā€™,5000);
});

function AutoRefresh(viewId,ms)
{

setTimeout(function(){
AutoRefresh(viewId,ms);
Knack.views[viewId].model.data.fetch()
},ms);

}

Change view id and ms according to your requirements.
https://roberts.knack.com/farmers#test-field/

Regards,
Sunny Singla
ssingla1985@gmail.com
+919855089359

2 Likes

Hey Sunny, couldnā€™t get your code to work for me. Iā€™m sure it something I did or didnā€™t do.

I changed view_157 on first 2 lines to my view (view_187) other than that, made no other changes.

Thanks as alwaysā€¦ JON

Hello John,

Yes, you just need to make only those changes. Share your page url where you implemented it.

It works for me can you check it on my given url

Regards,
Sunny Singla

Iā€™ll put the code in an example app and email you. I did open your url link above but wasnā€™t sure if your sample program was doing what was expecting. I was hoping for a solution to refresh a given tableā€™s data every few seconds or minutes. It was hard to tell what your app was doing. I inputted some same data but thatā€™s about it.

@Sunny_Singla your code works for me.
A tweak so itā€™s easier to paste into JS inside a view render:

$(document).on(ā€˜knack-view-render.view_157ā€™, function (event, view, records) {
     AutoRefresh(view.key,5000);
});

function AutoRefresh(viewId, ms) {
     setTimeout(function() {
          AutoRefresh(viewId, ms);
          Knack.views[viewId].model.data.fetch()
     }, ms);
}

Thanks @Sunny_Singla

1 Like

Sunny, is there a solution that would stop the page from refreshing using your code when a user filters by search criteria.

Using your example app, when a user searches for ā€œBMWā€ the table shows data containing ā€œBMWā€ but then quickly refreshes and shows all table data. Any and all searches made are shown, but quickly refreshed within 5 seconds.

Also, is there a way for you to turn on ā€œin line editingā€ for ā€œtestmessagenameā€ field for your example app? Iā€™d like to see if a user makes and submits changes this way if it will update on a 2nd open window within given time (ie 5 seconds). I couldnā€™t get changes made this way to reflect on 2nd open window in our app unless I pressed refresh on browser or F5.

Also, I created an ā€œeditā€ link in our app that opened another page to edit given data. I couldnā€™t get updated/submitted data on 2nd open window unless we manually updated browser or pressed F5.

As always thank youā€¦ JON

Hi gentlemen,

I invite you to go see my new Youtube channel.

Cortex R&D Inc - Youtube Knack tutorials and more

Iā€™ve just completed my first demo of the KTL. I think my auto refresh code adresses part of your problem since it pauses refreshing as soon and as long as you set the focus on a Search input field, and maintains the results afterwards.

Give it a try and let me know if it helps.
Norm

1 Like

Hi Sunny and Brad - I have tried both of your proposed solutions without success. Normand, your code looks interesting and addresses the issue indicated about search results refreshing, but Iā€™m not prepared to implement everything at once nor do I want to spend time turning off functions of the code, perhaps one day!

Sunny and Brad, is there something Im missing in my code? I changed the view to my table view (857) but no luck. Any help is greatly appreciate!

$(document).on(ā€˜knack-view-render.view_857ā€™, function (event, view, records) {
AutoRefresh(ā€˜view_857ā€™,5000);
});

function AutoRefresh(viewId,ms)

{

setTimeout(function(){
AutoRefresh(viewId,ms);
Knack.views[viewId].model.data.fetch()
},ms);

}

Hello Ryan,

Just see your message. And I donā€™t think there is any issue in this code.

May be some other issue. If possible share your page url so we can check.

Regards,
Sunny Singla
ssingla1985@gmail.com
+919855089359

Hi RyanL,

Iā€™ve added a new template file for those like you whoā€™d like to test the KTL with everything turned off, and enabling only the desired features one by one.

See the updated documentation here.

Let me know if you have questions or concerns.

Norm

Hey Normand,

Your KTL is pretty amazing. I havenā€™t used it personally but I am digging through your code and borrowing some features. I do have a couple of questions. I noticed in this thread that folks suggest using a five second interval along with Knack.views[view.key].model.data.fetch(). Meanwhile your code is 60 seconds and you use Knack.views[view.key].model.fetch().

I am curious if you noticed a difference between data and model refresh?

Also curious why you defaulted to 60 seconds? I tried the 5 second approach but I am worried if the amount of calls to the server could overload it. The server for one of my apps went down last night after introducing the 5 second interval and that was the only change I made.

1 Like

Hi Robert,

Thanks for the nice comments :slight_smile: Glad you like it.

About your questions:

  1. fetch()

When I use Knack.views[ā€˜view_1ā€™].model.data.fetch(); I see no updates in the view. As if it does nothing.

But when I use Knack.views[ā€˜view_1ā€™].model.fetch(); I so see the updates.

Note that if you use the complete function ktl.views.refreshView(ā€˜view_1ā€™), it will work with ANY view types, not just grids. Even forms will refresh!

  1. My default of 60 seconds was just an arbitrary choice, that I thought would make sense - not too slow nor too fast. I decided to enforce a lower limit of 5 seconds to avoid overloading the ā€œalready-busy-enoughā€ servers :slight_smile:

Of course, 5 seconds is quite fast for large tables with lots of rows and fields. But I can tell you that I use several grids with 10 seconds refresh where they have a single row and 5 fields - without problems.

It should not cause a Knack servers crash per say, but I noticed that the refresh function sometimes encounters errors 500, 502, etc. In such cases, the app needs to be reloaded manually (F5 or Ctrl+R) most of the time, unless it was caught by your code or the KTL and handled appropriately.

These unrecoverable errors are currently causing big problems to my client, where they have dozens of unattended dashboards across their industrial plant to monitor the production status.

I have developed a custom solution involving a NodeJS watchdog (they use Linux devices with Chromium), talking to the Knack App and force a refresh in case of a crash. Works well.

Norm