Is there a way to pass extra parameters to Handsontable validator functions? - handsontable

I am using Handsontable v0.35.1 which is the latest version at the time of posting this question. It is used as part of Angular 5 (Typescript) component and view.
For each cell in table, I attach a custom validator as per guidelines in official documentation . Code looks something like this:
class ValidationService {
static myCustomColumnAValidator(value, callback) {
var contextObject = this;
//... validation logic
}
static myCustomColumnBValidator(value, callback) {
var contextObject = this;
//... validation logic
}
}
var hot = new Handsontable(document.getElementById('myTableContainer'), {
data: [ { 'ColumnA': 'Data'}, { 'ColumbB' : 'Data' } }],
columns: [
{
data: 'ColumnA',
validator: ValidationService.myCustomColumnAValidator
},
{
data: 'ColumnA',
validator: ValidationService.myCustomColumnBValidator
}
]
});
Question is, can I pass some extra parameters to the custom validator functions (myCustomColumnAValidator and myCustomColumnBValidator) apart from the value and callback function reference? I need some extra parameters as part of validation logic.
Also note that I had to mark the custom validator functions in ValidationService as static because the context object this is overridden to be the ColumnSettings object when Handsontable calls the validation function. If this was not the case, I could initialize the ValidationService with some member variables via its constructor and use in non-static version of the same validation function using this which would refer to the ValidationService instance. But this does not seem to be possible because Handsontable overrides the "this" context object, which I can see by stepping through the Handsontable code which uses ValidationFunction.call(contextObject, value, callback) mechanism.
Any pointers will be greatly appreciated. Thanks!

Try this way,
columns: [
{
data: 'ColumnA',
validator: (value, callback, extraData = this.something) => {
console.log(extraData);
}
}
]

Pass whatever you want here
columns: [
{
data: 'ColumnA',
validator: ValidationService.myCustomColumnAValidator,
customParameter: 10
}
]
And you can access it like so this.customParameter in the validator.

Using Typescript, I was able to do the following in order to pass an additional parameter to the Handsontable column validator function so the validator can be dynamic based on this parameter. In my case, the parameter was an entire object that had many properties I needed to evaluate:
columns: [
{
data: 'ColumnA',
validator: (value, callback) => {
return this.yourCustomValidatorFunction(value, callback, yourCustomParameterHere)
}
}
]

Related

Sinon with complex object structure

I'm using #types/xrm and attempting to test a method call with sinon. Unfortunately I am hitting quite a few issues due to the complex nature of the return and call I need to mock. I can find really simple examples of sinon stubbing or spying on calls, but nothing more complex than that.
I have the following simple code:
export class AccountForm {
static Fields = class {
static PrimaryContact = "primarycontactid";
static Name = "name";
}
public onLoad(context: Xrm.Events.EventContext): void {
// Get the form context
const formContext = context.getFormContext();
// Get the attributes required
const primaryContact = formContext.getAttribute(AccountForm.Fields.PrimaryContact);
const name = formContext.getAttribute(AccountForm.Fields.Name);
// Add our onchange events
primaryContact.addOnChange(this.onChangePrimaryContact);
name.addOnChange(this.onChangeName);
}
public async onChangePrimaryContact(context: Xrm.Events.EventContext): Promise<void> {
alert("Do something");
}
public async onChangeName(context: Xrm.Events.EventContext): Promise<void> {
alert("Do something else");
}
}
I want to test that an onchange event has been registered to both fields. Ideally, I'd like to check it's the RIGHT onchange, but I'll settle with the fact that it's been called once.
The "easy" way has been to check that the addOnChange method was called twice, this is as below:
import {AttributeMock, XrmMockGenerator} from "xrm-mock";
import * as sinon from "sinon";
import { AccountForm } from "../../src/entities/Account/Form";
describe("Account Form Tests", () => {
describe("Check Onload", () => {
beforeEach(() => {
XrmMockGenerator.initialise();
XrmMockGenerator.Attribute.createString("name", "");
XrmMockGenerator.Attribute.createLookup("primarycontactid", []);
});
it("should register onChange functions", () => {
// Arrange
let formContext = XrmMockGenerator.getFormContext();
let context = XrmMockGenerator.getEventContext();
// Stub
const attributeStub = sinon.stub(AttributeMock.prototype, "addOnChange");
// Act
let form = new AccountForm();
form.onLoad(context);
// Assert
expect(attributeStub.calledTwice).toBeTruthy();
});
});
});
But this is not very resilient, as it is not checking WHICH attributes the onChange functions were added to, or what function was registered.
I've tried stubbing the ForContext's "GetAttribute", but looks like it's requiring me to mock the entire return object, as otherwise, the stub does not return anything? I can get around this with using spy, but still can't work out how to check the attribute that the onChange is being added to and what the function is
Am I missing something obvious here?

How to change validation of a control after it has been initiated?

I am wondering how can I dynamically change Validation on a field. I have control group, which has 5 fields. For this, I want to use custom validator on this control group, to check if any of the inputs are not empty. If any of those are empty, I want to set validation of all of the fields within this control group to be required.
To do this, I need to dynamically change validation of each control to be required, after I verify if any of the inputs are empty. I got the first part - but I still cant figure out how to change validation of a control after it has been initiated. Any ideas?
What I am doing at the moment, is I put a validator on the group like this:
this._formBuilder.group({....}, {validator: customValidator})
You can use a custom validator that changes behavior depending on a value
class MyComponent {
constructor(fb:FormBuilder) {
this.form = fb.group({
c1: ['', (c) => this.myValidator(c)],
...
});
}
someState = true;
myValidator(c:Control) {
if(this.someState && control.value ....) {
}
}
}
This way the validator can for example access the status of the current component. You can also move the validator to another class and pass the method reference of that class to the validator parameter and update properties of this class to change the behavior of the validator.
class MyValidator {
someState = true;
validate(c:Control) {
if(this.someState && control.value ....) {
}
}
}
class MyComponent {
myValidator = new MyValidator();
constructor(fb:FormBuilder) {
this.form = fb.group({
c1: ['', this.myValidator.validate.bind(this.myValidator)],
...
});
}
onSomeEvent() {
this.myValidator.someState = !this.myValidator.someState;
this.form.control.c1.updateValueAndValidity();
}
}

Getting filters used in KendoUI Grid

In KendoUI, I have a grid with filters on. I am more interested in knowing all the filters used by user for saving purpose so that next time user comes in, they can be auto-fill. How do I get all the filters used?
Not sure how your code looks but you should be able to get the Grid's datasource object and call the method filter() to get the currently applied datasource filters. Take a look at the example below taken from here:
<script>
var dataSource = new kendo.data.DataSource({
data: [
{ name: "Jane Doe" },
{ name: "John Doe" }
],
filter: { field: "name", operator: "startswith", value: "Jane" }
});
var filter = dataSource.filter();
console.log(filter.logic); // displays "and"
console.log(filter.filters[0]); // displays '{field: "name", operator: "startswith", value: "Jane"}'
</script>
Just to add to the answers above - if you want to do it purely on the front end without even targeting the actual data source - your can use the following...
var myGrid = $("#gridname").data("kendoGrid");
myGrid.dataSource.filter().filters[0];
You can than break this up into different parameters ...
myGrid.dataSource.filter().filters[0].field
myGrid.dataSource.filter().filters[0].operator
myGrid.dataSource.filter().filters[0].value
Hope this helps - cheers
My configuration is MVC with the data being fetched server-side. What I mean is that the grid calls this method to get its data:
public ActionResult Documents_Read([DataSourceRequest] DataSourceRequest request,
int documentType = (int)ApiGlobals.TrxTypes.חשבונית)
What I do is simple:
I capture the request object in a Session variable and then use the session variable whenever I need it. In the following example I use the filter to print only the documents filtered in the grid:
public ActionResult Documents_Read([DataSourceRequest] DataSourceRequest request,
int documentType = (int)ApiGlobals.TrxTypes.חשבונית)
{
Session["DocumentListFilter"] = request;
...
}
public ActionResult PrintListKendo(int documentType)
{
DataSourceRequest request = (DataSourceRequest)Session["DocumentListFilter"];
if (request == null)
{
request = new DataSourceRequest();
}
request.Page = 1;
request.PageSize = int.MaxValue;
IQueryable<Document> data = _db.GetDocuments()
.Where(d => d.TrxTypeId == (int)documentType);
DataSourceResult filteredData = data.ToDataSourceResult<Document, DocumentListSingleDocumentViewModel>(request,
d => ViewModelFactory.Create(d));
//... call the reporting package with the data, etc.
}
You can save the request object (=serialize it, or save it properties) to the database and reload it when the user opens the relevant page and the grid fetched its data,

ajax call from view to controller in Play framework

I am newbie to MVC and Play framework (Java). Instead of using Groovy for dynamic HTML, I made our own page with static HTML, I mean we haven't any Groovy expressions. Here, I have a controller "Customer", generates JSON object which has to be sent to an ajax call in view. I tried with render() method, seems I haven't used correctly. can you give me some idea to forward from here. thanks.
public static void customer(){
WordAPI objWordAPI=new WordAPI();
List<WordInfo> listObjWord= objWordAPI.MakeAPIObject(nSurveyId);
JSONSerializer modelSerializer=new JSONSerializer().exclude("NSpontanity","NWordRepresentativity","NWordValue","NWordFrequency","class").rootName("Words");
render("Application/wordcloud.html",modelSerializer.serialize(listObjWord));
}
and ajax call in view "wordcloud.html"
$.ajax({
url: "/customer",
dataType : 'json',
success: function (data) {
alert(data);
}
})
I believe this should work:
public static void customer(){
WordAPI objWordAPI=new WordAPI();
List<WordInfo> listObjWord= objWordAPI.MakeAPIObject(nSurveyId);
JSONSerializer modelSerializer=new JSONSerializer().exclude("NSpontanity","NWordRepresentativity","NWordValue","NWordFrequency","class").rootName("Words");
renderJSON(modelSerializer.serialize(listObjWord));
}
I've never used rootName before, I usually just do something more like this:
public static void refreshNotifications()
{
JSONSerializer notifySerializer = new JSONSerializer().include("message","notifyId","class").exclude("*");
List<Notification> notificationList = user.getNotifications();
renderJSON(notifySerializer.serialize(notificationList));
}
Side Note: With refreshNotifications I have a Security method I run before which verifies and populates the user object.

