javascript - What's the best way to get data back from a jqGrid cell? -


i'm new jqgrid , needed data cell editing , wanted avoid accessing server.

since custom formatter adding tags data, , using getcell returns tags well,my first implementation strip off 'manually':

createcolumn: function (val, options) {     return '<div style="white-space:pre-wrap">' // etc.         + val         + '</div>'; } // ... // note: rowid known @ point var mydata = $('#mygrid').jqgrid('getcell',rowid,'mydata'); var mydatawrapper = mydata.createcolumn(''); data = data.substr(mydatawrapper.length-6,data.length-mydatawrapper.length); 

of course, break if adds more complex tags. looked @ way getcell finds data. unfortunately, @ present, searches name code is:

var n = -1; val colmodel = $('#mygrid').jqgrid('getgridparam','colmodel'); (var in colmodel) {     if (colmodel[i].name === 'mydata') {     n = parseint(i)+1; } var mydata = $('#'+rowid+' td:nth-child('+n+') div').text(); 

it seemed wrong duplicate code in getcell, there doesn't seem other way find column number name (at least, couldn't find it!). assumes data wrapped in single div, in first version.

looking @ generated html, noticed jqgrid tags cell using aria-describedby (which i've never seen before). if guaranteed there, above can done using:

var mydata = $('#'+rowid+' td:[aria-describedby=mygrid_mydata] div').text(); 

but still uses assumption data wrapped in single div and, of course, future version of jqgrid change or remove attribute.

so tried simple solution adding id wrapping data in span find it:

createcolumn: function (val, options) {     return '<div style="white-space:pre-wrap">' // etc.         + '<span id="' + options.gid + '_' + options.rowid + '_' + options.colmodel.name + '">' + val + '</span>'         + '</div>'; } // ... var mydata = $('#mygrid_'+rowid+'_mydata').text(); 

this seem best way (as have full control on it) there better way data passed formatter function won't break on possible future upgrades jqgrid , doesn't require adding tags?

regarding initial part of question:

since custom formatter adding tags data, , using getcell returns tags well...

the standard way data without tags implement unformat function. jqgrid docs:

as mentioned in predefined formatter chapter predefined types compatible editing modules. means numbers, links, e-mails, etc., converted can edited correctly. methods (like getrowdata , getcell) data, used unformat in order original value. question is: if use custom formatter function , want to have original value if use editing or methods getrowdata , getcell?

the answer is: can use own custom unformatter function that. function can used in colmodel

for example:

jquery("#grid_id").jqgrid({ ...    colmodel: [        ...        {name:'price', index:'price', width:60, align:"center", editable: true,        formatter:imageformat,         unformat:imageunformat},       ...    ] ... });  function imageformat( cellvalue, options, rowobject ){     return '<img src="'+cellvalue+'" />'; }  function imageunformat( cellvalue, options, cell){     return $('img', cell).attr('src'); } 

see unformatting section of jqgrid documentation full details, including parameter descriptions.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -