Load LDAP datas to editor Input

Load LDAP datas to editor Input

dg_datatablesdg_datatables Posts: 51Questions: 10Answers: 1

Hello,
i have a table with 3 simple columns: ID, Name and eMail. In the datatables editor i have only the input field "id". After entering the "id", i want to ask the LDAP if the "id" is right. If it's right, the fields "name" and "email" should be automatically fill out by the LDAP.
If the "id" doesn't exists, the editor has to show an error messages (i.e. "Wrong ID") and waits for a new id or a cancelation.

How can i make this with datatables editor?

Thanks a lot for any help.

Additional Info:
In an earlier version of my project i did this, with an own input page with php and ajax (to get the LDAP datas). But in a new version of my project i want to do everything by datatables with the editor extension. It's easier for the users to use only one style of input forms.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 6,797Questions: 0Answers: 1,165
    edited September 25

    Hi @dg_datatables ,

    You could use dependent() to validate the additional data, or post the error with the -e-api field().error() for the ID.

    Cheers,

    Colin

  • dg_datatablesdg_datatables Posts: 51Questions: 10Answers: 1

    Hi Colin,
    thank you very much for your fast support.
    I'll give the depent() a try. I will answer again after my testing.

    Regards
    Christian

  • dg_datatablesdg_datatables Posts: 51Questions: 10Answers: 1

    I did not manage to get a result with depent(). I tried the preSubmit(). If i understand it right, this will be fired after the input of the form. This seems for me more logical to use this api. I tried this:

            editor.on( 'preSubmit', function ( e, pdata, action ) {
                $.each( pdata.data, function ( key, values ) {
                    $.ajax({
                        url  : "/include/ajax/loadDisplay.php",
                        type : "POST",
                        data : { nt: pdata.data[ key ][ 'login' ] },
                        success: function(axdata) {
                            pdata.data[ key ][ 'tname' ] = axdata;
                        },
                        error: function(xhr) {
                            alert("Ajax Error! (LDAP-Display)\n" + xhr.statusText + "\n" + xhr.responseText);
                        }
                    });
                } );
            } );
    

    pdata.data[ key ]['login'] is the entered NT-Login
    pdata.data[ key ]['tname'] is the field for the displayName

    pdata.data[ key ]['login'] is send via ajax to a small php file. This asked the LDAP and send back the displayName of the NT-Login. This works perfectly, but datatables does not take the feedback from Ajax.

    If i write alert(axdata), i see the response from ajax.
    If i write pdata.data[ key ][ 'tname' ] = 'foo'; then 'foo' is shown as the displayname in datatables. So i can manipulate this field, but why does it not take the ajax response?

    I'm sorry but i can't show the webpage, because it's in an intranet and has no public access.

  • allanallan Posts: 50,842Questions: 1Answers: 7,618 Site admin

    dependent() is the way I would suggest doing this as well. Can you show me the code you tried for it please?

    The problem with using preSubmit here is that it does not expect an async result. Rather it will just immediately submit the form, without waiting for your Ajax response!

    Allan

  • dg_datatablesdg_datatables Posts: 51Questions: 10Answers: 1

    Uhhh no async!
    I will see if i will find my dependent() tries, otherwise i will give it a new try.

  • dg_datatablesdg_datatables Posts: 51Questions: 10Answers: 1

    Ok, i tried this one:

    editor = new $.fn.dataTable.Editor( {
                ajax: "usermngnt_ajax.php",
                table: "#usrmng",
                fields: [ {
                        label: "NT-Login",
                        name:  "login",
                        placeholder: "Please enter login"
                    }, {
                        type:    "hidden",
                        name:    "tname"
                    }
                ]
            } );
    
    editor.dependent( 'login', function ( val, data, callback ) {
                    $.ajax( {
                        url: '/include/ajax/loadDisplay.php',
                        dataType: 'text',
                        success: function ( axdata ) {
                            editor.field('tname').val( axdata );
                        },
                        error: function(xhr) {
                            alert("Ajax Error! (LDAP-Display)\n" + xhr.statusText + "\n" + xhr.responseText);
                        }
                    } );
                } );
    

    My problem is, that nothing is send to the ajax file. The ajax file responses with "not found" and then "not found" is entered in the correct datatables field (tname). Why will the value of the field "login" not send to the ajax?
    Have i to send it manually? i.e. data: { login: login }

  • dg_datatablesdg_datatables Posts: 51Questions: 10Answers: 1

    I played a little around and changed the code to the following:

            editor.dependent( 'login', function ( val, data, callback ) {
                $.ajax( {
                    url: '/include/ajax/loadDisplay.php',
                    data: { nt: val },
                    "type": "POST",
                    dataType: 'text',
                    success: function ( axdata ) {
                        editor.field('tname').val( axdata );
                    },
                    error: function(xhr) {
                        alert("Ajax Error! (LDAP-Display)\n" + xhr.statusText + "\n" + xhr.responseText);
                    }
                } );
            } );
    

    Now Ajax received the values and response the correct datas (Yewha!). But datatables doesn't accept it. This line editor.field('tname').val( axdata ); is now my problem. 'axdata' is correct filled with the string (displayname from LDAP).

    How do i get it into datatables?

    Thanks a lot for your help.

  • allanallan Posts: 50,842Questions: 1Answers: 7,618 Site admin

    In what way does it not accept it? Is there an error or something else?

    Allan

  • dg_datatablesdg_datatables Posts: 51Questions: 10Answers: 1
    edited November 12

    No, no error message.
    The ajax response with the correct value. If i put "alert(axdata)" in line 8, i see the correct value. But the value will not be accepted by datatables. The editor window closes normaly and you see the website with the datatables table, but the new value is not in there.

    Is this the correct syntax?
    editor.field('tname').val( axdata );

  • colincolin Posts: 6,797Questions: 0Answers: 1,165
    Answer ✓

    Yep, here's an example using that - http://live.datatables.net/vejolizo/1/edit

  • dg_datatablesdg_datatables Posts: 51Questions: 10Answers: 1

    Thank's a lot Colin, it works!

    I had to change
    editor.field('tname').val( axdata );
    into
    editor.field('tname').set( axdata );

  • allanallan Posts: 50,842Questions: 1Answers: 7,618 Site admin

    Odd - val should work just the same way as set when passing in a parameter. Good to hear you've got it working though.

    Allan

  • dg_datatablesdg_datatables Posts: 51Questions: 10Answers: 1

    @allan
    Hmmm, it seems that val is only readable and to insert values you have to use set.

  • colincolin Posts: 6,797Questions: 0Answers: 1,165

    It does with both - I wrote the unit tests :) See that last example updated here.

  • dg_datatablesdg_datatables Posts: 51Questions: 10Answers: 1

    But why does val not work to set a value for me?

  • colincolin Posts: 6,797Questions: 0Answers: 1,165

    Without seeing it, it's hard to say. Are you able to update my test case above to demonstrate that issue?

  • dg_datatablesdg_datatables Posts: 51Questions: 10Answers: 1
    edited November 15

    I find out that val takes a lot of time zu proceed. If i use val in the Code and fill out the needed data in the field and directly press on the "Update" or "New" button, then i will get no results.
    If i wait around 10 secs. before i press the button i will get an result.

    I understand that it's very hard for you to find an resolution without seeing the website. But perhaps you have a hint where i have to look in the code.

  • colincolin Posts: 6,797Questions: 0Answers: 1,165

    I'd say if field().set() works stay with that. They are doing the same thing in the code, so it shouldn't be making a difference, and impossible to suggest where to look. If that solution doesn't work, we'd need to see it - either by updating my example (which is showing them both working), or a link to your page.

  • dg_datatablesdg_datatables Posts: 51Questions: 10Answers: 1

    Unfortunately, I can not share the website, as it runs on a corporate intranet and does not have internet access.
    I will use set as it works with it. I had only wondered why val did not work.

Sign In or Register to comment.