Is there a way to catch livescroll event in a Primefaces Datatable? - ajax

I have a datatable with livescroll in my view, and an ajaxstatus component. With most of the components I avoid firing the ajaxstatus dialog by setting:
<p:ajax event="someEvent" global=false\>
in the ajax event, but I don't find a way to do the same with a datatable since there are 'page' and 'sort' events but not a 'scroll' event. Any ideas how to avoid this situation?

If you 'just' want achieve a global="false" like behaviour, the easiest thing to do is to override the relevant function in the PrimeFaces Datatable.js function and add a global: false in the options of the ajax call that is made under the hood.
This can be done by including the following javascript
PrimeFaces.widget.DataTable.prototype.loadLiveRows: function() {
if(this.liveScrollActive||(this.scrollOffset + this.cfg.scrollStep > this.cfg.scrollLimit)) {
return;
}
this.liveScrollActive = true;
this.scrollOffset += this.cfg.scrollStep;
//Disable scroll if there is no more data left
if(this.scrollOffset === this.cfg.scrollLimit) {
this.shouldLiveScroll = false;
}
var $this = this,
options = {
source: this.id,
process: this.id,
update: this.id,
global: false, /* Added this so the global is not triggered */
formId: this.cfg.formId,
params: [{name: this.id + '_scrolling', value: true},
{name: this.id + '_skipChildren', value: true},
{name: this.id + '_scrollOffset', value: this.scrollOffset},
{name: this.id + '_encodeFeature', value: true}],
onsuccess: function(responseXML, status, xhr) {
PrimeFaces.ajax.Response.handle(responseXML, status, xhr, {
widget: $this,
handle: function(content) {
//insert new rows
this.updateData(content, false);
this.liveScrollActive = false;
}
});
return true;
},
oncomplete: function(xhr, status, args, data) {
if(typeof args.totalRecords !== 'undefined') {
$this.cfg.scrollLimit = args.totalRecords;
}
$this.loadingLiveScroll = false;
$this.allLoadedLiveScroll = ($this.scrollOffset + $this.cfg.scrollStep) >= $this.cfg.scrollLimit;
// reset index of shift selection on multiple mode
$this.originRowIndex = null;
}
};
PrimeFaces.ajax.Request.handle(options);
},
There unfortunately is no way to partially overide and reuse most (all) of the existing function. I thought you could override the global in a global way so no ajax call triggers the global status. I can unfortunately not find a reference on how to do that anymore.
You can however override the default behaviour in absence of a global setting by overriding Primefaces.ajax.Request.handle
handle: function(cfg, ext) {
cfg.ext = ext;
if (PrimeFaces.settings.earlyPostParamEvaluation) {
cfg.earlyPostParams = PrimeFaces.ajax.Request.collectEarlyPostParams(cfg);
}
if(cfg.async) {
PrimeFaces.ajax.Request.send(cfg);
}
else {
PrimeFaces.ajax.Queue.offer(cfg);
}
},
By adding an explicit setting of cfg.global to false when absent
var orgHandle = PrimeFaces.ajax.Request.handle;
Primefaces.ajax.Request.prototype.handle = function(cfg, ext) {
var global = (cfg.global === false || cfg.global === undefined) ? false : true;
cfg.global = global;
orgHandle(cfg, ext);
}
Take your pick.

Related

What causes jqgrid events to fire multiple times?