Can't get jQuery Ajax to parse JSON webservice result

I have validated the JSON response from my C# Webmethod, so I don't believe that's the problem.
Am trying to parse the result using simple jQuery $.ajax, but for whatever reason I can't get the method to correctly fire and parse the result, also incidentally can't seem to get the function to fire the result. Are their any limits on the size of the JSON object that can be returned.
I also removed this code from inside a "Site.Master" page because it would always refresh when I hit the simple button. Do tags work correctly with jQuery elements like the button input I'm grabbing from the DOM?
function ajax() {
//var myData = { qtype: "ProductName", query: "xbox" };
var myData = { "request": { qtype: "ProductName", query: "xbox"} };
$.ajax({
type: "POST",
url: "/webservice/WebService.asmx/updateProductsList",
data: {InputData:$.toJSON(myData)},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// var msg = {__type: "Testportal.outputData", id: "li1234", message: "it's work!", myInt:101}
alert("message=" + msg.d.ProductName + ", id=" + msg.d.Brand);
},
error: function (res, status) {
if (status === "error") {
// errorMessage can be an object with 3 string properties: ExceptionType, Message and StackTrace
var errorMessage = $.parseJSON(res.responseText);
alert(errorMessage.Message);
}
}
});
}
And the page:
<asp:Button ID="Button1" runat="server" OnClientClick="ajax();" Text="Button" />
And the Serverside Webmethod:
public class WebService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public OutputData updateProductsList(InputData request)
{
OutputData result = new OutputData();
var db = new App_Data.eCostDataContext();
var q = from c in db.eCosts
select c;
if (!string.IsNullOrEmpty(request.qtype) && !string.IsNullOrEmpty(request.query))
{
q = q.Like(request.qtype, request.query);
}
//q = q.Skip((page - 1) * rp).Take(rp);
result.products = q.ToList();
searchObject search = new searchObject();
foreach (App_Data.eCost product in result.products)
{
/* create new item list */
searchResult elements = new searchResult()
{
id = product.ProductID,
elements = GetPropertyList(product)
};
search.items.Add(elements);
}
return result;
}
And helper classes:
public class OutputData
{
public string id { get; set; }
public List<App_Data.eCost> products { get; set; }
}
public class InputData
{
public string qtype { get; set; }
public string query { get; set; }
}
One problem you may be having is that you aren't doing anything to prevent the button from submitting the form and executing a full postback/reload at the same time you're starting your $.ajax() callback.
I'd suggest wiring this up unobtrusively instead of using the OnClientClick property, like this:
$(document).ready(function() {
// May need to use $('<%= Button1.ClientID %>') if your Button is
// inside a naming container, such as a master page.
$('#Button1').click(function(evt) {
// This stops the form submission.
evt.preventDefault();
$.ajax({
// Your $.ajax() code here.
});
});
});
I also agree with Oleg that you should use json2.js for your JSON stringifying and parsing. In newer browsers, that will fall back to the browsers' native implementations of those methods, which is much faster and makes the parsing safer.
Update:
To answer your question about the data, no that doesn't look quite right.
What you want to ultimately send to the server is this (sans formatting):
{"request":{"gtype":"ProductName","query":"xbox"}}
To accomplish that, you want something like this:
var req = { request : { qtype: "ProductName", query: "xbox" }};
$.ajax({
data: JSON.stringify(req),
// Remaining $.ajax() parameters
});
Keep in mind that request, qtype, and query must match your server-side structure with case-sensitive accuracy.
You can also be more verbose in defining the request object (which I prefer, personally, to keep things clear and readable):
var req = { };
req.request = { };
req.request.qtype = "ProductName";
req.request.query = "xbox";
I've written a bit more about this here, if you're interested: http://encosia.com/2009/04/07/using-complex-types-to-make-calling-services-less-complex/
What does your server side code method look like? Set a break point. Is it even being hit?
It should look something like:
[WebMethod, ScriptMethod]
public string updateProductsList(string qtype, string query)
{ // stuff
}
Also, your javascript data params do not look formatted correctly.
It seems to me that your problem is that you try to use manual JSON serialization. There are more direct way. You should just declare [ScriptMethod (ResponseFormat = ResponseFormat.Json)] or [ScriptMethod (UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] and return return direct an object instead of the string from the web method. On the client side (in JavaScript) I strictly recommend you to use JSON.stringify (from json2.js which can be downloaded from http://www.json.org/js.html) for the constructing of the data parameter of jQuery.ajax.
Look at Can I return JSON from an .asmx Web Service if the ContentType is not JSON? and How do I build a JSON object to send to an AJAX WebService? probably also in JQuery ajax call to httpget webmethod (c#) not working you have have an interest for more experiments.

Resources