JS Library to display table in realtime - php

Now I'm familiar with JSON and jQuery I'm looking for a library which do this:
Update in real time my table (with the JSON (call every x seconds)) and only delete or hide the rows wich are deleted or insert the new rows, but i need to display the new rows in a special way:
Let me explain:
Json request 1 send:
1;Tomato 2;Apple 3;Salad 4;Carot
Json request 2 send:
1;Tomato 3;Salad 4;Carot 5;Potatoes
I would like the second row disapear with a effect (fadeOut) and the rows below move Up. For the row 5, i just want a new row appears with a fade in.
Is there any library existing doing this?
I'm doing it in PHP, but i hope to write all this in JS.
The user could just look the table and see the new rows appearing and the old rows deleting.
Any ideas or am I supposed to write it from scratch?

You could use the awesome jqGrid plugin.
To do the autorefresh, you should do this:
setInterval(function(){
$("#grid1").trigger("reloadGrid");
}, 10000);
To change which params to send, use the plugin method .setPostData()
Hope this helps. Cheers.

Your can write this in pure jquery. Just load table every time, with .post() as example. Fade in/out - is also not a big problem, just append html (div/row) and them show() with effect of vertical slide. Maybe with opacity change.
PS. Specially suggest your look inside ExtJS grids ... this is another way make table appear nicely. One thing your need to extjs - timer, to update it, if mysql is changing in background also.

Related

Table Fixed Header Plugin that retains OnClick

I have a table built dynamically with PHP. The PHP gives each row an on click event that highlights the selected row and display details associated with it in another section of the page.
I wanted this table to be sortable so I started using this:
http://tablesorter.com/docs/
That worked great, it was sortable, selectable, and moderately fast.
I started looking into fixed headers as well and found this:
http://www.tablefixedheader.com/
Sorting and fixed headers. Awesome.
This works in all the ways I want to but it's unfortunately slow and gets rid of the onClick event I create for each table row.
That's not acceptable, in fact, it fundamentally breaks my application.
Is there a better plugin, a way to prevent this or an easier solution?
I need a sortable table with a fixed header that allows me to add unique onClick events and IDs to each table row. And I really don't want to write my own...
DataTables changed my life.
http://datatables.net/index
The fixed header functionality is an "extra": http://datatables.net/extras/fixedheader/

Populate a JSON into a table in real time with JQUERY

I'm looking for a library which do this :
Retrieve a JSON through an AJAX call
Populate table with the JSON
Update in real time the table with the JSON (call every x seconds) and only delete or hide the rows wich are deleted or insert the new rows.
/Editing after first answer
Ok I guess my first explanation was not good.
Retrieving through jQuery a JSON and build a table is good, I could do that, but my request was more on the other part.
I'm looking for a library to display the result in a special way.
Let me explain.
Json request 1 send :
1;Tomato 2;Apple 3;Salad 4;Carot
Json request 2 send :
1;Tomato 3;Salad 4;Carot 5;Potatoes
I would like the second row disapear with a effect (fadeOut) and the rows below move Up. For the row 5, i just want a new row appears with a fade in.
Is that more clear?
Is there any library existing doing this?
I'm doing it in PHP, but i hope to write all this in JS.
The user could just look the table and see the new rows appearing and the old rows deleting.
Any ideas or am I supposed to write it from scratch?
You can get the json like this (use get or post, ill show post here):
function do_json_live(){
$.post('url.php','somedata', function(returnJSON){
alert(returnJSON);
//do something with the `returnJSON`
setTimeout(do_json_live, 2000); //2000 ms = 2 seconds
},'json');
}
If you want something friendly and full of various useful features, you can use jQuery plugin called DataTables.
It provides API allowing you to provide new data from the server on request: http://www.datatables.net/api
It works for simple implementations also, is pretty customizable, allows to change its outlook etc.
Hope this is useful.
Here is a really good article on different polling/comet techniques that you will want to look into. It breifly describes each and points out some pitfalls you might not think of.: http://query7.com/avoiding-long-polling. Also here is a jquery plugin for long polling: http://enfranchisedmind.com/blog/posts/jquery-periodicalupdater-ajax-polling/
Try Jquery Grid Plugin. You can retrieve JSON from server and build a grid on the client side. Take a look at the web site, there are some examples including php.
First I would read this, but the code is actually really simple.
On your front end, you'd have your table
<table id="myTable"></table>
Then you'd make your AJAX post within JQuery
$.ajax({
url: "yourphpfile",
data: <data you want your php file to read>
success: function(data){
$('#myTable').html(data);
}
});
Your method in php would take in your posted data, it would create an HTML string of a table element, and then you'd set your table's innerHTML on the front end with .html() built into JQuery -- that way you never have to worry about showing/hiding, everytime you post, your given the table itself, so you just display exactly that, you can handle all the fancy stuff server side.
You could use the awesome jqGrid plugin.
To do the autorefresh, you should do this:
setInterval(function(){
$("#grid1").trigger("reloadGrid");
}, 10000);
Hope this helps. Cheers.
If real-time updating is truly required, as Neal suggested, Comet or Stream-Hub would be one avenue worth checking into.
As for the interface, I recently have been using JQuery Templates, and when reconciling added / removed / updated records, I use JQuery selectors to clear & update, and use Templates to add in new records. And because I'm using JQuery in all 3 events, I could easily integrate their motion / visual effects.
JQuery Templates
JQuery Selectors
JQuery Effects
Stream-Hub
I myself only needed polling (every 15 seconds) so I'm using Robert Fischer's improved JQuery PeriodicalUpdater
JQuery Periodical Updater

Save information to database after jquery callback

I'm new here and I'm very new at programming but I need some serious hand-by-hand help here.
I was searching jquery and found a script to drag and drop stuff on the screen, basicly i just want to move some divs around, thats the easy part, the script I found has a callback function that writes onto the div that you just moved "dropped", this is exactly what I need but instead of writting dropped I want to save the 2 postion variables into a database (mysql), this is so that if I close the browser and open it again the div's will be on the last place I dropped them.
Can you help? Is there a jquery user interface with this already built in ?
I think this is easy to do with jquery ajax functions right? basicly I should send the serialized data (json right?) into a page that processes that data once its feed into it, then jason returns the handler with success or even with some output right?
It would be cool for the dragabble div to have a handler with last know position retrieve by jason from an external page that acts like a buffer to the database.
Is this the correct pipeline?
Best Regards
Joricam
you have kind of a vague question here, but I can try to help you get closer to the answer.
Imagine two sides of the puzzle:
When the page loads, the two (or more) DIVs are drawn on the screen. If you want them to draw in a specific order, you need to keep track of that in the database. So be sure your db has a field called something like display_order, and then display the DIVs in that order. (You can usually just add ORDER BY display_order to the SQL, so they are retrieved in the order you want, and then draw the DIVs right out in a loop.)
When someone drags and drops a DIV, you use AJAX/JSON/etc to tell your PHP script the new order. In this case, when that happens, rather than draw the word 'dropped' in the DIV, you should instead immediately update the display_order fields in the database. This way you are remembering each DIV's position.
Does that help/make sense?
UPDATED: thinking more about your question, here is the pseudo code:
in "display.php":
Fetch the contents for each DIV from the database, with ORDER BY display_order on the rows.
Draw them on the screen, looping through each database row.
Also in this HTML, use the jQuery script you already have to call another PHP script (dragged.php) when a row is dragged.
in "dragged.php":
This script is called when a row is dragged on the screen.
Currently it puts the word "dropped" in the DIV that is dragged. That's not helpful, so remove that.
Instead, you now know (from the variables passed to you) that a specific DIV needs to be in a specific place.
So grab a list of the DIVs from the database, then change the order of them (by altering the display_order column) based on the new position(s) you know.
Save that back to the database, so when display.php is called again next time, it draws the DIVs in the order you want.
Hope this helps explain further. If you are still struggling, I respectfully suggest you try to write the code, and post a more specific question about the part that you're stuck on. This will help you get a good answer quickly. (You may also want to Google this one a lot; I'm sure there are code samples out there showing how to do all this.)

Pagination with Jquery/Javascript/Ajax and PHP/Mysql

Does anyone know of a link to a site that has a tutorial/code on good way to paginate information coming from a database? (without page refresh) I have spent the better part of the day looking for a site that has what I need. Most are dealing with static/fixed data in the forms of lists etc.
I need one that has something like this for my tables:
Data Data Data Data
Data Data Data Data
Records 1 to 8 of 27 First Previous Next Last
We just implemented a few things using this plugin for JQuery:
Datatables.net
It works on basic html tables and has support for AJAX. Basically you would just write a PHP page that returns a partial result from your database in JSON, etc. The plugin will handle making the AJAX call and displaying the data.
This is only a very partial answer (sorry, not much time atm) but look for the 'LIMIT' command in mysql. It lets you pull back only a certain set of records, say records 11-20. I use that when I do pagination.
Again, this isn't complete, but hopefully it'll help!
I've worked with this one quite a bit. jQgrid... seems to be pretty well maintained.
http://www.trirand.com/blog/

sequentially show mysql data using php

I have a php code that fetches data from mysql. The data has (say) 15 rows. I want to display only 5 rows at a time to the user, with links to each of the set (3 in this case) such that when a user clicks on either of the links, the same page will show the corresponding results. Since, php code has the final result set, I don't want a solution that involves me to navigate to other pages and possibly re-calculate the next set of solutions (5~10 or 10~15). How can I do this? Thanks in advance.
If I am using javascript or ajax, how can I achieve this? I don't know javascript much.
You can try loading everything in your page and simulating the pagination thanks to javascript.
An example in jQuery here
It sounds like you want to send the Data as a complete set to the client but not let him display everything to the user. So use Javascript to just show you the pages 1*page_number to 5*page_number (with a for-loop).
By far, the easiest solution in this situation is a Dom-based Grid system. I recommend checking out Datatables. In essence, the Datatables code will take a fully-formatted html table and reformat it to include only the amount of rows you tell it to, with paging on the bottom as you've requested. In addition, you can turn on such features as filtering, toggle-able length, sorting, and selection. Once you get the hang of it, the additional code takes no more than a minute per table and the features are outstanding.
This is really straightforward. Use PHP to output ALL of the table, with all the rows. The only gotcha here is to include the full html, including <thead> and <tbody> Give the table an id such as "example"
Now, include the files that you download from the datatables site--datatables.js and Jquery.js. Instantiate the jquery like this:
$(document).ready(function() {
$('#example').dataTable();
} );
That's it. As you can see from the examples, it's a really cool tool. Good luck.

Categories