Get Datatables cell value that is input text field

前端 未结 2 343
[愿得一人]
[愿得一人] 2021-01-13 06:48

I\'m generating a DataTable with a javascript data source. The data is returned from an ajax call to nodejs which queries SQL Server DB table and returns 2 columns,

相关标签:
2条回答
  • 2021-01-13 07:07

    I have a sort of hackish yet applicable and simple solution. The thing to note is my code DOES allow usage of the DataTables data() method and also that this is more of a solution for anybody receiving HTML code instead of inner text from each cell of a row, rather than trying to get the value of an input text box. The row clicked on returns an array through data(). Each cell of the row is wrapped in a span so any plaintext/non-HTML strings inside a td can be rendered as HTML elements and therefore be recognized by the jQuery text() method:

    $('#mytable tbody').on('click', 'tr', function (e) {
       var rowArray = aTable.row(this).data();
       var colAchange = $('<span/>').html(rowArray[0]).text();
       var colBchange = $('<span/>').html(rowArray[1]).text();
    });
    

    JSFiddle: https://jsfiddle.net/nyv7j6h8/

    0 讨论(0)
  • 2021-01-13 07:15

    If you just want to extract the values entered in the input boxes, you must go through jQuery or native DOM. dataTables itself is not aware of any changes made to form input fields, so trying to retrieve the values by cell().data() will never work, with or without id's / orthogonal data :

    aTable.cell(0,2).nodes().to$().find('input').val()
    aTable.cell(0,3).nodes().to$().find('input').val()   
    

    Will give you the current value of the various inputs. Have replicated your above scenario 100% in this fiddle -> http://jsfiddle.net/obmghyo7/

    This is basically also how the official documentation suggests a way to extract values from form inputs -> https://datatables.net/examples/api/form.html

    If you want to perform filtering / searches within the table, which also includes changes made by the user in form inputs, then it is a little more tricky -> JQuery Datatables search within input and select (mine, but I dont know any better answer)

    0 讨论(0)
提交回复
热议问题