0

I am creating column model at code behind, but I am stack at creating editoption for numeric values which is a function checks the value if its numeric otherwhise alert a message but I couldn't fit javascript function at code behind.

after I model at code behind I convert to json to use at in aspx page.

c# code behind

else if (prop.PropertyType == typeof(decimal))
{
    pr.name = prop.Name;
    pr.index = prop.Name;
    pr.sorttype = "number";
    pr.editoptions = new { dataInit = numericonly };
}

aspx

    function numericonly(elem) {
        $(elem).numeric(false, function() { alert("Integers only"); this.value = ""; this.focus(); });
    }
Mert
  • 6,432
  • 6
  • 32
  • 68

1 Answers1

1

The problem is clear. JSON don't support functions as a type. What you easy can do is the following: 1) you fill strings as the value of dataInit on the server side. 2) you "pre-process" the column model and change illegal string values of dataInit to the reference to the corresponding function. You can "pre-process" the column model before creating of the grid or after creating inside of beforeProcessing callback or even later somewhere before starting of editing. Because dataInit will be used only during editing you have to fix dataInit at any time before the editing will be started.

To change colModel you can either use setColProp method or you can get the reference of internal colModel array by var colModel = $("#gridid").jqGrid("getGridParam", "colModel"); and then do any required modification of some columns. The code will look about as below:

function numericonly(elem) {
    ...
}

...
var colModel = $("#gridid").jqGrid("getGridParam", "colModel"), i, cm;

for (i = 0; i < colModel.length; i++) {
    cm = colModel[i];
    if (cm.editoptions != null && typeof cm.editoptions.dataInit === "string") {
        switch (cm.editoptions.dataInit) {
            case "numericonly":
                // function numericonly is visible here and can be used
                cm.editoptions.dataInit = numericonly;
                break;
            ....
            default:
                delete cm.editoptions.dataInit; // delete wrong value
                break;
        }
    }
}

The old answer contains description of the same idea for the case of custom formatters.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • 1
    @Mert: You are welcome! By the way I would recommend you to use `template` property of `colModel` (see [here](http://stackoverflow.com/a/6047856/315935) for details). You would have the same problem as with `dataInit` because the value of `template` property have to be object and not a string. Nevertheless the usage of column templates mostly corresponds to format based of the **type of data** with all sorting, editing, searching and formatting settings. Using templates you can reduce `colModel` and make your code more readable and maintainable. – Oleg Aug 28 '14 at 07:09