Skip to main content
Topic solved
This topic has been marked as solved and requires no further attention.
Topic: Reference fields dynamically based on previous entry. (Read 6145 times) previous topic - next topic

Reference fields dynamically based on previous entry.

When I use LookupDependentField, and LookupDependentMatchField on the page I want-  I am able to see "Select A Value" Placeholder drop down box with the correct answer present..

Is it possible to skip the "Select a Value" and see a read only version of that answer displayed without additional clicking/selecting?

When I attempt to do Default Selected Value I think I may be doing it incorrectly if that's the solution...

"SELECT  DISTINCT Goal_1 AS value,Goal_1 AS label FROM student"

I have 3 tables-
Student      - this table has Student_Name, Goal_1, Goal_2, Teacher_Name
Teacher      - Teacher_Name
Chart         - Teacher_Name, Student_Name, Goal_1, Goal_1_value, Goal_2, Goal_2_value, etc...

I want Chart/Add to:

Select a Student and show a read only copy of Goal_1 so that I can assess and assign a  value to Goal_1

Please let me know if I can explain in greater detail. Thank you!



Re: Reference fields dynamically based on previous entry.

Reply #2
Thank you. I did see that option in the other page fields, but not in Select. I might be looking in the wrong area.

I tried to upload pics of examples, but "the upload directory is full."

https://photos.app.goo.gl/Ue3ycq8Paf34j9gz7

That's the area I was poking around in, and I also included a snap of the page to better explain what I mean.

On my page I have to click the drop down to display a value, but I'd like for the value to show automatically. I'm using LookupDependentField to match the goal to the student because Goal_1 is a unique value for each student. On the snip example Goal_2 and Goal_3 are displaying how I'd like, but display incorrect info because they don't reference the Student_Name.  If you're able to guide me in a different way to yield the same result I'm open to that also. I'm quite new at this, but trying to invest the time to improve.

Thanks for your Youtube videos.


Re: Reference fields dynamically based on previous entry.

Reply #3
@phpdad‍ the Read_Only configuration for the select is not there but you can use the Disabled configuration, to disable the select input field.

Re: Reference fields dynamically based on previous entry.

Reply #4
Thank you @willvin I greatly appreciate your reply.

I gave it a shot, but might still be missing or overlooking something. When I disable the field entry I see the placeholder text, but not the value.

Disabled Set to True: https://photos.app.goo.gl/ZBf1a9Ag8dL4W1Zd8

Disabled Set to False: https://photos.app.goo.gl/onvjUBnFWXf6CmUS8

In this case I'm trying to get "Dance Better" to show where my placeholder text is showing.

Thank you again for your assistance.

Re: Reference fields dynamically based on previous entry.

Reply #5
@phpdad I think the issue is with your MySql query for the default value. Please note that the value your query should return should be a single value and not a list. And from what I see on your query, you just copied the query generated for you from the select field and paste in in the Selected Default value, which is wrong.

Your query should look like this
Code: [Select]
SELECT Goal_1 FROM students WHERE (Goal_1 = 'Dance Better')

Re: Reference fields dynamically based on previous entry.

Reply #6
@phpdad I think the issue is with your MySql query for the default value. Please note that the value your query should return should be a single value and not a list. And from what I see on your query, you just copied the query generated for you from the select field and paste in in the Selected Default value, which is wrong.

Your query should look like this
Code: [Select]
SELECT Goal_1 FROM students WHERE (Goal_1 = 'Dance Better')


Thank you. I think I may be trying to do something that's not supported, or possibly need another trick to get me there.
https://photos.app.goo.gl/rMpvR25MnY6tF25DA  <- Video

When I select the student I want Goal_1 to show the personalized individual goal that was set, like "Get Better at coding" in this example.

Then I can have a table of all the relevant info I'd need for reports.



Re: Reference fields dynamically based on previous entry.

Reply #9
@phpdad‍ do you have TeamViewer or AnyDesk installed on your computer?

I have installed TeamViewer

I see a couple videos on your patreon that I hadn't seen yet. I'll try to check those out soon too. Thanks again.

Re: Reference fields dynamically based on previous entry.

Reply #10
@phpdad‍ when you are ready to connect, you can send me your TeamViewer id and password on Patreon.

@Catalin‍ if I do resolve it for him, I will post that here.

Re: Reference fields dynamically based on previous entry.

Reply #11
Quote
@Catalin‍ if I do resolve it for him, I will post that here.
Many thanks, look forward for a solution, it's quite a basic and very useful feature, to be able to autocomplete a field based on another field value, e.g. at on-change event ! Thanks again...


Re: Reference fields dynamically based on previous entry.

Reply #13
@Catalin‍ as promised, here is the solution to his issue.

This👇 was added to his student Select field ClientEvents.
Code: [Select]
$('#ctrl-Student_Name').on('change', function(){ 
    let student_name = $(this).val();
    sendData(student_name);
});

function sendData(student_name){
    var data = {// We are trying to organize our data for the server
        student: student_name
    }
   
    //We now make an ajax post to the server
    var request = $.ajax({
        type: 'POST', // the type of request we are making.
        url: siteAddr+'api/getGoals/?csrf_token='+csrfToken, //our api link/url
        data: data, // the data we are sending to the server
        cache: false, // we dont want the request to be cached.
    });
   
    // Now we check for response that will be gotten from the server.
    // Callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        $('#ctrl-Goal_1').val(response.data.Goal_1);
        $('#ctrl-Goal_2').val(response.data.Goal_2);
        $('#ctrl-Goal_3').val(response.data.Goal_3);
        $('#ctrl-Goal_4').val(response.data.Goal_4);
    });
   
    // Now we check for errors we get when trying to make our post request.
    request.fail(function (jqXHR, textStatus, errorThrown){
    });
}

and this👇 was added to his ApiController.php.
Code: [Select]
	function getGoals(){
    if(!empty($_POST)){ // we check if the post variable is not empty.
$student = $_POST['student']; // pass the student to our variable $student

$db = $this->GetModel();
$db->where("student_name", $student);
$done = $db->getOne("student"); // get our data from database

if($done) { // checks if it was successful or not
$res['success'] = true;
$res['msg'] = "Action Successfully.";
$res['data'] = $done;
} else{
$res['success'] = false;
$res['msg'] = "Error getting student goal.";
}

render_json($res); // we return result of the update query in JSON format as expected.
} else {
            $res['success'] = false;
            $res['msg'] = "No post data detected.";
           
            render_json($res); // here we return access denied if Csrf fails
        }
}

What the codes above do is, get the value from the selected field, which is student, which then sends the value to an API link called getGoals. getGoals then queries the student table to get the student's goals which are then returned to the JS code, which the JS code assigns them to their fields.

Re: Reference fields dynamically based on previous entry.

Reply #14
Many thanks Willvin ! ...Somehow this should be part of the framework, easy to be accesses from the interface in the future. It is a heavy used functionality to populate a field based on another field value, on-change.
Respect & thanks ! ...Catalin