yui - How can I use a custom sortFunction with a Grails gui:datatable tag? -
yui - How can I use a custom sortFunction with a Grails gui:datatable tag? -
i have datatable in grails several sortable columns. unfortunately, sort case-sensitive default, such "zed" shown before "alice". want add together custom case-insensitive sort function. i'll start sorting username column in manner.
i have read sortoptions , sortfunction @ http://developer.yahoo.com/yui/datatable/ can't seem work.
i added next javascript list.gsp:
var caseinsensitivesortfunction = function(a, b, desc, field) { // see http://developer.yahoo.com/yui/datatable/ , 'sortfunction' // set function name sortfunction alternative // deal empty values if (!yahoo.lang.isvalue(a)) { homecoming (!yahoo.lang.isvalue(b)) ? 0 : 1; } else if (!yahoo.lang.isvalue(b)) { homecoming -1; } homecoming yahoo.util.sort.compare(string(a).tolowercase(), string(b).tolowercase(), desc); };
and here datatable definition on same page:
<gui:datatable id="usertable" controller="user" action="listjson" rowsperpage="20" sortedby="username" columndefs="[ [key:'username', label:'username', resizeable: false, sortable: true, sortoptions: { sortfunction: 'caseinsensitivesortfunction' }], [key:'email', label:'email', resizeable: false, sortable: true], [key:'firstname', label:'first name', resizeable: false, sortable: true], [key:'lastname', label:'last name', resizeable: false, sortable: true], [key:'enabled', label:'enabled', resizeable: false, sortable: true], [key:'accountlocked', label:'locked', resizeable: false, sortable: true], [key:'roledescription', label:'role', resizeable: false, sortable: true] ]" draggablecolumns="true" rowclicknavigation="true" paginatorconfig="[ template:'{firstpagelink} {previouspagelink} {pagelinks} {nextpagelink} {lastpagelink} {currentpagereport}', pagereporttemplate:'{totalrecords} total record(s)', alwaysvisible:true, containers:'dt-paginator' ]"/> <div id="dt-paginator" class="yui-skin-sam yui-pg-container"></div>
changing 'username' key next (sans quotes) not help.
[key:'username', label:'username', resizeable: false, sortable: true, sortoptions: { sortfunction: caseinsensitivesortfunction }],
both approaches create next source. note sortoptions username blank.
<div id="dt_div_usertable"></div> <script> yahoo.util.event.ondomready(function () { var datasource = yahoo.util.datasource, datatable = yahoo.widget.datatable, paginator = yahoo.widget.paginator; var usertable_ds = new datasource('/admin/gpupuser/listjson?'); usertable_ds.responsetype = datasource.type_json; usertable_ds.connmethodpost=true; usertable_ds.responseschema = { resultslist : 'results', fields : ["username","email","firstname","lastname","enabled","accountlocked","roledescription","dataurl"], metafields : { totalrecords: 'totalrecords' } }; usertable_ds.dobeforecallback = function(orequest, ofullresponse, oparsedresponse, ocallback) { homecoming grailsui.util.replacedatestringswithrealdates(oparsedresponse); }; var usertable_paginator = new paginator( {'template': '{firstpagelink} {previouspagelink} {pagelinks} {nextpagelink} {lastpagelink} {currentpagereport}', 'pagereporttemplate': '{totalrecords} total record(s)', 'alwaysvisible': true, 'containers': 'dt-paginator', 'rowsperpage': 20} ); var registereditorlistener = function(editor, field, url,successcallback,failurecallback) { editor.subscribe("saveevent", function(oargs) { grailsui.usertable.loadingdialog.show(); var editorcallback = { success: successcallback, failure: function(o) { // revert cell value grailsui.usertable.updatecell(oargs.editor.getrecord(), field, oargs.olddata); // alert user if (failurecallback) failurecallback(o); else alert('received error during edit: ' + o.responsetext); } }; yahoo.util.connect.asyncrequest('post', url, editorcallback, 'id=' + oargs.editor.getrecord().getdata('id') + '&field=' + field + '&newvalue=' + oargs.newdata); }); }; var mycolumndefs = [{'key': 'username', 'label': 'username', 'resizeable': false, 'sortable': true, 'sortoptions': }, {'key': 'email', 'label': 'email', 'resizeable': false, 'sortable': true}, {'key': 'firstname', 'label': 'first name', 'resizeable': false, 'sortable': true}, {'key': 'lastname', 'label': 'last name', 'resizeable': false, 'sortable': true}, {'key': 'enabled', 'label': 'enabled', 'resizeable': false, 'sortable': true}, {'key': 'accountlocked', 'label': 'locked', 'resizeable': false, 'sortable': true}, {'key': 'roledescription', 'label': 'role', 'resizeable': false, 'sortable': true}, {'key': 'dataurl', 'type': 'datadrilldown', 'hidden': true}]; grailsui.usertable = new grailsui.datatable('dt_div_usertable', mycolumndefs, usertable_ds, '', { initialrequest : 'max=20&offset=0&sort=username&order=asc&', paginator : usertable_paginator, dynamicdata : true, sortedby : {key: "username", dir: yahoo.widget.datatable.class_asc}, 'draggablecolumns': true, 'selectionmode': 'single', 'rowclicknavigate': false, 'rowclickmode': 'navigate', 'formatter': 'text' }); // update totalrecords on fly value server grailsui.usertable.handledatareturnpayload = function(orequest, oresponse, opayload) { opayload.totalrecords = oresponse.meta.totalrecords; homecoming opayload; }; // set editing flow var highlighteditablecell = function(oargs) { var elcell = oargs.target; if(yahoo.util.dom.hasclass(elcell, "yui-dt-editable")) { this.highlightcell(elcell); } }; grailsui.usertable.subscribe("cellmouseoverevent", highlighteditablecell); grailsui.usertable.subscribe("cellmouseoutevent", grailsui.usertable.oneventunhighlightcell); grailsui.usertable.subscribe("cellclickevent", grailsui.usertable.oneventshowcelleditor); }); </script> <div id="dt-paginator" class="yui-skin-sam yui-pg-container"></div>
is there way datatable utilize custom sortfunction when configured tag?
in listjson
action, add together ignorecase: true
parameter list()
. see doc. it's easy way.
you cannot force totally custom function javascript level database level, if you're going back upwards paging - custom function sort current page only, paging based on default (by id
) ordering. in other words, custom function works correctly no more 1-page tables.
you can customize sorting extent if utilize criteria
or sql in listjson
action - still, you're limited hibernate or sql level.
grails yui yui-datatable
Comments
Post a Comment