Category Archives: jQuery

Javascript jQuery Organisation ProgrammingTips

jQuery Best Practices- Performance

Published by:

Despite being one of the most popular JavaScript libraries today jQuery has its shortcomings too.

If used carelessly, it can lead to performance issues. This post will list some of the best practices in using jQuery that will help us avoid them.

  1. Limit DOM Interaction 
    There is a cost when manipulating the DOM tree using jquery. So try to limit interaction with the DOM. Lets take a look at the below code:

    Well, no points for guessing whats wrong here. This piece of code works just fine, since the fruits array is of 8 items only. But suppose we had a list of 1000 items, it would really mess up the speed of the website because we are repeatedly appending elements to the DOM, inside a loop.
    Instead of doing this, we can build up a html first and then append it to the DOM.
  2. Handling Events on Selectors
    Though jQuery can select elements very quickly, it is not necessary to search for the same elements many times.

  3. Optimize Selectors
    With the first approach, jQuery queries the DOM using document.querySelectorAll(). With the second, jQuery uses document.getElementById(), which is faster, although the speed improvement may be diminished by the subsequent call to .find().

    Specificity: 
    Be specific on the right-hand side of your selector, and less specific on the left.
  4. Detach Elements to Work with Them
    The DOM is slow; you want to avoid manipulating it as much as possible. jQuery introduced detach()  in version 1.4 to help address this issue, allowing you to remove an element from the DOM while you work with it.
  5. Use Stylesheets for Changing CSS
    If you’re changing the CSS of more than 20 elements using .css(), consider adding a style tag to the page instead for a nearly 60% increase in speed.
  6. Avoid $( window ).load()

    The $( document ).ready() is fired when a page’s own DOM is loaded, but not external assets like images and stylesheets while $( window ).load() is fired when everything a page is made up of, including its own content and its sub-resources are loaded.

    So, if you’re writing a script that relies on a page’s sub-resources, like changing the background color of a div that’s styled by an external stylesheet, only then use $( window ).load() else it’s better to stick to $( document ).ready().

 

Javascript jQuery jQuery Plugin

Disable sorting of a column when using Datatables

Published by:

Hello,

For a while now, along with other team members I’ve been working on building a payroll software for one of our clients. This is a multitenant application and each tenant can use this software to process the salaries of their employees in addition to certain other functionalities that use employee information.

One such functionality of the application has got to do with the generation of reports and as an application end user(this could probably be a company HR person for example) one might want to generate a report containing certain data of only a selected list of employees. The next question could be, how could one easily filter or sort a table list full of employees to get only a selected list of employees that match certain critera? Turns out there is a jQuery plug-in called [Datatables](http://www.datatables.net/) which converts an HTML table into one which provides a sorting functionality along with an additional set of functionalities like searching and pagination, all, out of the box.

Since there is a need to select only a particular number of employees(based on what was discussed above) for a report that needs to be generated, we’d be selecting the employees using a checkbox that comes as part of each employee record as shown in the screenshot below. The only problem is that datables by default provides sorting for all columns and we didn’t want this to be there for the checkbox column as this is meaningless and could easily confuse an end user.

Datatables with a sorting option on the first column

Datatables with a sorting option on the first column

It took me a while for me to figure out how to disable the sorting on a specific column when using data tables hence I thought I’d share some code on this could be accomplished. The below configuration settings that are passed to datatables, disable the up and down arrow options as shown in the screenshot below for the first column from when the employee data is first loaded on a page. This code was last tested using Datatables 1.10.8 .

Datatables without a sorting option on the first column

Datatables without a sorting option on the first column

Below is how one could pass these options to a datatable. Here example represents the table id that one would specify as part of the HTML table tag.

Here targets[0] represents the first column of the datatable.

Credits

1. Thanks to this discussion within the Datatables forum for some initial leads into the issue.