Shaun Mccran

My digital playground

19
M
A
Y
2011

JQuery datatable with added FancyBox and auto refresh

Recently I've had a few questions about how to use the datatables plugin to display data, and also integrate a pop up or lightbox function. The pop up would have an edit form in it, and when the user completes the edit the light box goes away and the datatable refreshes.

This is relatively straightforward to do, but there are several key concepts to getting everything working correctly, so I've broken it down into a few chunks.

You can see an example of a datatable with added FancyBox here.

Firstly we cannot use the standard method of calling a datatable. We need to load it into a JavaScript function rather than just using the document.ready() method. This is because we need to check if it exists, and we need to be able to refresh it in code, and we can't do that unless it has a reference.

Below is the code to create the datatable as a JavaScript function. Notice that what it does is check if a reference exists 'dTable' and if it doesn't it runs the datatable code. If it does exist then it runs the dTable.fnDraw() method instead, which causes the datatable to refresh itself. All the other datatable code remains the same.

view plain print about
1<s/cript type="text/javascript" charset="utf-8">
2        function getData()
3            {
4            if (typeof dTable == 'undefined') {
5
6            dTable = $('#displayData').dataTable( {
7            // All the rest of the datatable code goes here
8            });
9            }
10        else
11        {
12            dTable.fnDraw();
13        }
14
15    }
16</script>

Next I want to change the JSON from the remote cfc. Instead of passing the id value back in the JSON response I want to change it into a link for my pop up event. In the loop that creates the JSON I find the id value, and switch it out for a hyperlink, and give it a class of pop-up. I am also adding the id in as the element id, so that I can pick it up and pass it to our pop up window.

Be careful with your quoted values at this point, quotes may interfere with your JSON.

view plain print about
1<cfif variables.index EQ 'id'>
2    <a class=pop-up id=#rResult[variables.i][rResult.currentRow]#>Edit</a>
3<cfelse>
4    #rResult[variables.i][rResult.currentRow]#
5</cfif>

So now the datatable will have a link in it as the first element. The next part of this puzzle is attaching a JQuery selector to the 'pop-up' class on our link. As the links are loaded in dynamically we cannot just give them a regular selector. We need to bind the event using the live() JQuery event method. This is a really powerful JQuery method, I won't go into depth about what it does here, but Ben Nadel has written a great entry about it here: http://www.bennadel.com/blog/1751-jQuery-Live-Method-And-Event-Bubbling.htm , I'd advise reading it and making sure you are clear on what its doing.

view plain print about
1<s/cript type="text/javascript">
2$(document).ready(function(){
3
4    // pop up handler
5    $( ".pop-up" ).live(
6        "click",
7            function( event ){
8
9            // get the id of the item clicked on
10            var id = $(this).attr('id');
11
12            $.fancybox({
13                'overlayOpacity'        : 0.5,
14                'href'    : 'blank.cfm?id=' + id,
15                'onClosed'        : function() {
16                    getData();
17                    alert('data table reloaded');
18                    }
19                });
20
21                // block the href actually working
22                return( false );
23                                }
24            );
25    
26        // load the datatable
27        getData();
28    });
29</script>

You can see above we are looking out for a click on any elements that exist, or are created with a class of 'pop-up'. Then we attach the fancybox plugin to them. I'm also reading the id value and using it in the href url call. In this way we can pass in the id of the record clicked on.

The fancybox plugin has a 'onClosed' option, which allows you to specify code to run when the lightbox closes through any method. in the example above I am running the getData() function, which as the datatable already exists will simply reload it.

The very last line is where you tell the page to load the datatable, as it is no longer within a document.ready() it will not auto load.

You can see an example of a datatable with added FancyBox here.

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
ripwit's Gravatar Nice.

I'm looking to do something similar by having the popup edit box be triggered by a double-click on a table cell and using the ckeditor plugin to update the contents.
# Posted By ripwit | 20/05/2011 03:19
Shaun McCran's Gravatar That would be pretty straight forward, as there is already a double click handler event in JQuery. http://api.jquery.com/dblclick/.

I guess you are trying to replicate a desktop environment?
# Posted By Shaun McCran | 20/05/2011 10:26
Steve 'Cutter' Blades's Gravatar Did you see the custom CF Query extension I wrote for DataTables?

http://blog.cutterscrossing.com/index.cfm/2011/2/7...
# Posted By Steve 'Cutter' Blades | 20/05/2011 13:46
ripwit's Gravatar @Shaun - well, not so much desktop (old school) as RIA (new) <grin>.
I currently have a simple data entry/edit web page where the users type into a ckeditor textarea, specifying each column and row by agonizing row. The results are displayed as a grid so I just thought I'd make the grid editable. Since the ckeditor menus,etc. take up so much real-estate I thought I would do a pop-out window for the edits along with a Cancel and Apply button. There would be a Save for the whole page of applied edits.

@Steve - I had looked at the DataTables plugin for another purpose but hadn't seen your extension for it. I will take a look.
# Posted By ripwit | 20/05/2011 14:34
Shaun McCran's Gravatar @Steve I hadn't seen that, thanks its great, next time I put together a datatable I'll look to integrate it.

@ripwit, ah, I see what you mean now. you can make each of the data items editable easily enough using the API. I'd be tempted to save each action as the user exits the editable boxes, maybe using an ajax request. That way there isn't one big commit at the end of the process.
# Posted By Shaun McCran | 20/05/2011 15:01
Mona's Gravatar Steve,
How can I now add a pencil image for some of the columns to allow the user to edit.
The fancybox works correctly now. I am using the server side.
Thanks
Mona
# Posted By Mona | 02/06/2011 04:56
Mona's Gravatar Thanks Shaun,
I have most things working now thanks to you. I would like to see how to add a date range filter to one of the columns. I am using your server side code. I have the two date called min and max on the page.
# Posted By Mona | 08/07/2011 10:45
Shaun's Gravatar Hi Mona,

Good to see that you have it working. You could use a datepicker plugin to add a min and max date value fields, that way when the date fields are filled in you trigger the json request passing in the values.

You'd have to amend the sql on the back end, giving it another where clause for the date fields.
# Posted By Shaun | 10/07/2011 04:34
Mona's Gravatar Shaun,
I got it working thanks so much!!
# Posted By Mona | 15/07/2011 04:21
Shaun McCran's Gravatar Mona,
That's great! Do you have a version online that we can see?
# Posted By Shaun McCran | 15/07/2011 04:31
Mona's Gravatar Would you like me to add the code to this page or should i attach a doc with source file?
# Posted By Mona | 20/07/2011 06:22
Shaun's Gravatar Hi,
You could send me the code and I'll host it here.
# Posted By Shaun | 21/07/2011 13:58
Fery Wardiyanto's Gravatar Nice tutorial. thanks
# Posted By Fery Wardiyanto | 09/12/2011 08:39
Andrew Paget's Gravatar I found the tutorial very good but I have just one question.

When the record is updated (or the fancybox is closed, as in the demo), I note the table refreshes back to very first page of the recordset. Is it possible to instead to remain on the same page of the recordset so that the user could see that the record they clicked the edit button for was in fact updated (for eample, I could include a 'Last Updated' column on the table too)
# Posted By Andrew Paget | 07/08/2012 02:04
Marc's Gravatar hi,

The files of this demo are downloadable somewhere ?
Thanks a lot,

