Monday, 26 August 2013

tips of jqgrid

  • Define own date formatter
  • $.extend($.fn.fmatter , {
        dateFormatter : function(cellvalue, options, rowObject) {
            if (cellvalue) {
                // parseExact just returns 'null' if the date you are trying to
                // format is not in the exact format specified
                var parsedDate = Date.parseExact(cellvalue, "yyyy-MM-ddTHH:mm:ss");
                if(parsedDate == null )
                    parsedDate = new Date(cellvalue);
    
                // if parsed date is null, just used the passed cell value; otherwise,
                // transform the date to desired format
                var formattedDate = parsedDate ? parsedDate.toString("yyyy-MM-dd HH:mm:ss") : cellvalue;
    
                return formattedDate;
            } else {
                return '';
            }
        }
    });
    
  • Add 'link' column to table
  • $.extend($.fn.fmatter , {
        dynamicLink : function (cellValue, options, rowObject) {
            return "<a href='#' onclick=\"viewReport('" + rowObject.custodyRefNumber + "', '" + rowObject.docketNumber + "');return false;\">view report</a>";
        }
    });
    
  • dynamic setting width/height to match parent component
  • function resizeJqgridWidth() {
        if (grid = $('.ui-jqgrid-btable:visible')) {
            grid.each(function(index) {
                gridId = $(this).attr('id');
                gridParentWidth = $('#gbox_' + gridId).parent().width() - 5;
                $('#' + gridId).setGridWidth(gridParentWidth);
            });
        }
    }
    
  • convert string to javascript json object
  • function convertDotNotationStringToNestJsonObject(data) {
        var obj = {}, t, parts, part;
        for (var k in data) {
            t = obj;
            parts = k.split('.');
            var key = parts.pop();
            while (parts.length) {
                part = parts.shift();
                t = t[part] = t[part] || {};
            }
            t[key] = data[k]
        }
        return obj;
    }