How to add more then two parameter in custom dropdown list.

$new_array = array();      
$result = $db->query("SELECT id,price,name FROM aos_product_categories where deleted='0'");
 while($row = $db->fetchRow($result)){ 
  $new_array[$row['id']] = $row['name']; 
}
 $GLOBALS['app_list_strings']['products_list']=$new_array;

Here my dropdown list will generate , like this [‘id’]=>['name]. and in the html
in name

here, i need to add the data-price in the html. which will fetch from database during the dropdown creation time.
i like this way,
name

please help me out.

You will need to elaborate more. I don’t why you would have any problem adding the price in as a data parameter. Are you creating the dropdown via smarty or something?

How exactly is the dropdown being generate from your array?

$new_array[$row['id']] = $row['name']; 

if i write this particular line it will create the the value with the key.

so it will come like this,

 <options value='id'>name</options>

if i want to add ‘data’ attribute in the options like this,

<options data-price='5000.0000' value='id'>name</options>

then where to pass the data value while creating the array.

I hope u got some idea what i want to say…

Ok I see what you are doing. I don’t think there is a way to pass in the data attribute the way you are trying to do it. You have to work round Sugars limitations.

I had to do something very similar recently and the way I went about it was to dynamically add the options to the select in the view via JavaScript.

The first thing to do is create a drop-down field in studio as normal but don’t add any options to it. Then place the field on the editview.

Then I created a new custom view.edit file for my module.

In the view file I do my query and echo out JavaScript from the php based on the results from the query. Basically I create two arrays in JavaScript. One for contacts and the other for users.


/**
 * Provides dropdown list of Users or Contacts based on linked project resources
 * See modules/AM_ProjectHolidays/showResources.js for on change function
 */

class AM_ProjectHolidaysViewEdit extends ViewEdit
{

    public function display()
    {
        global $mod_strings;

        if ($_REQUEST['return_module'] == 'Project'){

            $project = new Project();
            $project->retrieve($_REQUEST['return_id']);

            //Get project resources (users & contacts)
            $resources1 = $project->get_linked_beans('project_users_1','User');
            $resources2 = $project->get_linked_beans('project_contacts_1','Contact');
            //sort into descending order
            ksort($resources1);
            ksort($resources2);

            echo "<script type='text/javascript'>";
            echo "var users = [];";
            echo "var contacts = [];";

            foreach($resources1 as $user){
                echo "var user = ['".$user->id."', '".$user->name."', 'User'];";
                echo "users.push(user);";
            }

            foreach($resources2 as $contact){
                echo "var user = ['".$contact->id."', '".$contact->name."', 'Contact'];";
                echo "contacts.push(user);";
            }

            echo "</script>";

        }
        parent::display();
    }


Then I created a custom js file and referenced it from the editviewdefs

custom/modules/AM_ProjectHolidays/showResources.js

In the js file I dynamically added the select options to the dropdown based on the arrays I created above.

Here is the example code I wrote for this in Jquery:


function showResources(value){

    var resource_type = value.value;
    $("#resourse_select").css("visibility", "visible");

    if(resource_type == 'User'){
        //Clear options before inserting new ones
        $("#resourse_select").find('option').remove();
        $.each(users, function(index, value){
            $("#resourse_select").append($("<option>",{
                value: value[0],
                text: value[1]
            }));
        });
        showResourcesName();
    }
    else if(resource_type == 'Contact'){
        $("#resourse_select").find('option').remove();
        $.each(contacts, function(index, value){
            $("#resourse_select").append($("<option>",{
                value: value[0],
                text: value[1]
            }));
        });

        showResourcesName();
    }
}

function showResourcesName(){

    //Populate name with the selected user name
    var text = $("#resourse_select option:selected" ).text();
    $("#name").val(text);
}

You should be able to do something very similar to achieve what you are trying to do.

yah Andy I also did the same thing. By jquery, i splitted the values puting into the option tag as per my requirement. bdw, nice concept u have given here. i will surely check out this.

One more thing here in sugarcrm 6.5 version jquery ‘data’ attribute won’t work . how to update the jquery library new version.

Hmm are you sure about that? Iv not had problem so far. Anyway there nothing stopping you from just including a link to a more updated version of jquery in a custom view.edit file. I dont think it would cause any problems if it was just in that particular module.

yah I am sure about that.

.data()

is not working.

and if you do the same work using

 .attr('data-xxx',45). 

it is working fine.

ok. I got it where to update the jquery library . I just need to rebuild the js .there i was lacking behind. now its ok. :cheer:

Cool.

is this working for you ?

thanks for your reply… you added this is edit view ? will it be working in subpanel?

jaydeep can you show how you achieved it retrieving from sql into dropdown thanks