简体   繁体   中英

DataTables search and filter dynamically added rows that contain input fields

http://jsfiddle.net/v7rjqogy/15/

Two things are wrong with it. First:

When I add a new line and then fill in some text into the input how do I get this to work with the search function?

Secondly (this one is more important)

You'll notice that I have added a custom Filter by: Strength: and By Dose drop downs at the top of the page. These correspond with the columns Strength and Dose in the table. I have added code to get it to search by these but it always show no rows returned.

$(document).ready(function () {

    $.fn.dataTableExt.ofnSearch["html-input"] = function (value) {
        return $(value).val();
    };

    var table = $("#lines").DataTable({
        "searching": true,
        "ordering": true,
        columnDefs: [
            { "targets": [15, 16], "searchable": false, "orderable": false },
            { "type": "html-input", "targets": [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14] }
        ],

    });

    $("#lines td input").on("change", function () {
        var td = $(this).parent();

        console.log($(this).parent().find("input").val());
        table.row($(this).closest("tr")).invalidate();
    });

    // Apply the search
    $("#strength_search").on("change", function () {
        table
            .columns(6)
            .search(this.value)
            .draw();
    });
    $("#dose_search").on("change", function () {
        table
            .columns(7)
            .search(this.value)
            .draw();
    });

});

Too much HTML to paste in here. The fiddle will show you all.

create something like this below the main table:

 <table>
    <tr><th>Brand Name</th></tr>
    <tr><td><input id="abrand_name0" name="brand_name[]" value="" size="15" type="text"></td></tr>
        <tr><td><button id="insert" onclick="">Insert line</button></td><tr>
</table>

and add row:

var oTable =  $('#lines').dataTable();
    oTable.fnAddData( [
        "<input name='brand_name[]' id='brand_name" + servln + "' size='15' value="+$('#abrand_name0').val()+ ">"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM