ajax.data and seach, style of writing

ajax.data and seach, style of writing

marcpiratmarcpirat Posts: 51Questions: 18Answers: 0

I checked this example to be able to do a search after init table (server side processing ): http://live.datatables.net/gelazeba/1/edit

I created this code

var urlI18n = '/i18n/' + '[(${#authentication.getPrincipal().getLang()})]' + '.json';
                var url = "/search";

                var tableConfig = {
                    "searching" : false, 
                    'columns': [
                        {'data': 'id'}, 
                        {'data': 'buildDate'}
                    ],
                    language: {
                        "url" :  urlI18n
                    },
                    'bLengthChange': false, //hide 'show entries dropdown
                    'processing': true,
                    'serverSide': true,
                    'pagingType': 'simple_numbers',
                    'dom': 'tp',
                };

                var searchSamplingsResultsTable = $("#searchSamplingsResultsTable").DataTable(tableConfig);

                $("#searchSamplingsResultsTable").on('click', function () {
                    tableConfig.ajax={
                        "url" : url
                    };

            /*
            tableConfig.ajax.data={
                        var current = $('#searchSamplingsResultsTable').DataTable();
                        d.page = (current != undefined) ? current.page.info().page : 0;
                        d.size = (current != undefined) ? current.page.info().length : 5;
                        d.sort = d.columns[d.order[0].column].data + ',' + d.order[0].dir;
                        d.search=d.search.value;
                    };

                    tableConfig.search = {
                        d.form = $("#searchSamplingsForm").serializeArray()
                    };
                    */

                    searchSamplingsResultsTable.destroy();
                    searchSamplingsResultsTable = $('#example').DataTable( tableConfig );

                });

How I need to write tableConfig.ajax.data and tableConfig.search?
Seem like should be wrote with json style

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @marcpirat ,

    The problem with the example is that searching is disabled, so no searching will take place. If you comment out that option, as I did here, the load works as expected.

    Cheers,

    Colin

  • marcpiratmarcpirat Posts: 51Questions: 18Answers: 0

    "searching" : false was there to remove the input for searching...

    real problem is more about that

             /*
            tableConfig.ajax.data={
                        var current = $('#searchSamplingsResultsTable').DataTable();
                        d.page = (current != undefined) ? current.page.info().page : 0;
                        d.size = (current != undefined) ? current.page.info().length : 5;
                        d.sort = d.columns[d.order[0].column].data + ',' + d.order[0].dir;
                        d.search=d.search.value;
             };
    
             tableConfig.search = {
                        d.form = $("#searchSamplingsForm").serializeArray()
             };
             */
    
  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    edited July 2018

    Hi @marcpirat ,

    I'm not clear what the problem is! If you could link to a test page, and explain what the issue is, we can take a look,

    Cheers,

    Colin

  • marcpiratmarcpirat Posts: 51Questions: 18Answers: 0
    edited July 2018

    I have a form for the search... user can search by many field, that why I created tableConfig.search.

    In tableConfig.ajax.data, I try to do the bridge between frond end and backend, they don't use same name parameter.

    with this code I get

    Uncaught SyntaxError: Unexpected identifier

    tableConfig.ajax.data={
                var current = $('#searchSamplingsResultsTable').DataTable();
                d.page = (current != undefined) ? current.page.info().page : 0;
                d.size = (current != undefined) ? current.page.info().length : 5;
                d.sort = d.columns[d.order[0].column].data + ',' + d.order[0].dir;
                d.search=d.search.value;
     };
    
     tableConfig.search = {
                d.form = $("#searchSamplingsForm").serializeArray()
     };
    
  • kthorngrenkthorngren Posts: 21,364Questions: 26Answers: 4,956

    Uncaught SyntaxError: Unexpected identifier

    This means you have a syntax error. The error is the way you are making the assignments within the curly braces {}. This represents an object. Take a look at this page which explains how to make assignments within the {}. Also take a look at the example you posted, the syntax is different than yours.

    Kevin

  • marcpiratmarcpirat Posts: 51Questions: 18Answers: 0

    you not posted link...

    for the link i posted they use a json style...

    habitually i do

            'ajax': {
                'type': 'get',
                'url': url,
                'data': function (d) {
                    var current = $('#samplingsTable').DataTable();
                    d.page = (current != undefined) ? current.page.info().page : 0;
                    d.size = (current != undefined) ? current.page.info().length : 5;
                    d.sort = d.columns[d.order[0].column].data + ',' + d.order[0].dir;
                    d.search=d.search.value;
                }
            },
    
  • kthorngrenkthorngren Posts: 21,364Questions: 26Answers: 4,956

    you not posted link...

    Sorry, here is the link:
    https://www.w3schools.com/js/js_objects.asp

    The difference in your last example is you are not setting object values but using a function.

    Kevin

  • marcpiratmarcpirat Posts: 51Questions: 18Answers: 0
    edited July 2018

    tried

        tableConfig.ajax = {
              "url": "ajax/objects_root_array.txt",
              "dataSrc": "",
              "data" : function (d) {
                var current = $('#searchSamplingsResultsTable').DataTable();
                  d.page = (current != undefined) ? current.page.info().page : 0;
                  d.size = (current != undefined) ? current.page.info().length : 5;
                  d.sort = d.columns[d.order[0].column].data + ',' + d.order[0].dir;
                  d.search=d.search.value;
              }
            };
    

    Cannot read property '0' of undefined

    http://live.datatables.net/fepidego/1/edit

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    The problem is because data is trying to reference d (d.columns[d.order[0].column].data) but d is an empty object on entry - see this here with debug.

  • rainkvrainkv Posts: 8Questions: 2Answers: 0

    I want to use this way to connect data from sqlsrv source but not ajax / objects_root_array.txt, what should I do?

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    @rainkv - your question isn't very clear. I suggest you start a new thread and give a better explanation.

This discussion has been closed.