Dependent Field doing weird things

Dependent Field doing weird things

jtoler5jtoler5 Posts: 93Questions: 34Answers: 3

I know this is a broad question and not very precise, but what is wrong with the below code? It's causing all sorts of errors on these 3 fields. One issue is it auto opens the select dropdowns on success. It also appears to be losing it's value for certain fields at certain points. Any help would be much appreciated!

// Update levels on edit of rows for level1
    editor.dependent( 'charges.B1', function ( val, data, callback ) {
        $.ajax({
            url: '../update_scripts/update_dropdown/level2.php',
            type: 'POST',
            dataType: 'JSON',
            data: data,
            success: function (json) {
                // Perform update on fields
                if (json['options']['charges.B2'] != null) {
                    callback(json);
                }
                if (json['values']['charges.B2'] != null) {
                    // Check if B2 is in B3
                    var b1 = json['values']['charges.B2'].substr(0, 1);
                    var b2 = val.substr(0, 1);
                    if (b1 !== b2) {
                        editor.set('charges.B2', '');
                    }
                }
            }
        });
    });

    // Update levels on edit of rows for level2
    editor.dependent( 'charges.B2', function ( val, data, callback ) {
        $.ajax({
            url: '../update_scripts/update_dropdown/level3.php',
            type: 'POST',
            dataType: 'JSON',
            data: data,
            success: function (json) {
                if (json['options']['charges.B3'] != null)
                    callback(json);
            }
        });
    });

Level2.php

$data = [
    'values' => [
        'charges.B2' => $L2ID,
    ],
    'options' => [
        'charges.B2' => $level2Array
    ]
];

echo json_encode($data);

Level3.php

$data = [
    'values' => [
        'charges.B3' => $L3ID,
    ],
    'options' => [
        'charges.B3' => $level3Array
    ]
];

echo json_encode($data);

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Hi,

    Any chance you can give me a link to the page?

    The loosing of values typically occurs when the field has a value that is not in the options available in the select list.

    Allan

  • jtoler5jtoler5 Posts: 93Questions: 34Answers: 3

    Ehh. That will be a litte hard. It is hosted on a business intranet. I have been looking at the server response and it is getting correct information. What should be happeneing is... User selects Level1, update L2 and L3... User selects Level2, update L3. But I believe it has something to do with how I am declaring the dependent fields or passing back the values. Does this help at all?

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Answer ✓

    If you were to do this:

    editor.dependent( 'charges.B1', '../update_scripts/update_dropdown/level2.php' );
    editor.dependent( 'charges.B2', '../update_scripts/update_dropdown/level3.php' );
    

    what happens? That will let Editor's built in logic handle the returned JSON data. It does mean that the logic for // Check if B2 is in B3 isn't present, but I'm not sure I understand that, based on the JSON data that is being returned. it gives options and values - can't the server just give an appropriate value to set?

    Allan

  • jtoler5jtoler5 Posts: 93Questions: 34Answers: 3

    It seems to work for inline editing but not for new entries. It wants to select the last option in each dropdown, no matter what I select. Maybe I can try to explain the concept behind B1, B2, and B3.... wish I could just give you a link.

    So each level (Bx) is four digits long.
    B1 is 1000, 2000, 3000, etc
    B2 is 1100, 2100, 3100, etc
    B3 is 1120, 2120, 3150, etc

    I hope this helps understand what I am trying to do... each level has its own table. So the user is present with 1000-7000 for Level1, the options for Level1 drop down do not need to change, as it is really the base of what should show for B2 and B3. Lets say the user selects a Level 1 of 2000, B2 should only show options where L2L1 (a column in the database) matches 2000. Same logic applies to B3, user select 2100 for Level2, B3 should only show where L3L2 matches 2100.

    Here is the response from the server for an inline edit. Before clicking on the cell the row had
    Level 1 = 2000
    Level 2 = 2600
    Level 3 =

    ../update_scripts/update_dropdown/level2.php

    {"values":{"charges.B2":"2600"},"options":{"charges.B2":[{"value":2100,"label":"Sr. Assoc Dean Office"
    },{"value":2200,"label":"Dept Accounting"},{"value":2300,"label":"Dept BISOM"},{"value":2400,"label"
    :"Dept Economics"},{"value":2500,"label":"Dept Finance"},{"value":2600,"label":"Dept Management"},{"value"
    :2700,"label":"Dept Marketing"},{"value":2800,"label":"Information Technology"},{"value":2900,"label"
    :"Facilities and Operations"}]}}
    

    ../update_scripts/update_dropdown/level3.php

    {"values":{"charges.B3":"2610"},"options":{"charges.B3":[{"value":2610,"label":"Travel"},{"value":2620
    ,"label":"Academic Speaker Events"},{"value":2630,"label":"Student\/Alumni Events"},{"value":2640,"label"
    :"Memberships"},{"value":2650,"label":"Awards"},{"value":2660,"label":"Dept Expenses"}]}}
    

    It also called level3.php 3 times (all 3 requests had the same post and server response listed above)... versus the once it called level2.php.

    I hope this helps!! I appreciate your help with figuring this out, this is the one hurdle I have to get correct before pushing out!

    It is lengthy, but here is the full JS file if it helps...
    http://pastie.org/10329923

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    Hi,

    Apologies for the delay in getting back to you on this. I haven't forgotten, just run out of time - I will try to do so over the weekend or Monday!

    Allan

  • jtoler5jtoler5 Posts: 93Questions: 34Answers: 3

    No problem. Thank you.

  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin

    It seems to work for inline editing but not for new entries

    I'm a little surprised it works correctly for inline editing - inline editing can only be used with one field at a time, so dependent fields don't really lend themselves to use with inline editing.

    Here is the response from the server for an inline edit. Before clicking on the cell the row had Level 1 = 2000 Level 2 = 2600 Level 3 = [...]

    Okay, so this looks good - and just to confirm, this works at this point?

    When you try to create a new row with the main form, what is JSON response that is given when you basically perform the same action?

    Allan

  • jtoler5jtoler5 Posts: 93Questions: 34Answers: 3

    If I put the code you suggested back in and let Editor handle the logic, when I create a new entry, it takes the level's of the very first row in the table and presets the drop downs. Something in my code is just wrong... I started out never using DataTables/Editor and this quickly became an extensive script and table/tables, granted I learned a lot about it now and probably could have done a way better job with it starting over now (something I do not have time for). Regardless here is a screenshot showing what happened when I click New.

    The reason for me saying something is wrong somewhere is that when I click New the first time it calls level2.php one time and level3.php two times. But, When I close the entry modal and I click New for the second time, it calls the php files way more than it should. (See screenshot)

    http://i.imgur.com/NRw8u3C.png

This discussion has been closed.