2

I need to do two jobs to be done,

1.Add datepicker in filter toolbar of jqgrid in one of the column. So when i select the datepicker i'll filter the date column.

2.When i click edit of the row in grid a popup will be shown in which we will have all the column values. In that one value is DateTime so i need a datepicker in that field so during an update i can use the datepicker for the specified column.

I'm adding the header values,types and grid values dynamically.

//Code:

        var colname = [];
            var colHeader = [];

            $.ajax({
                url: '@(Url.Action("LoadColumns", "Home"))',
                datatype: 'json',
                mtype: 'GET',
                success: OnComplete,
                error: OnFail
            });
            function OnComplete(result) {

                $.each(result.rows, function (inx,val) {

                   colHeader.push(this.Name);

                   switch (this.Datatype) {

                        case 'int':
                            colname.push({ name: this.Name, index: this.Name, width: 200, align: 'left', sortable: true, editable: false, sorttype: 'int' });
                            break;
                        case 'String':
                            colname.push({ name: this.Name, index: this.Name, width: 200, align: 'left', sortable: true, editable: true });
                            break;
                        case 'DateTime':
                            colname.push({
                                name: this.Name, index: this.Name, width: 200, align: 'left', sortable: true, editable: true, sorttype: 'date',
                                formatter: 'date', formatoptions: { newformat: 'm-d-Y' }, datefmt: 'm-d-Y' 
                            });
                            break;
                        case 'dropdown':
                            if (this.ValueList != null && this.ValueList != '') {
                                var ValueListArray = this.ValueList.split(" ");
                                var valueListItems = '';
                                $.each(ValueListArray, function (index, values) {
                                    valueListItems = valueListItems + values + ":" + values + ";";
                                });
                            }
                            colname.push({
                                name: this.Name, index: this.Name, width: 200, edittype: "select", formatter: 'select',
                                editoptions: { value: valueListItems.slice(0, -1) }, stype: 'select'
                                        , searchoptions: { value: ':All;' + valueListItems.slice(0, -1) }, align: 'left', sortable: true, editable: true
                            });
                            break;

                    }


                });
   jQuery("#jQGridDemo").jqGrid({
            url: '@(Url.Action("LoadData", "Home"))',
            datatype: "json",
            mtype: 'GET',
            colNames: colHeader,
            colModel: colname,

            jsonReader: {
                root: 'rows',
                id: 'ProductId',
                repeatitems: false
            },
            rowNum: 10,
            rowList: [10, 20, 30],
            pager: '#jQGridDemoPager',
            sortname: '_id',
            viewrecords: true,
            loadonce: true,
            sortorder: 'desc',
            caption: "Grid",
            gridview: true,
            autoencode: true,
            ignoreCase: true,
            editurl: '@(Url.Action("EditData", "Home"))'

        });


        jQuery("#jQGridDemo").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false, defaultSearch: "cn" });

        $('#jQGridDemo').jqGrid('navGrid', '#jQGridDemoPager',
                   {
                       edit: true,
                       add: true,
                       del: true,
                       search: true,
                       searchtext: "Search",
                       addtext: "Add",
                       edittext: "Edit",
                       deltext: "Delete"
                   },
                   {   //EDIT
                       //                       height: 300,
                       //                       width: 400,
                       //                       top: 50,
                       //                       left: 100,
                       //                       dataheight: 280,
                       closeOnEscape: true, //Closes the popup on pressing escape key
                       reloadAfterSubmit: true,
                       drag: true,
                       afterSubmit: function (response, postdata) {
                           debugger;
                           if (response.responseText == "") {

                               $(this).jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid'); //Reloads the grid after edit
                               return [true, '']
                           }
                           else {

                               $(this).jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid'); //Reloads the grid after edit
                               return [false, response.responseText]//Captures and displays the response text on th Edit window
                           }
                       },
                       editData: {
                           EmpId: function () {

                               var sel_id = $('#jQGridDemo').jqGrid('getGridParam', 'selrow');
                               var value = $('#jQGridDemo').jqGrid('getCell', sel_id, '_id');
                               return value;
                           }
                       }
                   },
                   {
                       closeAfterAdd: true, //Closes the add window after add
                       afterSubmit: function (response, postdata) {

                           if (response.responseText == "") {

                               $(this).jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid')//Reloads the grid after Add
                               return [true, '']
                           }
                           else {
                               alert(response);
                               $(this).jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid')//Reloads the grid after Add
                               return [false, response.responseText]
                           }
                       }
                   },
                   {   //DELETE
                       closeOnEscape: true,
                       closeAfterDelete: true,
                       reloadAfterSubmit: true,
                       closeOnEscape: true,
                       drag: true,
                       afterSubmit: function (response, postdata) {
                           if (response.responseText == "") {

                               $("#jQGridDemo").trigger("reloadGrid", [{ current: true}]);
                               return [false, response.responseText]
                           }
                           else {
                               $(this).jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid');
                               return [true, response.responseText]
                           }
                       },
                       delData: {
                           EmpId: function () {
                               var sel_id = $('#jQGridDemo').jqGrid('getGridParam', 'selrow');
                               var value = $('#jQGridDemo').jqGrid('getCell', sel_id, '_id');
                               return value;
                           }
                       }
                   },
                   {//SEARCH
                       closeOnEscape: true

                   }
            );
    }
    function OnFail(result) {
        alert('Failed');
    }

Not sure where i'm wrong or where to add the datepicker. Any help?

A Coder
  • 3,039
  • 7
  • 58
  • 129
  • [see this](http://stackoverflow.com/questions/20278496/how-to-add-datepicker-in-jqgrid) may be useful. – Bhushan Kawadkar Jul 22 '14 at 12:46
  • It's unclear which problem you have. Do you tried to call `datepicker` inside of `dataInit` of `searchoptions` or `editoptions`? Is your problem is the requirement to force filtering directly after the date have been selected? In the case you can follow [the answer](http://stackoverflow.com/a/6876951/315935). Probably you have some other problem? – Oleg Jul 22 '14 at 12:55
  • 1
    Probably your problem exist because you don't defined any `sopt` inside of `searchoptions`? If use `defaultSearch: "cn"` then the search operation will be applied to **all** columns. Integer, dropdown and DateTime should use *other* default operation in the filter toolbar. See [the answer](http://stackoverflow.com/a/17727805/315935) for an example. In any way you should formulate your question more clear: what is your problem, what is expected results and what you have instead. – Oleg Jul 22 '14 at 13:05
  • @Oleg: Updated the question. Can you look into that? – A Coder Jul 23 '14 at 05:23
  • @Oleg: Can you look into this? http://stackoverflow.com/questions/21854022/error-status-internal-server-error-error-code-500-in-mvc4-jqgrid-edit-and-d – A Coder Aug 05 '14 at 09:19
  • 1
    @SanthoshKumar: Sorry, but I an in vocation now – Oleg Aug 05 '14 at 15:15
  • @Oleg: Thanks for your reply. Let me know once you are back. – A Coder Aug 06 '14 at 06:40
  • @Oleg: Can you please look into this http://stackoverflow.com/questions/25158702/pass-parameters-in-edit-url-of-jqgrid-for-form-editing – A Coder Aug 12 '14 at 10:53
  • I'm still in vocation. I'll try to help you later if the problem will be not solved till the time. – Oleg Aug 12 '14 at 14:39

0 Answers0