2016-09-01 2 views
0

Ich habe mehr als genug Forschung in diesem, aber immer noch ein Mysterium getan. Ich zog Tabellenschema vom Server, um Spalten zu erstellen (result.colNames) und Modell (result.colModel), die gut funktionieren.Load JQGRID auf AJAX Erfolg mit dynamischen Spalten und Modell

bei demselben Ergebnis, ziehe ich Tabellendaten (result.colD), um bei Erfolg in JQgrid aufzufüllen.

Grid erstellt zwar Erfolg, aber Daten werden nicht geladen.

Hier sind der Code und die Screenshots.

Ich habe viel Zeit in diesem verbracht, dann Posten hier..Hope das wird hier gelöst werden.

$.ajax({ 
        type: "GET", 
        url: "webapi/do/pullSchema/"+display, 
        data: "", 
        dataType: "json", 
        success: function(result) 
        { 

         colD = JSON.stringify(result.colData); 
         colN = result.colNames; 
         colM = result.colModel; 

         jQuery("#list").jqGrid({ 
          data:JSON.parse(colD),datatype: "local", 
          colNames:colN, colModel :colM, 
          pager: jQuery('#pager'), 
          rowNum: 5, 
          rowList: [5, 10, 20, 50], 
          viewrecords: true, 
          caption: 'DAta from table', 
          loadtext:'Loading, please wait'}); 


        }, 
        error: function(x, e) 
        { 
         alert(x.readyState + " "+ x.status +" "+ e.msg); 
        } 
       }); 

unten Beispieldatensätze:

[dbname=null, tables=null, ColNames=[Plan_code, LOB], 
colModel=[{name:'Plan_code',index:'Plan_code',width:255}, {name:'LOB',index:'LOB',width:255}], colData=[{LOB=N, Plan_code=C82ACC}, {LOB=P, Plan_code=C82ACC}, {LOB=B, Plan_code=C82ACC}, {LOB=I, Plan_code=C82ACC}, {LOB=I, Plan_code=C82IRA}, {LOB=R, Plan_code=C82IRA}] 

Antwort

0

Das Problem ist mit colData nicht in einem Format jqGrid zu sein erwartet, es ist json (Dies ist ein häufiger Fehler, den viele jqGrid Entwickler machen) Siehe

die Frage in Here für Details.

Also in Ihrem Fall wäre die Lösung zu manipulieren ColData und erstellen Sie die richtige JSON.

colRows=[{LOB=N, Plan_code=C82ACC}, {LOB=P, Plan_code=C82ACC}, {LOB=B, Plan_code=C82ACC}, {LOB=I, Plan_code=C82ACC}, {LOB=I, Plan_code=C82IRA}, {LOB=R, Plan_code=C82IRA}] 

colData = { 
"total": 1, 
"page": 1, 
"records": colRows.count, 
rows:[{LOB=N, Plan_code=C82ACC}, {LOB=P, Plan_code=C82ACC}, {LOB=B, Plan_code=C82ACC}, {LOB=I, Plan_code=C82ACC}, {LOB=I, Plan_code=C82IRA}, {LOB=R, Plan_code=C82IRA}] 
} 



Send this to jqgrid and then it will know how to display it. 
+0

Länge der Spalte <> MODEL => {name: "Plan_code", index: "Plan_code", Breite: 255}, {name: "LOB", index: "LOB", width : 255} JSONStringify => ["{Name: \" Plan_code \ ", Index: \" Plan_code \ ", Breite: 255}", "{Name: \" LOB \ ", Index: \" LOB \ ", Breite: 255} "] parseJSON => {name:" Plan_code“, index: "Plan_code", Breite: 255}, {name: "LOB", index: "LOB", Breite: 255} Spaltennamen Spalten => Plan_code, LOB JSON Stringify => ["Plan_code", "LOB"] parseJSON => Plan_code, LOB h ardcoding unten, funktioniert gut // colNames: ["Plan_code", "LOB"], // colModel: [{Name: "Plan_code", Index: "Plan_code", Breite: 255}, {Name: "LOB ", index:" LOB ", width: 255}], –

+0

Entschuldigung, ich war mir nicht sicher, wie ich meine nächste große Erklärung posten sollte. –

0

Problem behoben:

unten Funktionen verwendet, um dynamische Spaltenüberschrift und Spalten Modell zu erstellen. Funktion getColNames (Daten) { var keys = []; für (var Schlüssel in Daten) { if (data.hasOwnProperty (Schlüssel)) { keys.push (Schlüssel); } }

return keys; 
} 

function getColModels(data) { 
    var colNames= getColNames(data); 
    var colModelsArray = []; 
    for (var i = 0; i < colNames.length; i++) { 
     var str; 
     if (i === 0) { 
      str = { 
       name: colNames[i], 
       index:colNames[i], 
       key:true, 
       editable:true 
      }; 
     } else { 
      str = { 
       name: colNames[i], 
       index:colNames[i], 
       editable:true 
      }; 
     } 
     colModelsArray.push(str); 
    } 

    return colModelsArray; 
}