1

I am using jQuery DataTables, I can get data from selected row using this code

var str = $.map(table.rows('.selected').data(), function (item) {
                    return item[5]+" "+item[0]
                });

where item[5] is id, item[0] is a string.

I want to split return string for passing id and string. the error founded in ajax code specifically in

data : {}

where is the problem in this code.

<script>

$(document).ready(function() {
    var table = $('#liveSearch').DataTable();
    $('#liveSearch tbody').off('click', 'tr').on( 'click', 'tr', function () {
        if ( $(this).hasClass('selected') ) {
            $(this).removeClass('selected');
        }
        else {
            table.$('tr.selected').removeClass('selected');
            $(this).addClass('selected');
        }
    } );

        $('.example3-1').on('click', function () {
            if ((table.rows('.selected').data().length) > 0) {
                var str = $.map(table.rows('.selected').data(), function (item) {
                    return item[5]+" "+item[0]
                });
                console.log(str);
            $.confirm({
                confirmButtonClass: 'btn-info',
                cancelButtonClass: 'btn-danger',
                confirm: function () {
                    $.ajax({
                        type: 'post',
                        url: 'delete.php',
                        data: {
                            str1 : str.substr(0,str.indexOf(' ')),
                            str2 : str.substr(str.indexOf(' ')+1)
                        },
                        success: function( data ) {
                            console.log( data );
                        }
                    });
                    table.row('.selected').remove().draw(false);

                }
            });
        }
        });
} );

2
  • Have you tried using the javascript debugger in the browser? Commented Aug 15, 2015 at 23:40
  • No Use the javascript debugger its a great tool and lives in all browsers. Try F12 Commented Aug 15, 2015 at 23:47

1 Answer 1

1

CAUSE

You're only allowing only one row selected. But you're using $.map which returns Array not a string as you expect. There is no sense in using $.map for just one row.

SOLUTION

Use the following code instead to get data for selected row and produce the string needed.

var rowdata = table.row('.selected').data();
var str = rowdata[5] + " " + rowdata[0];

It could be simplified further:

var rowdata = table.row('.selected').data();

// ... skipped ...

$.ajax({

   // ... skipped ...

   data: {
      str1: rowdata[5],
      str2: rowdata[0]
   }

   // ... skipped ...

});

NOTES

The solution would be different if you allow multiple row selection.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.