Friday, October 23, 2015

Close all kendo windows

$(".k-window-content").each(function(){
  $(this).data("kendoWindow").close();
});

Thursday, October 15, 2015

Sort a kendo grid from razor configuration

Simple, add sort instructions to the data source:

     .DataSource(data => data.Ajax()  
                     .Model(model =>  
                     {  
                       model.Id(record => record.doseID);  
                       model.Field(m => m.drugID).DefaultValue(23);  
                     })  
             .Read(read => read.Action("GetPatientDoses", "PHMedications", new { id = @queryValue, drug = 23, isIV = false, isSQ = false }))  
             .Create(create => create.Action("DoseCreate", "PHMedications").Data("GetqueryValue"))  
             .Update(update => update.Action("DoseUpdate", "PHMedications"))  
             .Destroy(delete => delete.Action("DoseDelete", "PHMedications"))  
             .Sort(m =>  
             {  
               m.Add("doseEndDate").Ascending();  
               m.Add("doseStartDate").Descending();  
             })  
             .PageSize(2)  

How to reload Kendo Grid Datasource and reload grid

An example in the Save event of the grid:

 function OralSaving(e) {  
   OralValidate(e);  
   if (e.model.isNew()) {  
     $('#Oraldosegrid').data('kendoGrid').dataSource.read();  
     $('#Oraldosegrid').data('kendoGrid').refresh();  
   }  
 }  

Editor Template not binding to model on Grid Popup Save

I had a kendo grid that used popup edit mode.  On the popup editor template had a typical Razor @Html.EditorFor(model => model.fk_DrugBrandID)


When editing/adding a record, the editor correctly showed a drop down list of items, but on save, it was not updating the model.  Spent hours trying to figure it out, then realized I had encountered this before.  It is because the grid does not handle nullable foreign keys!  The solution is to use the Save Event of the grid to something like this:



 //grid doesn't handle nullable foreign keys, this passes the value to the controller  
   function tvyascoSave(e) {  
     /*alert('saving: ' + e.model.fk_DrugBrandID);*/  
     if (!e.model.fk_DrugBrandID) {  
       //change the model value  
       e.model.fk_DrugBrandID = 0;  
       //get the currently selected value from the DDL  
       var currentlySelectedValue = $(e.container.find('[data-role=dropdownlist]')[0]).data().kendoDropDownList.value();  
       //set the value to the model  
       e.model.set('fk_fk_DrugBrandID', currentlySelectedValue);  
     }  
   }