从json字符串生成colModel

时间:2013-12-27 15:36:56

标签: jquery json jquery-plugins jqgrid

我有一个Json字符串。我想动态填充JQqgrid。我想从Json字符串填充colmodel。我怎样才能实现。 字符串是这样的 -

var data = {
    'Table':
        [

        { "id": "1", "firstName": "Jenny", "lastName": "Gibson", "companyName": "Wipro", "pictureURL": "../Data/{56A3419B-E150-45A4-95F0-C2344461C4E9}.jpg", "emailAddress": "jgibson@wipro.com" },
        { "id": "2", "firstName": "James", "lastName": "Githers", "companyName": "Burger King", "pictureURL": "../Data/{D309C1DA-FBAE-408D-B5BF-E3678D84FF04}.jpg", "emailAddress": "ggithers@burgerking.com" },
        { "id": "3", "firstName": "Christina", "lastName": "Greenburg", "companyName": "Ford", "pictureURL": "../Data/{BBAC9C68-68E1-49CB-88B6-905041F9982C}.jpg", "emailAddress": "cgreenburg@ford.com" },
        { "id": "4", "firstName": "Ted", "lastName": "Sellet", "companyName": "Chrysler", "pictureURL": "../Data/{2252B9ED-E12A-4795-85E5-81B5E5B82927}.jpg", "emailAddress": "msellet@chrysler.com" },
        { "id": "5", "firstName": "Richard", "lastName": "Alterson", "companyName": "Dave & Busters", "pictureURL": "../Data/{445B7B03-AFFE-42C0-9BBE-42926900478F}.jpg", "emailAddress": "alterson@dandb.com" },
        { "id": "6", "firstName": "Mary", "lastName": "Jacob", "companyName": "Calvin Klein", "pictureURL": "../Data/{83B9083D-BB60-4022-8D99-4BC158519A5E}.jpg", "emailAddress": "jjacob@ck.com" },
        { "id": "7", "firstName": "Scott", "lastName": "Alsworth", "companyName": "U.S. Army", "pictureURL": "../Data/{7EE70E81-B786-4412-AFC7-F83C13657F73}.jpg", "emailAddress": "talsworth@usarmy.gov" },
        { "id": "8", "firstName": "Silvia", "lastName": "Reynolds", "companyName": "Armani", "pictureURL": "../Data/{40880DB2-2962-4BD6-B5C8-D5C1F975F07C}.jpg", "emailAddress": "areyn@armani.com" },
        { "id": "9", "firstName": "Ray", "lastName": "White", "companyName": "Del Taco", "pictureURL": "../Data/{6D2833C1-CA98-4990-B637-B6600A130D7F}.jpg", "emailAddress": "bwhite@deltaco.com" },
        { "id": "10", "firstName": "Kalifa", "lastName": "Timmins", "companyName": "Xavor", "pictureURL": "../Data/{68C70FE2-B18B-4135-BCC9-AC982DE97540}.jpg", "emailAddress": "stimmins@xavor.com" },
        { "id": "11", "firstName": "Jeff", "lastName": "Gilbertson", "companyName": "Wilson", "pictureURL": "../Data/{8FD8A448-0EFE-4505-809C-0A483EEBA9C4}.jpg", "emailAddress": "mgilbertson@WilsonSports.com" },
        { "id": "12", "firstName": "Paulina", "lastName": "Roberston", "companyName": "Sports Authority", "pictureURL": "../Data/{AF7E91A0-6D1F-40B8-A219-0805D7570644}.jpg", "emailAddress": "rrobertson@spauth.com" },
        { "id": "13", "firstName": "Yumiko", "lastName": "Kimura", "companyName": "IBM", "pictureURL": "../Data/{927C8A4F-5518-491C-9DE8-EDE7CBE5A9E8}.jpg", "emailAddress": "waura@ibm.com" },
        { "id": "14", "firstName": "Michelle", "lastName": "Jones", "companyName": "Logica", "pictureURL": "../Data/{23523C2E-0243-487D-B262-42F87C799EC2}.jpg", "emailAddress": "jjones@logica.com" },
        { "id": "15", "firstName": "Allen", "lastName": "Tollison", "companyName": "Warner Bros.", "pictureURL": "../Data/{D696F315-245C-4F1B-B4A9-45BBBA3B3ABC}.jpg", "emailAddress": "ctollison@warnerbros.com" },
        { "id": "16", "firstName": "Claudiu", "lastName": "Renault", "companyName": "Cisco", "pictureURL": "../Data/{D83E749C-BB2F-4562-BAB9-749B040EA486}.jpg", "emailAddress": "renault@cisco.com" },
        { "id": "17", "firstName": "Yara", "lastName": "Ali", "companyName": "Galaxy", "pictureURL": "../Data/{4F88A794-D0DE-4468-8FA7-BFB327CE6BED}.jpg", "emailAddress": "yaraali@galaxy.com" },
        { "id": "18", "firstName": "Tina", "lastName": "Evens", "companyName": "Accenture", "pictureURL": "../Data/{232EFE8C-BF24-45EA-896A-367F15F70A46}.jpg", "emailAddress": "tevens@accenture.com" },
        { "id": "19", "firstName": "Beth", "lastName": "Frizel", "companyName": "ConnectCare", "pictureURL": "../Data/{CFBB2113-AFB6-42B0-9063-F73F905A5A71}.jpg", "emailAddress": "bfrizel@connectcare.com" },
        { "id": "20", "firstName": "Jenny", "lastName": "Shawnson", "companyName": "buy.com", "pictureURL": "../Data/{C74A8C06-C505-492D-B86C-5824D98D3341}.jpg", "emailAddress": "shawsone@buy.com" },
        { "id": "21", "firstName": "Amy", "lastName": "Swanson", "companyName": "Cedar Technology", "pictureURL": "../Data/{533CEC42-D95C-409F-8497-F6B0EC85D8B8}.jpg", "emailAddress": "swanson@cedartech.com" },
        { "id": "22", "firstName": "Seth", "lastName": "Dillins", "companyName": "BMW", "pictureURL": "../Data/{07A253F1-EC12-4ABB-B235-2AA8FAB18277}.jpg", "emailAddress": "ldillins@bmw.com" },
        { "id": "23", "firstName": "Mindy", "lastName": "Ralston", "companyName": "Big Boy", "pictureURL": "../Data/{7E891A87-7160-4F59-9E61-060B4D10A36D}.jpg", "emailAddress": "mralston@bigboy.com" },
        { "id": "24", "firstName": "Cassandra", "lastName": "Gray", "companyName": "Kroger", "pictureURL": "../Data/{68932F0A-407C-42D1-B98C-288AD539B533}.jpg", "emailAddress": "cgray@kroger.com" },
        { "id": "25", "firstName": "Tanya", "lastName": "Ulticittens", "companyName": "Sun Country Travel", "pictureURL": "../Data/{6CC6A773-7C94-4C38-BC91-BCEF34BE35D6}.jpg", "emailAddress": "jutlills@suntravel.com" }

    ]
};

1 个答案:

答案 0 :(得分:0)

我终于能够生成列名称&动态列模块。我正在创建一个列名称& json字符串中的列模块数组。

代码如下 -

var col = GetHeaders(data);
 var colN = columnsData(col);
 var colM = columnsModel(col);



//Get the column names
 function GetHeaders(obj) {
 var cols = new Array();          
 var p = obj.Table[0];


for (var key in p) {               
            cols.push(key);
}


 return cols;
}



 //Create a column name array

function columnsData(Data) {   

 var cd = new Array();           
    for (var i = 0; i < Data.length; i++) {
        cd.push(Data[i]);
    }


 return cd;
 }



 //Create a column module array
 function columnsModel(Data) {


 var cm = new Array();

        for (var i = 0; i < Data.length; i++) {
            cm.push({name:Data[i],index: Data[i],editable: true});

        }



  return cm;
    }