Forums

Dropdown list from Database

bcouvin 06 Jan, 2016
Hello,

I have a field with a select type in my listing. This selector (dropdown) calls onload a javascript function which calls also a php file to find a list of users. I used a ajax method to get this list fro database on load.

My problem: The list of users is not loaded as options in my dropdown list. Here are my codes. The id here is my project_id. The list of users is the list of project team members related to the project_id.

In FrontList/Settings/Fields, the definition of my selector for each row (indexed by Model.id) is:

Model.field:<select id="Model_{Model.id}" onload="GetMyList({Model.id})"><option value="">MyList</option></select>


In FrontList/List Display/Table/Header:

<script>

function GetMyList(id) {
   jQuery(document).ready(function() {
      var $myvalue = jQuery("#Model_"+id);
      jQuery.ajax({
         url: 'GetMyList.php',
         type:'POST',
         data:'ID='+id,
         dataType: 'json',
         success: function(json) {
            jQuery.each(json, function(user_id, name) { 
                $myvalue.append('<option value="'+ user_id +'">'+ name +'</option>');
            });
         }
      });
   });
}
</script>


The 'GetMyList.php' php code is:

//  
<?php

$project_id = $_POST['ID'];

......

while ($row = mysql_fetch_assoc($results)) {
   $user_id  = $row['user_id'];
   $user_name= $row['name'];
   $json[] = array($user_id => $user_name);
    }
   echo json_encode($json);
?>


I tested this code independently and this one gives:

[{"794":"John Smith"},{"831":"Barbara Gould"}]


This result is what I expected, with 2 users with their related id and name. The problem should not come from the php code.
When I have a look on the id of the dropdown list (Model_{Model.id}) from by browser in inspector mode, I don't get the options added.

Do you have an idea what is wrong with that?

Thanks for your help
Bertrand
GreyHead 06 Jan, 2016
Hi Bertrand,

The $ in $myvalue may be the problem - I don't think JavaScript variables names can start with $

Please see this StackOverFlow answer for a similar code example.

Bob
bcouvin 06 Jan, 2016
Hello Bob,

I tried to write directly the value of $myvalue to bypass this problem and also to force the value of json. Here is the code:
    <script>

    function GetMyList(id) {
       jQuery(document).ready(function() {
          jQuery.ajax({
             url: 'GetMyList.php',
             type:'POST',
             data:'ID='+id,
             dataType: 'json',
             success: function(json) {
                jQuery.each({794:"John Smith",831:"Barbara Gould"}, function(user_id, name) { 
                    jQuery("#Model_19").append(jQuery('<option></option>').val(user_id).html(name) );
                });
             }
          });
       });
    }
    </script>


There is nothing changed. Do you know if we could restructure the html element after the web page had been loaded?
I had the similar problem in my last support request. I could not change the structure of html element, once the page has been loaded:
https://www.chronoengine.com/forums/posts/f12/t101064/add-id-in-the-cells-of-ccv5-listing.html

My initial objective was to get an array of value in my Model.field. This array is the list of Project Team members.
This is why I would like to use a dropdown list for this to select the particular member of the project (i.e. the value of my array).

If you had a workaround, it would be great.

Thanks

Bertrand
GreyHead 06 Jan, 2016
Hi Bertrand,

You should be able to do it like this - please post or PM a link to the listing so that I can take a look.

Bob
bcouvin 07 Jan, 2016
Hello Bob,

I almost solve my problem. It still have an little point on ajax call for each row.

The point is that the function onload has to be used (this is what I saw in different forum) with the body tag

Model.field:<select id="Model_{Model.id}"><option value="">MyList</option></select><body onload="GetMyList({Model.id})">


The GetMyList() has been executed for the first row (i.e. Model.id) met.

My question is how to call GetMyList() for each row?

Thank for your help.
Bertrand
GreyHead 07 Jan, 2016
Hi Betrand,

I don't know - I'll see if I can build a list to test. I assume that it is different for each row?

Bob
bcouvin 21 Jan, 2016
Answer
1 Likes
Hello,

Pb solved. Here is the method:

The solution is to initialize first the empty dropdown elements with each id named 'Model.field_{Model.id}'. The {Model.id} is the project reference number. It is project_id. the name of 'Model.field_{Model.id}' is given by CCv5 for each row.

When the webpage has finished to load, this will launch GetMyList() by <body onload=GetMyList()> to execute the function to find for each project the related users. We code this function in the header box of CCv5"

To find the project_id, we write first a function to find all elements with the name started with 'Model_field'.

We get then an array of id of dropdown list. In this array, we extract the {Model.id}.

Those ids are the list of specific project_id that we wanted.

As we got this project_id, we make a ajax call to find all the users belonging to those project_id.

We return the list of users belonging to project_id to the dropdown list that we know its id which is 'Model.field_{Model.id}'.


Bertrand
This topic is locked and no more replies can be posted.