Blogger Widgets
  • Sharing Photos using SignalR
  • TFS Extenstion - allows copy work items between projects
  • Displaying jquery progressbar with ajax call on a modal dialog
  • Managing windows services of a server via a website
  • Exploring technologies available to date. TechCipher is one place that any professional would like to visit, either to get an overview or to have better understanding.

Search This Blog

Friday, 12 August 2011

JQGrid extension to track row selection between pages

Have been using JQGrid for quite some time now and was pretty impressed with the jqGrid plugin itself.

Ok now, how about extending jqGrid for tracking selection between pages. Here is a jquery extension that does the job.

; (function($) {
    $.jgrid.extend({
        enableTracking : true,
        gridTrackingIds : null,  
        TrackPageSelection: function (Rowids, selected, currentpageno) {
            var currentPage = this.GetCurrentPage(currentpageno);
            currentPage.RowIDs = new Array();
            if(selected)
            {
                currentPage.RowIDs = Rowids;
            }
            this.UpdateSelection(currentPage);
        }, 
        GetCurrentPage: function(currentpageno){
            var selectedPages = $(this).jqGrid.gridTrackingIds;

            var currentGrid = this;
            if (selectedPages == null) {
                selectedPages = new Array();
            }

            //find current page
            var currentPage = null;
            $.each(selectedPages, function (i, item) {
                if(item.Page == currentpageno){
                    currentPage = item;
                }
            });

            if(currentPage == null)
            {
                currentPage = {
                    Page : currentpageno,
                    RowIDs : new Array()
                };
                selectedPages.push(currentPage);
            }

            $(this).jqGrid.gridTrackingIds = selectedPages;
            return currentPage;
        },
        UpdateSelection: function(currentPage){
            var selectedPages = $(this).jqGrid.gridTrackingIds;

            var currentGrid = this;
            if (selectedPages == null) {
                selectedPages = new Array();
            }

            var filteredPages = $.grep(selectedPages, function(elem, index) { 
		            return elem.Page != currentPage.Page;
	            });

            selectedPages = filteredPages;
            selectedPages.push(currentPage);

            $(this).jqGrid.gridTrackingIds = selectedPages;

        },
        TrackSelection: function (id, selected, currentpageno) {
            var selectedItems = $(this).jqGrid.gridTrackingIds;            
            //find current page
           var currentPage = this.GetCurrentPage(currentpageno);

           if(selected){                
                var itemIndex = $.inArray(id, currentPage.RowIDs);
                if(itemIndex == -1){
		            currentPage.RowIDs.push(id);
	            }
            }
            else{
                var filteredItems = $.grep(currentPage.RowIDs, function(elem, index) { 
		            return elem !== id;
	            });
	            currentPage.RowIDs = filteredItems;
	        }
            this.UpdateSelection(currentPage);                            
        },
        UpdatePageSelection: function(currentpageno){            
            var selectedItems = $(this).jqGrid.gridTrackingIds;
            if(selectedPages != null)
            {
                //find current page
                var currentPage = this.GetCurrentPage(currentpageno);                
                if(currentPage != null)
                {
                    var selectedItems = currentPage.RowIDs;
                    var currentGrid = this;
                    if(selectedItems != null) {
                        $.each(currentGrid.jqGrid("getDataIDs"), function (i, id) {
                            if($.inArray(id, selectedItems) > -1) {
                                currentGrid.setSelection(id, false);
                            }
                        });                        
                    }
                }
            }
        }
        ResetTracking: function(){
            $(this).jqGrid.gridTrackingIds = null;            
        }
    });
})(jQuery);



All you need is to just copy this into a .js file and add it to your project. Now on row select just call this

onSelectRow: function(rowid,selected)
{
  var pageno = $(this).getGridParam('page');
  $(this).TrackSelection(rowid,selected,pageno);   
}




At each increase of knowledge, as well as on the contrivance of every new tool, human labour becomes abridged.
Charles Babbage

0 comments:

Post a Comment

Copyright © 2013 Template Doctor . Designed by Malith Madushanka - Cool Blogger Tutorials | Code by CBT | Images by by HQ Wallpapers