Marc
# Posted By Marc | 03/12/2014 01:10
Jenny's Gravatar I think it will be very helpful to the users with the addition of FancyBox and auto refresh in the JQuery data table. I saw the new data table and it looks so easy to understand each command in the data table. http://www.originalorkopina.com
# Posted By Jenny | 25/05/2015 20:45
Ninsaa's Gravatar JQuery datatable avec FancyBox ajoutée et rafraîchissement automatique http://www.trustedmontre.com
# Posted By Ninsaa | 23/06/2015 00:11
amyahlom's Gravatar How should we get back to that good experiencing location from a location of disappointment along with animosity?
<a href="http://www.barenakedlife.com/how-to-turn-a-guy-on/...;
# Posted By amyahlom | 20/09/2015 12:40
amyahlom's Gravatar Very good about yourself, about lifestyle as well as the long term with each other. It really is this very good experience that we the two want, it does not take giving as well as obtaining of this that produces a a solid partnership.
http://www.barenakedlife.com/how-to-turn-a-guy-on/...
# Posted By amyahlom | 20/09/2015 12:40
buy traffic's Gravatar the users with the addition of FancyBox and auto refresh in the JQuery data table. I saw the new data table and it looks so easy to understand each command in the data table.
# Posted By buy traffic | 05/10/2015 03:13
buy traffic's Gravatar FancyBox and auto refresh in the JQuery data table. I saw the new data table and it looks so easy to understand each command in the data table.
# Posted By buy traffic | 05/10/2015 03:15
cardsharing's Gravatar I currently have a simple data entry/edit web page where the users type into a ckeditor textarea, specifying each column and row by agonizing row.
# Posted By cardsharing | 08/10/2015 00:03
insta followers's Gravatar Good to see that you have it working. You could use a datepicker plugin to add a min and max date value fields, that way when the date fields are filled in you trigger the json request passing in the values.
how to get more instagram likes
http://socialgrand.com/buy-instagram-likes/
# Posted By insta followers | 08/10/2015 00:15
furniture stores Chicago's Gravatar simple data entry/edit web page where the users type into a ckeditor textarea, specifying each column and row by agonizing row.
# Posted By furniture stores Chicago | 10/10/2015 02:56
www.evisa.gov.tr's Gravatar The files of this demo are downloadable somewhere ?
Thanks a lot,
# Posted By www.evisa.gov.tr | 10/10/2015 05:30
cab service jaipur to delhi's Gravatar When the record is updated (or the fancybox is closed, as in the demo), I note the table refreshes back to very first page of the recordset.
# Posted By cab service jaipur to delhi | 11/10/2015 00:27
cccam server's Gravatar As anyone who has spent time living in New Yorkge will tell you,
# Posted By cccam server | 18/10/2015 04:49
cccam server's Gravatar could he shoot my heart,because my soul is empty now
# Posted By cccam server | 19/10/2015 04:13
Dan Smart's Gravatar This article is from 2011, and during the years jquesry has even-become more popular. Thanks for this tutorial. I used it on my blog.<a href="http://www.theworkbootsdoctor.com/">boots&...; blogger.
# Posted By Dan Smart | 19/10/2015 10:39
amyahlom's Gravatar Jquery is an excellent framework!
http://www.theworkbootsdoctor.com/
# Posted By amyahlom | 19/10/2015 10:40
cccam server's Gravatar That would be pretty straight forward, as there is already a double click handler event in JQuery.
# Posted By cccam server | 27/10/2015 23:19
Erik Mentz's Gravatar I don't understand; I'm working with jquery on my website and you added this
http://www.therowingmachinedoctor.com/
code:
if (typeof dTable == 'undefined') {
5
6 dTable = $('#displayData').dataTable( {
7 // All the rest of the datatable code goes here
8 });
9 }

Where can I find the rest of the database code?
# Posted By Erik Mentz | 30/10/2015 09:31
Never Lose Him's Gravatar The fancybox plugin has a 'onClosed' option, which allows you to specify code...
# Posted By Never Lose Him | 04/11/2015 02:21
cccam server's Gravatar Firstly we cannot use the standard method of calling a datatable. We need to load it into a JavaScript function rather than just using the document.ready...
# Posted By cccam server | 05/11/2015 01:45
cccam server's Gravatar It really is this very good experience that we the two want, it does not take giving as well as obtaining of this that produces a a solid partnership.
# Posted By cccam server | 05/11/2015 02:48
cccam server's Gravatar Below is the code to create the datatable as a JavaScript function...
# Posted By cccam server | 07/11/2015 01:15
continuedinnovation.com's Gravatar This is relatively straightforward to do, but there are several key concepts to getting everything working correctly, so I've broken it down into a few chunks.
# Posted By continuedinnovation.com | 08/11/2015 02:53
packers and movers pune's Gravatar I currently have a simple data entry/edit web page where the users type into a ckeditor textarea, specifying each column and row by agonizing row. The results are displayed as a grid so I just thought I'd make the grid editable.
# Posted By packers and movers pune | 08/11/2015 21:57
cccam server's Gravatar Equipped with the most advanced treatment solutions available today, Dr. Lin spearheads the fertility center’s mission...
# Posted By cccam server | 11/11/2015 02:17
cccam server's Gravatar Im my previous article on how to use the JQuery datatables plug I concentrated mainly on the JQuery script, and how to build the AJAX request to receive a JSON response.
# Posted By cccam server | 11/11/2015 04:19
cccam server's Gravatar Next to actually build our data table object. We simply list any of the parameters that we want to pass to the dataTable method as name value pairs
# Posted By cccam server | 11/11/2015 21:35
jackrobin's Gravatar When the record is updated (or the fancybox is closed, as in the demo), I note the table refreshes back to very first page of the recordset.
<a href="https://uk.fiverr.com/brayden_ben/do-high-trust-fl...; target="_blank">High Trust Flow backlinks</a>
# Posted By jackrobin | 18/11/2015 03:45
male yield's Gravatar You must strive to function as the person who controls the interest rate along with temperature of a partnership and not allow the woman guide. These are many ways that may supply you with a begin in preserving control and also maintaining the girl curious.
# Posted By male yield | 21/11/2015 08:26
My Way's Gravatar Are you sure its

