Search code examples
kendo-gridkendo-asp.net-mvctelerik-gridtelerik-mvc

KendoGrid MVC - Datasource not set properly


I have this grid:

@(Html.Kendo().Grid<SVD.Web.Areas.Caracteristiques.Models.SegmentViewModel>()
.Name("grdRechercheCarac")
.Scrollable()
.Selectable(selectable => selectable
.Mode(GridSelectionMode.Single)
.Type(GridSelectionType.Row))
.Columns(columns =>
{
columns.Bound(c => c.EnteteGroupeDt).ClientGroupHeaderTemplate("#= dtGroupHeaderText(value)#").Hidden(true);
columns.Bound(c => c.EnteteGridGroupeRtssDirection).ClientGroupHeaderTemplate("#= rtssGroupHeaderText(value)#").Hidden(true);
columns.Bound(c => c.EnteteGridGroupSequenceur).ClientGroupHeaderTemplate("#= sequenceurGroupHeaderText(value)#").Hidden(true);

columns.Bound(c => c.SegmentId).ClientTemplate("#= SegmentId#").Title("Id").Hidden(true);
columns.Bound(c => c.SequenceurId).ClientTemplate("#: SequenceurId #").Title("SequenceurId").Hidden(true);
columns.Bound(c => c.Voie).ClientTemplate("#= Voie #").Title("Voie").Hidden(true);
columns.Bound(c => c.Direction).ClientTemplate("#= Direction #").Title("Direction").Hidden(true);
columns.Bound(c => c.Rtss).ClientTemplate("#= Rtss #").Hidden(true);
columns.Bound(c => c.ChainageDebut).ClientTemplate("#:ChainageDebut#").Title("Chainage début");
columns.Bound(c => c.ChainageFin).ClientTemplate("#:ChainageFin#").Title("Chainage fin");
columns.Bound(c => c.Longueur).ClientTemplate("#:Longueur#").Title("Longueur");
columns.Bound(c => c.TypeRevetementId).ClientTemplate("#: TypeRevetementId #").Hidden(true);
columns.Bound(c => c.TypeRevetementNom).ClientTemplate("#: TypeRevetementNom #").Title("Revêtement");
columns.Bound(c => c.IdRtss).ClientTemplate("#: IdRtss #").Hidden(true);
columns.Bound(c => c.CodeDt).ClientTemplate("#: CodeDt #").Hidden(true);
columns.Bound(c => c.Dt).ClientTemplate("#: Dt #").Hidden(true);
columns.Bound(c => c.SequenceurNom).ClientTemplate("#: SequenceurNom #").Hidden(true);
columns.Bound(c => c.IdDerniereModif).ClientTemplate("#: IdDerniereModif #").Hidden(true);
columns.Bound(c => c.LibConfigIriId).ClientTemplate("#: LibConfigIriId #").Hidden(true);
columns.Bound(c => c.LibConfigIriNom).ClientTemplate("#: LibConfigIriNom #").Hidden(true);
columns.Bound(c => c.LibConfigOrnId).ClientTemplate("#: LibConfigOrnId #").Hidden(true);
columns.Bound(c => c.LibConfigOrnNom).ClientTemplate("#: LibConfigOrnNom #").Hidden(true);
columns.Template(@<text>custom</text>).ClientTemplate("<button title='Afficher les données à valider' class='btn btn-danger'>Données à valider.</button>").Title("Reste à valider").Hidden(true);
columns.Template(@<text>custom</text>).ClientTemplate(
        "<button title='Afficher les graphiques' onClick='boutonAfficherGraphique(event)' class='btn btn-default btnAfficherGraphique'><span class='glyphicon glyphicon-zoom-in'></span></button>" +
        "<button type='button'  title='Afficher le séquenceur sur la carte' onClick='cliquerAfficherCarteSequenceur(event)' class='btn btn-default' id='btnTraceGPSCarac' style='margin-left:10px;'><span class='glyphicon glyphicon-globe'></span></button>" +
        "<button type='button' class='btn btn-default' style='margin-left:10px;' onClick='btnAfficherDetailTrace(this)' title=\"Détails de l\'essai\"><span class='glyphicon glyphicon-list-alt'></span></button>" +
        "<button title=\"Afficher les images de l\'essai\"onClick='boutonAfficherImages(event)' class='btn btn-default imagesResultatsRecherche' style='margin-left:10px;'><span class='glyphicon glyphicon-film'></span></button>"
    ).Title("Actions");
})
.NoRecords(Constants.KENDO_GRID_NO_RECORDS)
.Sortable()
.AutoBind(false)
.DataSource(dataSource => dataSource
    .Ajax()
    .Sort(x => x.Add("ChainageDebut").Ascending())
    .Group(groups => { groups.Add(p => p.EnteteGroupeDt) ; groups.Add(p => p.EnteteGridGroupeRtssDirection); groups.Add(p => p.EnteteGridGroupSequenceur); })        
    .Model(model => model.Id(p => p.Id))
    .Read(read => read.Action("ObtenirResultatsRecherche", "Caracteristiques", new { area = Constants.AREA_CARACTERISTIQUES }).Type(HttpVerbs.Get))
    .Events(e => e.Change("changementResultatsRecherche")
    )
)
.Events(e => e.DataBound("GetHistorique"))
)

When I try to read the datasource via Jquery, it does nothing:

$("#grdRechercheCarac").data("kendoGrid").dataSource.read();

Si I've tried to check what the kendoGrid looks like:

var datasource = $("#grdRechercheCarac").getKendoGrid();

When I look at this in the devTools debugger, I see that the datasource member does not fit at all with the specifications in my initial kendoGrid: I have no "group" set, no "read" action set, no "sort". But the columns and the other general options of the KendoGrid are OK.

Image of devTools Debugger

What am I doing wrong ?


Solution

  • Ok I answered myself. I was working in some existing code, and for a totally unknown reason, there was this code at the beginning of the JS file for the page:

    var grid = $("#grdRechercheCarac").data("kendoGrid");
    grid.setDataSource(newDataSource);
    

    So this was resetting the datasource to the default format.