简体   繁体   中英

How to get key and value from a js map

I am trying to write a custom grid in jquery so took a look at jquery plugin development. In it I assigned headers for the grid and want to assign rows, but need help with assigning values to rows.

var headers=new Array();
headers=["Name","Host Name","Description","Type","Last Update Time"];

I thought of creating a array of maps like below, which will get assigned as rows in my grid, but need help in understanding how to assign. Similar to headers these rows can be generic.

var myrows = [
                {cfgid:0, name:"Lukas Arts",host:"baxton", description:"test",type:"Dev",updateDate:"Thu Apr 26 04:31:10 IST 2012"},
                {cfgid:1, name:"Adiga",host:"beetle", description:"test",type:"Dev",updateDate:"Fri Apr 27 07:21:43 IST 2012"},
                {cfgid:2, name:"Max Miller",host:"barry", description:"test",type:"Dev",updateDate:"Mon Apr 16 04:11:40 IST 2012"}
                ];

This is the code

jQuery.fn.abcGrid = function (tableid, heading, data, options) {
    abcoptions = jQuery.extend ({
    tableCss: "tableForms",
    tableHeaderCss: "tableHeading",
    label: "Informatica CSM",
    header: heading,
    rows: data
    }, options);

    var ele_table='';
    var ele_table_start='<table id="'+abcoptions.tableid+'" class="'+abcoptions.tableCss+'"><tbody>';
    var ele_table_end='</tbody></table>';
    var ele_table_header='';
    var ele_table_row='';

    var tempHeader='';
    var tempRow='';

    /*Table header creation*/
    for(var i=0;i<abcoptions.header.length;i++)
        tempHeader+='<td class="'+abcoptions.tableHeaderCss+'">'+abcoptions.header[i]+'</th>';
    ele_table_header='<tr>'+tempHeader+'</tr>';

    ele_table=ele_table_start+ele_table_header+ele_table_end;
    $(this).html(ele_table);
    //it creates only headers properly
};

I call this code as

$('#updateDiv').abcGrid("tabel1", headers, myrows);

To get the value of an item in the map, you could do:

abcoptions.rows[i].cfgid

The rows[i] is your map -- you can place the key after the dot, or rows[i]['cfgid']

-- Edit: renamed header to rows .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM