javascript - Slickgrid custom cell editor updates all cells -
i've customised code @ http://mleibman.github.io/slickgrid/examples/example3a-compound-editors.html provide me custom editor takes numerator , denominator , displays percentage once values updated (custom formatter code below):-
function numericrangeformatter(row, cell, value, columndef, datacontext) { return isnan(datacontext.from/datacontext.to) ? "" : ((datacontext.from/datacontext.to) * 100).tofixed(2) + "%"; }
this works expected, i've found if have more 1 column identified using custom editor / formatter there appears glitch in that, if edit cell in row, rest of cells utilise editor update same value.
my columns defined below:-
var columns = [ {id: "indicator", name: "indicator", field: "indicator", width:300}, { id: "apr", name: "april", field: "apr", width: 100, formatter: numericrangeformatter, editor: numericrangeeditor }, { id: "may", name: "may", field: "may", width: 100, formatter: numericrangeformatter, editor: numericrangeeditor }, { id: "jun", name: "june", field: "jun", width: 100,formatter: numericrangeformatter, editor: numericrangeeditor } ];
so, basically, if edit cell in 'april' column, cells both 'may' , 'june' update same value. don't want behaviour.
i can't see wrong in code handles values in editor, , i've read guidance @ https://github.com/mleibman/slickgrid/wiki/writing-custom-cell-editors.
is there obvious missing? code editor below:-
function numericrangeeditor(args) { var $from, $to; var scope = this; this.init = function () { $from = $("<input type=text style='width:40px' />") .appendto(args.container) .bind("keydown", scope.handlekeydown); $(args.container).append(" / "); $to = $("<input type=text style='width:40px' />") .appendto(args.container) .bind("keydown", scope.handlekeydown); scope.focus(); }; this.handlekeydown = function (e) { if (e.keycode == $.ui.keycode.left || e.keycode == $.ui.keycode.right || e.keycode == $.ui.keycode.tab) { e.stopimmediatepropagation(); } }; this.destroy = function () { $(args.container).empty(); }; this.focus = function () { $from.focus(); }; this.serializevalue = function () { return { from: parseint($from.val(), 10), to: parseint($to.val(), 10) }; }; this.applyvalue = function (item, state) { item.from = state.from; item.to = state.to; }; this.loadvalue = function (item) { $from.val(item.from); $to.val(item.to); }; this.isvaluechanged = function () { return args.item.from != parseint($from.val(), 10) || args.item.to != parseint($from.val(), 10); }; this.validate = function () { if (isnan(parseint($from.val(), 10)) || isnan(parseint($to.val(), 10))) { return { valid: false, msg: "please type in valid numbers." }; } if (parseint($from.val(), 10) > parseint($to.val(), 10)) { return { valid: false, msg: "'from' cannot greater 'to'" }; } return { valid: true, msg: null }; }; this.init(); }
you have columns namely [apr,may,jun]
use same formatter/editor.
the problem referencing same fields within datacontext to
, from
. if open editor on of columns [apr,may,jun]
, overwriting fields to
, from
. thus, changes reflects other columns formatter use same fields reference
to resolve this, have add new fields on datacontext store values each month e.g. aprilto,aprilfrom, mayto,mayfrom,junto,junfrom. then, change editor to
this.applyvalue = function (item, state) { item[args.column.field + 'from'] = state.from; item[args.column.field + 'to'] = state.to; };`
formatter :
function numericrangeformatter(row, cell, value, columndef, datacontext) { return isnan(datacontext[columndef.field + 'from'] / datacontext[columndef.field + 'to']) ? "" : ((datacontext[columndef.field + 'from'] /datacontext[columndef.field + 'to']) * 100).tofixed(2) + "%"; }
Comments
Post a Comment