Our jqgrid is configured in an initgrid function that is called as the last statement of a ready handler. For some reason, the gridcomplete function is getting called multiple times. With the code below, it gets called twice, but it had been getting called 3 times. Twice is bad enough. After stepping through it multiple times, I don't see what is triggering the second execution of the gridComplete function.
When I hit the debugger at the start of gridComplete, the call stack is virtually identical each time, the only difference being a call to 'L' in the jqgrid:
Anyone have an idea why this is occurring? We are using the free version 4.13, in an ASP.net MVC application.
$(function(){
....
initGrid();
}
function initGrid(){
$gridEl.jqGrid({
xhrFields: {
cors: false
},
url: "/IAConsult/GetWorkFlowIARequests",
postData: {
showAll: showAllVal,
role: role,
IsIAArchitect: userIsIA
},
datatype: "json",
crossDomain: true,
loadonce: true,
mtype: 'GET',
sortable: true,
viewrecords: true,
pager: '#workFlowIAGridPager',
multiselect: true,
rowNum: 50,
autowidth: true,
colModel: [...],
beforeSelectRow: function (rowid, e) {
var $myGrid = $(this),
i = $.jgrid.getCellIndex($(e.target).closest('td')[0]),
cm = $myGrid.jqGrid('getGridParam', 'colModel');
return (cm[i].name === 'cb');
},
jsonReader: {
repeatitems: true,
root: "IAConsultWorkflowRequestsList"
},
beforeSubmitCell: function (rowid, name, value, iRow, iCol) {
return {
gridData: gridData
};
},
serializeCellData: function (postdata) {
return JSON.stringify(postdata);
},
gridComplete: function () {
console.log('grid complete');
let rowIDs = $gridEl.getDataIDs();
let inCompleteFlag = false;
let dataToFilter = $gridEl.jqGrid('getGridParam', 'lastSelectedData').length == 0
? $gridEl.jqGrid("getGridParam", "data")
: $gridEl.jqGrid('getGridParam', 'lastSelectedData');
let $grid = $gridEl, postfilt = "";
let localFilter = $gridEl.jqGrid('getGridParam', 'postData').filters;
let columnNames = columns.split(',');
$('.moreItems').on('click', function () {
$.modalAlert({
body: $(this).data('allitems'),
buttons: {
dismiss: {
caption: 'Close'
}
},
title: 'Design Participants'
});
});
rowCount = $gridEl.getGridParam('records');
gridViewRowCount = rowCount;
let getUniqueNames = function (columnName) {
... };
let buildSearchSelect = function (uniqueNames) {
var values = {};
values[''] = 'All';
$.each(uniqueNames,
function () {
values[this] = this;
});
return values;
};
let setSearchSelect = function (columnName) {
...
};
function getSortOptionsByColName(colName) {
...
}
$grid.jqGrid("filterToolbar",
{ stringResult: true, searchOnEnter: true });
if (localFilter !== "" && localFilter != undefined) {
globalFilter = localFilter;
}
let grid = $gridEl.jqGrid("setGridParam",
{
postData: {
"filters": globalFilter,
showAll: showAllVal,
role: role,
IsIAArchitect: userIsIA
},
search: true,
forceClientSorting: true
});
//grid.trigger("reloadGrid");
//Ending Filter code
for (i = 0; i < columnNames.length; i++) {
var htmlForSelect = '<option value="">All</option>';
var un = getUniqueNames(columnNames[i]);
var $select = $("select[id='gs_workFlowIAGrid_" + columnNames[i] + "']");
for (j = 0; j < un.length; j++) {
val = un[j];
htmlForSelect += '<option value="' + val + '">' + val + '</option>';
}
$select.find('option').remove().end().append(htmlForSelect);
}
debugger;
},
// all grid parameters and additionally the following
loadComplete: function () {
$gridEl.jqGrid('setGridWidth', $(window).width(), true);
$gridEl.setGridWidth(window.innerWidth - 20);
},
height: '100%'
});
I personally almost never use gridComplete callback. It exists in free jqGrid mostly for backwards compatibility. I'd recommend you to read the old answer, which describes differences between gridComplete and loadComplete.
Some additional advices: it's dangerous to register events inside of callbacks (see $('.moreItems').on('click', ...). If you need to make some actions on click inside of grid then I'd recommend you to use beforeSelectRow. Many events, inclusive click event supports event bubbling and non-handled click inside of grid will be bubbled to the parent <table> element. You use already beforeSelectRow callback and e.target gives you full information about clicked element.
I recommend you additionally don't use setGridParam method, which can decrease performance. setGridParam method make by default deep copy of all internals parameters, inclusive arrays like data, which can be large. In the way, changing one small parameter with respect of setGridParam can be expensive. If you need to modify a parameter of jqGrid then you can use getGridParam without additional parameters to get reference to internal object, which contains all jqGrid parameters. After that you can access to read or modify parameters of jqGrid using the parameter object. See the answer for example for small code example.
Also adding the property
loadonce: true
might help.

Execute validation with button

I'm looking to execute the Handsontable validation on the click of a button instead of on cell change. Something like this: validateCells() (return bool isValid). This function doesn't seem to be working for me.
var
data = [],
container = document.getElementById('hot-Handsontable'),
save = document.getElementById('save'),
hidden = document.getElementById('hot-Handsontable-value'),
hot,
hotIsValid = true,
emailValidator;
emptyValidator = function(value, callback) {
callback(false);
};
hot = Handsontable(container, {
data: data,
minRows: 1,
minCols: 21,
maxCols: 21,
minSpareRows: 1,
stretchH: 'all',
colHeaders: ['Test'],
columns: [{data:'Test',allowInvalid:true, validator: emptyValidator}]
});
// exclude empty rows from validation
$('.title-block .united-alert a[href^=#Handsontable]').click(function() {
var href = $(this).attr('href');
var row = href.getIdIndex();
var prop = /([^__]*)$/.exec(href)[0];
hot.selectCellByProp(parseInt(row), prop);
return false;
});
// Save event
Handsontable.Dom.addEvent(save, 'click', function(e) {
var test = hot.validateCells(); // test is undefined
if (hotIsValid === true) {
hidden.value = JSON.stringify(hot.getData());
} else {
e.preventDefault();
}
});
What you should be doing, instead of var test = hot.validateCells() is the following:
// Save event
Handsontable.Dom.addEvent(save, 'click', function(e) {
hot.validateCells(function(hotIsValid) {
if (hotIsValid === true) {
hidden.value = JSON.stringify(hot.getData());
} else {
e.preventDefault();
}
})
});
Notice that validateCells takes a callback and returns undefined. This is why you see test as undefined. One other thing to note is that the callback is executed for every cell in your table so be careful with it.

How do I retain filters in JqGrid whilst still getting remote data?

I'm using this demo to display text and dropdown list filters in the columns of a JqGrid. The grid has a remote data source and with each sort, filter, or page view etc, it grabs the data from the remote source.
The problem I am having is that when the new data arrives, the grid is refreshed, and the filters revert to default. I've looked at a few examples by Dr Oleg but I can't get it to work with remote data and persistence. Any setting of datatype to "local" or loadonce to true breaks the remote datasource.
Does anyone have any ideas of how to get this to work?
I've tried the following, but as I said, this stops the JqGrid from making API requests:
loadComplete: function () {
var $this = $(this);
var postfilt = $this.jqGrid('getGridParam', 'postData').filters;
var postsord = $this.jqGrid('getGridParam', 'postData').sord;
var postsort = $this.jqGrid('getGridParam', 'postData').sidx;
var postpage = $this.jqGrid('getGridParam', 'postData').page;
console.log(postfilt);
console.log(postsord);
console.log(postsort);
console.log(postsort);*/
if ($this.jqGrid("getGridParam", "datatype") === "json") {
setTimeout(function () {
$this.jqGrid("setGridParam", {
datatype: "local",
postData: { filters: postfilt, sord: postsord, sidx: postsort },
search: true
});
$this.trigger("reloadGrid", [{ page: postpage}]);
}, 25);
}
}
I think the issue has something to do with the select2 dropdown menus. Here you can see it destroys the filter menu and recreates it.
var options = colModelOptions, p, needRecreateSearchingToolbar = false;
if (options != null) {
for (p in options) {
if (options.hasOwnProperty(p)) {
if (options[p].edittype === "select") {
options[p].editoptions.dataInit = initSelect2;
}
if (options[p].stype === "select") {
options[p].searchoptions.dataInit = initSelect2;
}
$grid.jqGrid("setColProp", p, options[p]);
if (this.ftoolbar) { // filter toolbar exist
needRecreateSearchingToolbar = true;
}
}
}
if (needRecreateSearchingToolbar) {
$grid.jqGrid("destroyFilterToolbar");
$grid.jqGrid("filterToolbar", filterToolbarOptions);
}
}
If there was a way this could be done just once per JqGrid load rather than per every request, then that may be a step in the right direction.
The answer to this was quite simple, it just didn't present itself to me until the next morning. I simply wrapped the code that built the search bar in a boolean check, so that it only loaded once.
// somewhere in the class
var gridLoaded = false;
// and in the JqGrid initialization
loadComplete: function (response) {
if (!gridLoaded) {
var options = colModelOptions, p, needRecreateSearchingToolbar = false;
if (options != null) {
for (p in options) {
console.log(p);
if (options.hasOwnProperty(p)) {
if (options[p].edittype === "select") {
options[p].editoptions.dataInit = initSelect2;
}
if (options[p].stype === "select") {
options[p].searchoptions.dataInit = initSelect2;
}
$(this).jqGrid("setColProp", p, options[p]);
if (this.ftoolbar) { // filter toolbar exist
needRecreateSearchingToolbar = true;
}
}
}
if (needRecreateSearchingToolbar) {
$(this).jqGrid("destroyFilterToolbar");
$(this).jqGrid("filterToolbar", filterToolbarOptions);
}
}
gridLoaded = true;
}
}
Thanks again to Dr Oleg for the help.

Bootstrap typeahead suggestions replaced when navigation

I'm using Bootstrap Typeahead to suggest som search results. The results are returned from a ajax ressource, and since this resource creates a delay, I'm experiencing a unfortunate effect.
Example:
If typing a 4 letter word, the suggestions will appear after 2 letters, I can then go through the results with the keys up/down, but suddenly the suggestions will reload because the last request has finished.
Is there any way to "cancel" any remaining, if user is currently using the keys up/down to go through the suggestions?
('#query').typeahead({
items: 4,
source: function (query,process) {
map = {};
$.getJSON('/app_dev.php/ajax/autosuggest/'+query, function (data) {
vehicles = [];
$.each(data, function(i,vehicle){
map[vehicle.full] = vehicle;
vehicles.push(vehicle.full);
});
process(vehicles);
});
},
updater: function (item) {
// do something here when item is selected
},
highlighter: function (item) {
return item;
},
matcher: function (item) {
return true;
}
});
I think the following will satisfy your needs (its hard to reproduce exactly) :
There is no easy way to abort a delayed response, but you could extend typeahead as I figured out here (without modifying bootstrap.js)
The concept is to catch keydown, detect if the event is KEY_UP or KEY_DOWN, set a flag is_browsing, and then abort process if is_browsing is true (that is, if the user has hitted KEY_UP or KEY_DOWN and no other keys afterwards).
Extending typeahead :
// save the original function object
var _superTypeahead = $.fn.typeahead;
// add is_browsing as a new flag
$.extend( _superTypeahead.defaults, {
is_browsing: false
});
// create a new constructor
var Typeahead = function(element, options) {
_superTypeahead.Constructor.apply( this, arguments )
}
// extend prototype and add a _super function
Typeahead.prototype = $.extend({}, _superTypeahead.Constructor.prototype, {
constructor: Typeahead
, _super: function() {
var args = $.makeArray(arguments)
// call bootstrap core
_superTypeahead.Constructor.prototype[args.shift()].apply(this, args)
}
//override typeahead original keydown
, keydown: function (e) {
this._super('keydown', e)
this.options.is_browsing = ($.inArray(e.keyCode, [40,38])>-1)
}
//override process, abort if user is browsing
, process: function (items) {
if (this.options.is_browsing) return
this._super('process', items)
}
});
// override the old initialization with the new constructor
$.fn.typeahead = $.extend(function(option) {
var args = $.makeArray(arguments),
option = args.shift()
// this is executed everytime element.modal() is called
return this.each(function() {
var $this = $(this)
var data = $this.data('typeahead'),
options = $.extend({}, _superTypeahead.defaults, $this.data(), typeof option == 'object' && option)
if (!data) {
$this.data('typeahead', (data = new Typeahead(this, options)))
}
if (typeof option == 'string') {
data[option].apply( data, args )
}
});
}, $.fn.typeahead);
This typeahead-extension could be placed anywhere, eg in a <script type="text/javascript"> -section
Testing the extension :
<input type="text" id="test" name="test" placeholder="type some text" data-provide="typeahead">
<script type="text/javascript">
$(document).ready(function() {
var url='typeahead.php';
$("#test").typeahead({
items : 10,
source: function (query, process) {
return $.get(url, { query: query }, function (data) {
return process(data.options);
});
}
});
});
</script>
A "serverside" PHP script that returns a lot of randomized options with forced delay, typeahead.php :
<?
header('Content-type: application/json');
$JSON='';
sleep(3); //delay execution in 3 secs
for ($count=0;$count<30000;$count++) {
if ($JSON!='') $JSON.=',';
//create random strings
$s=str_shuffle("abcdefghijklmnopq");
$JSON.='"'.$s.'"';
}
$JSON='{ "options": ['.$JSON.'] }';
echo $JSON;
?>
It really seems to work for me. But I cannot be sure that it will work in your case. Let me now if you have success or not.

jqGrid need a field editable on Add dialog but not Edit dialog

I'm attempting to use jqGrid in my ASP.Net MVC application and have a requirement that some columns arre editable in the Add dialog but not the Edit dialog. Apparently the way to do this is to use the beforeShowForm javascript event and set the properties on the particular input field.
So far I can't manage to get the beforeShowForm event to fire. Below is an example I found on another SO question but so far I haven't managed to get it working. Is there some trick I'm missing? I'm using the latest 3.8 version of jqGrid.
Controller:
[Authorize]
public ActionResult Index()
{
var gridModel = new MyGridModel();
SetUpGrid(gridModel.MyGrid);
return View(gridModel);
}
private void SetUpGrid(JQGrid grid)
{
grid.DataUrl = Url.Action("GridDataRequested");
grid.EditUrl = Url.Action("EditRows");
grid.ToolBarSettings.ShowSearchToolBar = false;
grid.ToolBarSettings.ShowEditButton = true;
grid.ToolBarSettings.ShowAddButton = true;
grid.ToolBarSettings.ShowDeleteButton = true;
grid.ToolBarSettings.ShowRefreshButton = true;
grid.EditDialogSettings.CloseAfterEditing = true;
grid.AddDialogSettings.CloseAfterAdding = true;
grid.EditDialogSettings.Modal = false;
grid.EditDialogSettings.Width = 500;
grid.EditDialogSettings.Height = 300;
grid.ClientSideEvents.GridInitialized = "initGrid";
}
Model:
public class MyGridModel
{
public JQGrid MyGrid { get; set; }
public MyGridModel()
{
MyGrid = new JQGrid
{
Columns = new List<JQGridColumn>()
{
new JQGridColumn { DataField = "id",
PrimaryKey = true,
Visible = false,
Editable = false },
new JQGridColumn { DataField = "username",
Editable = true,
EditFieldAttributes = new List<JQGridEditFieldAttribute>()
{
new JQGridEditFieldAttribute(){ Name = "readonly", Value = "true"},
new JQGridEditFieldAttribute(){ Name = "disabled", Value = "true"}
},
Width = 100},
new JQGridColumn { DataField = "domain",
Editable = true,
EditFieldAttributes = new List<JQGridEditFieldAttribute>()
{
new JQGridEditFieldAttribute(){ Name = "readonly", Value = "true"},
new JQGridEditFieldAttribute(){ Name = "disabled", Value = "true"}
},
Width = 100}
}
}
}
}
View:
function initGrid() {
jQuery("#myGrid").jqGrid('navGrid','#myGrid-pager',
{ }, //options
{ // edit options
beforeShowForm: function(frm) {
alert("beforeShowForm edit");
}
},
{ // add options
beforeShowForm: function(frm) {
alert("beforeShowForm add");
}
},
{ }, // del options
{ } // search options
);
}
<div>
<%= Html.Trirand().JQGrid(Model.MyGrid, "myGrid") %>
</div>
It seems to me that the answer to your question you will find here and here (look at the example also).
UPDATED: I don't know commertial version of jqGrid. If you don't solve your prblem you should post your question better in the forum http://www.trirand.net/forum/default.aspx.
If I understand your code correct you can try to remove definition of the attributes readonly and disabled (JQGridEditFieldAttribute) from the EditFieldAttributes. Instead of that you can try to do following
If you want to show readonly fields 'username' and 'domain' in the edit dialog you can do following
jQuery("#myGrid").jqGrid('navGrid','#myGrid-pager',
{ }, //options
{ recreateForm: true, // edit options
beforeShowForm: function(form) {
$('#username',form).attr('readonly','readonly');
$('#domain',form).attr('readonly','readonly');
}
});
or without the usage of recreateForm: true option:
jQuery("#myGrid").jqGrid('navGrid','#myGrid-pager',
{ }, //options
{ // edit options
beforeShowForm: function(form) {
$('#username',form).attr('readonly','readonly');
$('#domain',form).attr('readonly','readonly');
}
},
{ // add options
beforeShowForm: function(frm) {
$('#username',form).removeAttr('readonly');
$('#domain',form).removeAttr('readonly');
}
});
If you want not to show the fields 'username' and 'domain' in the edit dialog you can do
jQuery("#myGrid").jqGrid('navGrid','#myGrid-pager',
{ }, //options
{ recreateForm: true, // edit options
beforeShowForm: function(form) {
$('#username',form).hide();
$('#domain',form).hide();
}
});
It should work in the free version of the jqGrd, but because you use SetUpGrid which setup ome settings of jqGrid navigation bar (like grid.ToolBarSettings.ShowEditButton = true). You use also
grid.ClientSideEvents.GridInitialized = "initGrid"
I am not sure what you can do inside of initGrid function. Probably you should define some additional callbackes instead of calling of jQuery("#myGrid").jqGrid('navGrid', ...);. Look at http://www.trirand.net/documentation/aspnet/_2s20v9uux.htm and http://www.trirand.com/blog/phpjqgrid/docs/jqGrid/jqGridRender.html#methodsetNavOptions
I ended up buying the paid for version of jqGrid - the time I save by being able to use a clean .Net object model compared to javascript will pay for itself in no time.
The answer to this question direct from Trirand support is.
You can use the client-side events AfterEditDialogShown and AfterAddDialogShown to disable/enable edit fields for both dialogs. The field for editing/adding will have the same ID is the DataField (case-sensitive). Example:
Model:
JQGrid1.ClientSideEvents.AfterEditDialogShown="disableFields";
JQGrid1.ClientSideEvents.AfterEditDialogShown="enableFields";
View:
<script type="text/javascript">
function disableFields() {
//jQuery("#fieldname").attr("disabled", "disabled");
$("#Source").attr("disabled", "true");
$("#ProgramName").attr("disabled", "true");
$("#Division").attr("disabled", "true");
$("#Medium").attr("disabled", "true");
$("#content").attr("disabled", "true");
}
function enableFields() {
$("#Source").attr("disabled", "false");
$("#ProgramName").attr("disabled", "false");
$("#Division").attr("disabled", "false");
$("#Medium").attr("disabled", "false");
$("#content").attr("disabled", "false");
}
</script>
Current Solution: jqGrid 4.5.4 - jQuery Grid
After this lines at editGridRow (line 7447)
if (rowid === "new") {
rowid = "_empty";
frmoper = "add";
p.caption=rp_ge[$t.p.id].addCaption;
} else {
p.caption=rp_ge[$t.p.id].editCaption;
frmoper = "edit";
}
I put this modification
$t.p.custom_frmoper = frmoper;
Then I can decide what to do whith elements at Popup EDIT and Popup ADD
$.ajax($.extend({
url: $.isFunction(options.dataUrl) ? options.dataUrl.call($t, rowid, vl, String(options.name)) : options.dataUrl,
type : "GET",
dataType: "html",
data: $.isFunction(postData) ? postData.call($t, rowid, vl, String(options.name)) : postData,
context: {elem:elem, options:options, vl:vl},
success: function(data){
var ovm = [], elem = this.elem, vl = this.vl,
options = $.extend({},this.options),
msl = options.multiple===true,
a = $.isFunction(options.buildSelect) ? options.buildSelect.call($t,data) : data;
if(typeof a === 'string') {
a = $( $.trim( a ) ).html();
}
if(a) {
$(elem).append(a);
setAttributes(elem, options, postData ? ['postData'] : undefined);
// CUSTOM CODE
$.each($t.p.colModel, function (i, current) {
if (current.not_editable) {
if ($t.p.custom_frmoper == 'edit') {
$("#" + current.name).attr('readonly', 'readonly');
$("#" + current.name).attr('disabled', 'disabled');
}
else {
$("#" + current.name).removeAttr('readonly');
$("#" + current.name).removeAttr('disabled');
}
}
});
I also added a custom not_editable attribute to the column model to decide that a specific column is editable when ADDING and readonly when MODIFIYING
{ name: 'ID', index: 'ID', not_editable: true }
I hope it helps. I created this modification because SELECT elements does not work with the current solutions in this thread.

Resources