3 var href = $(this).attr('href');
# Posted By My Way | 21/11/2015 08:29
Quality Plumbing's Gravatar You must strive to function as the person who controls the interest rate along with temperature of a partnership and not allow the woman guide. These are many ways that may supply you with a begin in preserving control and also maintaining the girl curious.
# Posted By Quality Plumbing | 22/11/2015 00:46
Mahanaim Place's Gravatar but there are several key concepts to getting everything working correctly, so I've broken it down into a few chunks.
# Posted By Mahanaim Place | 23/11/2015 00:22
www.seamlessaluminiumguttering.com's Gravatar I truly appreciate you taking the time to post this. I really liked reading it and am looking forward to more posts from you!
# Posted By www.seamlessaluminiumguttering.com | 24/11/2015 07:21
examples of hazardous waste waste disposal's Gravatar The results are displayed as a grid so I just thought I'd make the grid editable. \
# Posted By examples of hazardous waste waste disposal | 25/11/2015 03:48
delhi to ajmer taxi's Gravatar See how our designers get creative with coffee table style for the living rooms.
# Posted By delhi to ajmer taxi | 29/11/2015 23:14
Serial data logger's Gravatar subscribe, any new posts to this thread will be sent to your email address.
# Posted By Serial data logger | 01/12/2015 05:43
breathing machine for sleep apnea's Gravatar Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information...
# Posted By breathing machine for sleep apnea | 02/12/2015 01:51
modern blinds's Gravatar This type of message always inspiring and I prefer to read quality content, so happy to find good place to many here in the post, the writing is just great, thanks for the post.
# Posted By modern blinds | 05/12/2015 01:28
dofollow seo backlink services's Gravatar You can see above we are looking out for a click on any elements that exist, or are created with a class of 'pop-up'. Then we attach the fancybox plugin to them
# Posted By dofollow seo backlink services | 14/12/2015 02:36
acupuncture weight loss's Gravatar Everything is very open with a really clear description of the issues. It was really informative. Your website is very helpful. Thanks for sharing...
# Posted By acupuncture weight loss | 14/12/2015 05:29
http://acceptcoverage.com/shop-around.html's Gravatar It was really informative. Your website is very helpful. Thanks for sharing.
# Posted By http://acceptcoverage.com/shop-around.html | 14/12/2015 21:57
law firms's Gravatar I starting late had the good circumstances of taking a gander at your article as to endeavor strategies. It was richly formed and contained sound, rational direction.
# Posted By law firms | 19/12/2015 01:40
orange county personal injury lawyer's Gravatar New web site is looking good. Thanks for the great effort.
# Posted By orange county personal injury lawyer | 19/12/2015 07:29
divorce lawyers in los angeles's Gravatar I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own Blog Engine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.
# Posted By divorce lawyers in los angeles | 22/12/2015 02:57
Dental Assistant's Gravatar I basically need to tell you that I am new to weblog and absolutely delighted in this site page...
# Posted By Dental Assistant | 22/12/2015 21:39
click's Gravatar I really loved reading your blog. It was very well authored and easy to understand. Unlike other blogs I have read which are really not that good.Thanks alot!
# Posted By click | 23/12/2015 00:01
cccam server's Gravatar Thanks for your website. Its really good....
# Posted By cccam server | 23/12/2015 03:53
coconut oil pulling at http://proteccozumel.com/'s Gravatar You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great Work.
Likable's Gravatar HabitLift is a morning wakeup call service where the person calling you asks about your habits. With each habit, they just ask you if you've done it, and they encourage you very much. They also get you to promise to do the habit.
# Posted By Likable | 24/12/2015 22:34
iPhone 6S cases's Gravatar you tell the page to load the datatable, as it is no longer within a document.ready() it will not auto load.
# Posted By iPhone 6S cases | 26/12/2015 02:31
tinnitus causes's Gravatar Everything is very open with a really clear description of the issues. It was really informative. Your website is very helpful. Thanks for sharing...
# Posted By tinnitus causes | 26/12/2015 04:46
read more here's Gravatar You will find a lot of approaches after visiting your post. I was exactly searching for. Thanks for such post and please keep it up. Great Work.
# Posted By read more here | 28/12/2015 06:41
source's Gravatar I really loved reading your blog. It was very well authored and easy to understand. Unlike other blogs I have read which are really not that good.Thanks alot!
# Posted By source | 29/12/2015 02:52
Hungry Shark Evolution Hack's Gravatar I know a little about jQuerry but i know javascript real good
# Posted By Hungry Shark Evolution Hack | 31/12/2015 09:25
click here's Gravatar I was going through your blog its a great writing style. I am truly astonished
# Posted By click here | 31/12/2015 09:26
Unlock Her Legs Review's Gravatar Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post!
# Posted By Unlock Her Legs Review | 04/01/2016 02:22
Leslie Cavins's Gravatar I was looking for such thing as javascript popup box and I am glad that I found it here. http://darkestblackoutusa.com/
# Posted By Leslie Cavins | 04/01/2016 17:30
Show Boards's Gravatar I’m now working with WordPress for a couple of with this blogs and forums nonetheless wanting to switch one of them over to your stand akin to you for a trial offer perform.
# Posted By Show Boards | 04/01/2016 21:58
Shelton Cartwright's Gravatar I tried to apply this javascript in my site at http://mygooglesnipersystem.com/ but it is not working
# Posted By Shelton Cartwright | 05/01/2016 17:02
deep sea fishing in dubai's Gravatar If you subscribe, any new posts to this thread will be sent to your email address.
# Posted By deep sea fishing in dubai | 05/01/2016 23:57
Tree Care's Gravatar Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which may do the trick? Have a great day!
# Posted By Tree Care | 07/01/2016 00:33
cccam server's Gravatar Thank you for giving posts and articles were very amazing. I really liked as a part of the article. With a nice and interesting topics...
# Posted By cccam server | 07/01/2016 00:50
The Bonding Code Review's Gravatar It was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity...
# Posted By The Bonding Code Review | 07/01/2016 01:18
iphone 6s cases's Gravatar The very last line is where you tell the page to load the datatable, as it is no longer within a document.ready() it will not auto load.
# Posted By iphone 6s cases | 09/01/2016 06:38
website traffic's Gravatar Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work.
# Posted By website traffic | 09/01/2016 22:06
jaipur airport to ajmer cab fare's Gravatar The very last line is where you tell the page to load the datatable, as it is no longer within a document.ready() it will not auto load
# Posted By jaipur airport to ajmer cab fare | 10/01/2016 22:48
Franco Noval's Gravatar I’m not that much of a internet reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your site to come back in the future. All the best...
# Posted By Franco Noval | 11/01/2016 22:58
How much chocolate can a dog eat's Gravatar This type of message always inspiring and I prefer to read quality content, so happy to find good place to many here in the post, the writing is just great, thanks for the post.
# Posted By How much chocolate can a dog eat | 12/01/2016 03:55
genetica dental's Gravatar The very last line is where you tell the page to load the datatable, as it is no longer within a document.ready() it will not auto load.
# Posted By genetica dental | 13/01/2016 02:15
hackear clash of clans . download shadow fight 2 a's Gravatar If it does exist then it runs the dTable.fnDraw() method instead, which causes the datatable to refresh itself. All the other datatable code remains the same.
abu dhabi escort's Gravatar They were first introduced in Paris. The roulette game wheel is a fusion thought of some English wheel game and Italian board games. ..
# Posted By abu dhabi escort | 14/01/2016 05:50
petirguntur14's Gravatar tanks for let me comment on this post
# Posted By petirguntur14 | 17/01/2016 01:32
Rayman Adventures Hack's Gravatar Rayman Adventures Hack will allow you to get all In-App purchases for free. To hack Rayman Adventures you need just enter Cheat Codes. Below you will see ...
# Posted By Rayman Adventures Hack | 19/01/2016 09:36
Mobile Strike Hack's Gravatar To hack Rayman Adventures you need just enter Cheat Codes. Below you will see ...
# Posted By Mobile Strike Hack | 19/01/2016 09:37
free cigars's Gravatar This is the details My business is locating everywhere you go. Appreciate it on your blog site, I just now signed up your site.
# Posted By free cigars | 19/01/2016 11:27
how to get instagram followers cheap's Gravatar Admiring the time and effort you put into your blog and detailed information you offer!..
# Posted By how to get instagram followers cheap | 20/01/2016 01:13
Back to top