Syed Daniyal Asif
Syed Daniyal Asif

Reputation: 736

Jquery make context menu with array

This is my code, it works perfect without using array, and creates context menu on div, but now i want to create some context menu items from array. when i apply array it is showing all items of array in one line and separates by Comma (,) . but i need all items in different rows.

http://jsfiddle.net/danial786zz/sobnwgeb/ this fiddle is working perfect but i need to make items from array.

$('#div_id").chromeContext({
items: [

      { title: array,         onclick: function () { RToTb(array_name) } },
      { title: 'properties'   onclick: function () { abc();            } },
      { title: 'view'         onclick: function () { def();            } }

    ]

});

Upvotes: 0

Views: 678

Answers (2)

Yunus Aslam
Yunus Aslam

Reputation: 2466

I think this will work for you :

I have created an array out of another array and used that in the context menu.

 $(function(){
    var arr = ['1st Row', '2nd Row', '3rd Row'];
    var menuitems = [];
    $.each(arr, function( index, value ) {
       menuitems[index] =  { title: value, onclick: function () { RToTb(array_name) }};
    });

    $('#div_id').chromeContext({
       items : menuitems
    });
});

FIDDLE

Upvotes: 2

Madcoe
Madcoe

Reputation: 213

It is about your array definition. There should be objects inside each array element.

$(function(){
   var arr=[];
   arr[0]= { 'title': '1st row', 'onclick:': function () { RToTb(array_name); } };
   arr[1]={ 'title': '2st row', 'onclick:': function () { RToTb(array_name); }};
   arr[2]={ 'title': '3st row', 'onclick:': function () { RToTb(array_name); }};


   $('#div_id').chromeContext({
      items: arr
   });
});

Check the link below for working example: http://jsfiddle.net/v7v8f386/1/

Upvotes: 0

Related Questions