Filter table by field come from JSON

Filter table by field come from JSON

hviegashviegas Posts: 11Questions: 1Answers: 0
edited April 2022 in Free community support

Hello everybody,

I have extensively searched and cannot find the answer, I intend to filter the table with a field from the HTML page.
Below my code and I intend to filter by the field "Owner.Own_Id" where I have in the code (search:"1").

Code:

function loadDataTable() {
     dataTable = $('#tblData').DataTable({
         "ajax": {
             "url": "/Admin/Property/GetOwners",
             type: "GET",
             contentType: "application/json",
             dataType: "json",
             success: function(date) {
                 alert(data.d.date)
             }
         },
         pageLength: 10,
         searchCols: [null, { search: "1" , "regex": true }, null, null, null, null, null, null],
         lengthMenu: [2, 5, 10, 20, 50, 100, 200, 500],
         "columns": [
             { "data": "prop_Id", "width": "3%" },
             { "data": "owner.own_Id", "width": "3%" },

Thanks

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Replies

  • rf1234rf1234 Posts: 2,950Questions: 87Answers: 416

    I intend to filter the table with a field from the HTML page.

    I assume you mean a field from your data table.

    You are not saying from which table row your search field should be coming from. I assume it is from the selected row (if any) in case you are using the "select" extension.

    yourTable
        .on ( 'select', function (e, dt, type, indexes ) {
             var selected = dt.row( {selected: true} );
             if (selected.any()) {
                 dt.search( selected.data().owner.own_id ).draw();
             }
        })
    

    Use Markdown please (see below).

  • hviegashviegas Posts: 11Questions: 1Answers: 0
    edited April 2022

    thank you very much for the support,

    I ended up using another solution, in the future I will filter directly in JSON.

    Below is the solution used:

    const ownerID = document.getElementById('Owner_Own_Id').value
    
    function loadDataTable() {
        dataTable = $('#tblData').DataTable({
            "ajax": {
                "url": "/Admin/Property/GetOwners",
                type: "GET",
                contentType: "application/json",
                dataType: "json",
                sucess: function (data) {
                    alert(data.d.data)
                }
            },
            pageLength: 10,
            searchCols: [null, { search: ownerID , "regex": true }, null, null, null, null, null, null],
    

    Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Sign In or Register to comment.