How to use dependent() to update one select field values from another field

How to use dependent() to update one select field values from another field

Frank YuFrank Yu Posts: 22Questions: 7Answers: 3

I've looked at ur example regarding the dependent(), but still cannot figure it out. The following is the situation. The user select the department and I will update the staff select fields.

editor.dependent('e.deptid', funciton(val, data, callback) {
        var staffids= $.ajax({
            url     : "ajax/staffs_json.php",
            data    : {
                        "action"        : "getRecords4Select_All",
                        "deptid"        : $("#deptid").val();
                      },
            type    : 'post',
            cache   : false,
            async   : false,
            dataType: 'json',
            success : function(json) {
                      callback(json);
            }
        });
    };

Answers

  • allanallan Posts: 57,300Questions: 1Answers: 9,127 Site admin

    Hi,

    Thanks for posting the code. The basic okay looks okay - two questions though:

    1. It doesn't look like you are using the selected value of e.deptid (the first parameter passed into the function) - is that intentional? (Unless you are giving that input element in the Editor form the id #deptid using some callback, rather than the Editor assigned id.)
    2. What is the value of json (or more specifically, what does the object contain?).

    Thanks,
    Allan

  • Frank YuFrank Yu Posts: 22Questions: 7Answers: 3

    Got the problem solved. All I have to do is return an options array with the data I want to put for the form's selection field, just like the first I got the data from the server. The following is the code I use:

    editor.dependent('e.deptid', function(val,data,callback){
                $.ajax ( {
                    url         : 'ajax/staffs_json.php',
                    data        : {
                                    "action"        : "getRecords4Select_All",
                                    "deptid"        : val
                                },
                    type        : 'post',
                    dataType    : 'json',
                    success     : function ( json ) {
                                    callback( json );
                                  }
                });
            }) ;
    

    The only different is the server's reply data format.
    Reply with Values array & Options array.

    {"values":{"e.staffid":"All"},"options":{"e.staffid":[{"label":"\u5168\u90e8","value":"All"},{"label":"ASDaf","value":"4234"},{"label":"BCE","value":"23223234"}]}}

  • allanallan Posts: 57,300Questions: 1Answers: 9,127 Site admin

    Good to hear you've got it sorted. If there is any particular part of the dependent() documentation you found confusing, I'd certainly be interested in knowing so I can try to improve it.

    Allan

  • Frank YuFrank Yu Posts: 22Questions: 7Answers: 3

    I would suggest that you could put an dependent() example on the web for getting data from the server triggerred by a Select field. The reason is that I don't use ur server pdo class, I use mine. And I just need to know the data format being exchanged between.
    The example you put is hide/show, don't help me a lot to understanding the data flow.

  • allanallan Posts: 57,300Questions: 1Answers: 9,127 Site admin

    Superb feedback - thanks! I will look at adding that!

    Allan

This discussion has been closed.