-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Client side filtering when using controller as source #32
Comments
Client-side filtering and server-side filtering are completely on shoulders of developer. loadData: function(filter) {
var d = $.Deferred();
// server-side filtering
$.ajax({
type: "GET",
url: "/items",
data: filter,
dataType: "json"
}).done(function(result) {
// client-side filtering
result = $.grep(result, function(item) {
return item.SomeField === filter.SomeField;
});
d.resolve(result);
})
return d.promise();
} Hope it will help. |
So what i want is dataset to be loaded only once during initialization and all filtering and extracting filtered json on client side.
Sorry for so many questions, I am new to this. Thankyou. |
$("#jsGrid").jsGrid("option", "fields", fields); // here fields array depends on your multiselect
$("#jsGrid").jsGrid("refresh");
$("#jsGrid").jsGrid({
onDataLoaded: function(args) {
// data is loaded, so do whatever you want, here you have also access to loaded data with args.data
}
});
var grid = $("#jsGrid").data("JSGrid"); However, I'm not sure why you need it. You can set any option and call any method using jQuery element. |
Even after providing json data to data field the plugin is posting to server for filtering. here is my initialization code. all i want is client side filtering. $.get('@Url.Action("Action", "Controller")', function(reportJson) {
Also please tell me how to extract the filtered json/data? Rest all is solved. |
Try following approach: don't use $.get('@Url.Action("Action", "Controller")', function(reportJson) {
$("#jsGrid").jsGrid({
width: "100%",
height: "470px",
heading: true,
filtering: true,
inserting: false,
editing: false,
selecting: false,
sorting: true,
paging: true,
pageLoading: false,
autoload: true,
controller: {
loadData: function(filter) {
return $.grep(reportJson, function(item) {
// do filtering
});
}
}, |
Sorry for being a noob. The above snippet gives me the noDataContent message. i want the functionality to be the same as you have provided in the Demo - Basic scenario. but the code there does not show controller block. i am not used to client side code. Can you please share the controller block of that demo that will solve my issue. And please tell how to extract the filtered data. |
You can download jsGrid (file link), and it has fully demo code. |
Thankyou that solves everything. |
hi!
|
Here is some example code for generic client side filtering
|
Hi, im' trying to use ajax request with pagin but dont work $(function () {
$("#jsGrid").jsGrid({
height: "80%",
width: "100%",
autoload: true,
paging: true,
pageLoading: true,
pageSize: 10,
pageIndex: 1,
controller: {
loadData: function (filter) {
var def = $.Deferred();
$.ajax({
contentType: "application/json",
dataType: "json",
url: "/api/sportbooks/" + filter.pageIndex + "/" + filter.pageSize
}).done(function (response) {
def.resolve({
data: response.Rows,
itemsCount: 500
});
});
return def.promise();
}
},
fields: [
{ name: "Id", type: "number", width: 50, },
{ name: "Name", type: "text", width: 100 },
{ name: "Address", type: "text", width: 100 },
{ name: "PhoneNumber", type: "text", width: 100 },
{ name: "email", type: "text", width: 100 },
{ name: "ContactName", type: "text", width: 100 }
]
});
$("#pager").on("change", function () {
var page = parseInt($(this).val(), 10);
$("#jsGrid").jsGrid("openPage", page);
});
}); |
Try to put fixed height (e.g. |
For anyone looking for a case insensitive regex implementation here's mine: loadData: function (filter) {
var d = $.Deferred();
$.ajax({
type: 'GET',
url: ajax_url,
dataType: 'json'
}).done(function (result) {
for (var prop in filter) {
if (filter[prop].length > 0) {
result = $.grep(result, function (item) {
var regexp = new RegExp(filter[prop], 'gi');
if (item[prop].match(regexp)) {
return item;
}
});
break;
}
}
d.resolve(result);
});
return d.promise();
} |
Hi, sorry about re-opening this issue. I'm new to jsGrid. I understand that the implementation of client-side or service-side filtering fall on developers, but why not provide function that will take different strategies for client-side, server-side filter, etc.? In the above example for server side filter, why do I need to iterate through the server-side filtered result one more time before display the results? David |
@wzhao6898, my example above is really how to use server-side and client-side filter at the same time. In most cases you would use only server-side, or sometimes client-side filtering. There cannot be a "function for server-side" filtering (as you put it), since server-side filtering happens on the server. |
But there should be reasonable support for client-side filtering...the current support for client-side filtering is incomplete as clearly described. Don't be a dick... |
@zopyx, GitHub is a very respectful place, so please avoid profanity. Let me explain my point of view: From my use cases filtering seemed way more flexible when it's not embedded in the representation component. In fact no data manipulation is embedded. Even sorting is kinda a separate part. For me there were way more questions, it seemed easier just let a user to define the filtering function. Also the component mission is to be lightweight. That's why I'm trying to avoid too many built-in features. Anyways, this is an open source free project. So if you don't like it, just pick up something that works better for you. No need to offend the author for the stuff he shared for free. |
@zopyx the author is certainly a better person than you. When you'll do something good for strangers, and do not offend them, then you can understand you are out of context here. |
Stop blathering. The maintainer has no feeling for obvious design issues or lack of support for this particular problem where lots of people need to do their own workaround. |
You mean implementation... If you don't like look for better tools elsewhere. No one impose you JsGrid. |
@maurorulli move on, you are not participating in this particular issaue |
And yes, I moved on to other solutions that are better maintained and where the maintainer is willing to discuss issues instead of appearing like an ignorant brick. |
Enjoy |
for everyone.. loadData: function (filter) {
criteria = filter;
var data = $.Deferred();
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "/fapplications",
dataType: "json"
}).done(function(response){
var res = [];
if(criteria.Name !== "")
{
response.forEach(function(element) {
if(element.Name.indexOf(criteria.Name) > -1){
res.push(element);
response = res;
}
}, this);
}
else res = response;
if(criteria.Title !== "")
{
res= [];
response.forEach(function(element) {
if(element.Title.indexOf(criteria.Title) > -1)
res.push(element);
}, this);
}
else res = response;
data.resolve(res);
});
return data.promise();
}, |
Just wanted to say thanks to dewelloper and tabalinas. Finally got filtering working with my js-grid. |
@dewelloper Json does throw on error on indexOf. I have all my "fields" directly under response.results. |
IF YOUR RESULT Is EXPECTING A NUMBER DO if(criteria.<fieldname> !== undefined) loadData: function (filter) {
criteria = filter;
var data = $.Deferred();
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "/fapplications",
dataType: "json"
}).done(function(response){
var res = [];
if(criteria.Name !== "")
{
response.forEach(function(element) {
//Name of your field should be here
//eg. if you have a field called email_id,you would put element.email_id.indexOf(criteria.email_id)>-1 ...
//do this for all your fields
//the indexof error should disappear.
if(element.Name.indexOf(criteria.Name) > -1){
res.push(element);
response = res;
}
}, this);
}
else res = response;
if(criteria.Title !== "")
{
res= [];
response.forEach(function(element) {
if(element.Title.indexOf(criteria.Title) > -1)
res.push(element);
}, this);
}
else res = response;
data.resolve(res);
});
return data.promise();
}, ```
|
Thanks a lot! |
I got is almost working. I still get the error on a number field;
In this case element.IDField is 120893 Then I get the IndexOf error (object does not support) Solved that by using:
|
@htkcodes I'm getting the filtered response but grid is still showing all records. |
in DB file that code but filtter not work...
}()); |
I think So the Then we can trigger client-side filtering like this,
I know this is not ideal, but there should be an alternative way that prevents developers from being confused. |
So basically two types of filtering and two types of json extraction is what i have in mind.
Any help is appreciated.
The text was updated successfully, but these errors were encountered: