javascript - Adding custom parameter to column object for custom formatter slickgrid -


i need suggestions regarding adding custom properties column object in slickgrid. here scenario try explain make problem statement clear.

i trying write custom formatter abbreviates , capitalizes names of cities using first 4 characters of original text (i have removed other rules make problem simple explain). eg, charlotte become char in formatter. want similar formatting on different string column need display , capitalize 5 chars.

this tried.

i wrote custom formatter , passed custom property 'numofchar' in column object. property accessible in columndef object of formatter. curious know if approach right , acceptable or there better way accomplish this.

abbreviationformatter = function(row, cell, value, columndef, datacontent) {    var output = '';      if(! columndef.numofchar) {        columndef.numofchar = 4 // sets default     }     if(value) {       output = value.substring(0, columndef.numofchar).touppercase();    }     return output; } 

somewhere in column definition

var columns = [ { id: 'id_1',                    field='city',                    name='city',                    formatter: abbreviationformatter,                    numofchar: 4 },                 { id: 'id_2',                    field='randomstring',                    name='randomstring',                    formatter: abbreviationformatter,                    numofchar: 6 } , ...               ]; 

thanks.

yes easy doable, made , tested it, works expected. first made custom formatter, please note extended slickgrid formatters separate file better, here's code custom formatter:

// extend slick.formatters object (function ($) {     // register namespace     $.extend(true, window, {         "slick": {             "formatters": {                                 "customupper": customupperformatter,                 "titlecase": titlecaseformatter             }         }     });      // --     // capitalize number of chars defined user     function customupperformatter(row, cell, value, columndef, datacontext) {         //console.debug(columndef.formatteroptions.numofchar);         var nbchar = columndef.formatteroptions.numofchar;         var output = value.substring(0, nbchar).touppercase() + value.substring(nbchar);          return value ? output : '';     }      // capitalize first char of each word     function titlecaseformatter(row, cell, value, columndef, datacontext) {         var output = value.replace(/\w\s*/g, function(txt){return txt.charat(0).touppercase() + txt.substr(1).tolowercase();});          return value ? output : '';     }        })(jquery); 

and new columns definition:

// column definition var columns = [ { id: 'id_1',                    field='city',                    name='city',                    formatter: slick.formatters.customupper,                    formatteroptions: { numofchar: 4 } },                 { id: 'id_2',                    field='randomstring',                    name='randomstring',                    formatter: slick.formatters.customupper,                    formatteroptions: { numofchar: 4 } } ,                 { id: 'id_3',                    field='randomstring',                    name='randomstring',                    formatter: slick.formatters.titlecase }, ...               ]; 

so can add whichever property want name want , call inside formatter columndef property, if console.debug(columndef); inside custom formatter, see properties of row. that's :)

note:
please note created object formatteroptions directly numofchar, if go approach started code with, call columndef.numofchar. reason why prefer using object can pass multiple options, example having minimum , maximum number of chars...

note #2
gave me idea of title case formatter (title case definition capitalize first letter of each word), updated code include well, no options required though.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -