0

I try to build a custom filter for jquery Datatable. I have a form which has 3 select combos and on form submit I would like to pass to DatTable Object additional filter params and redraw my table but as it seems is not best approach what I'm tryin. My code

//here I build the table 
oTable = $('#my_table').dataTable({  
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": url
    .
    .
    .
    "fnServerParams": function (aoData) {             
        if(brand !=''){  
            aoData.push({ "name": "brand", "value": brand});              
        } else if(shop !=''){
          aoData.push({"name": "shop", "value": shop});          
        } 
    }
})


//here I try to pass variable to table Object

$('#filter_table').on('submit', function(e){
    e.preventDefault();
    brand    = $("#filter_table :input[name='brand']").val();
    shop     = $("#filter_table :input[name='shop']").val();
    prodcat = $("#filter_table :input[name='prod_cat']").val();
    console.log(brand);   
    oTable.fnDraw();
});

But the variable value acts as HTML Object

I generate the markup dynamically with php as for example

<div class="form-group">
    <label>Brand</label>
    <select name ="brand" id ="brand" class="form-control">                   
        <?php foreach ($filters['brands'] as $brand) : ?>
            <option value="<?= $brand['brand'] ?>"><?= $brand['brand'] ?></option>
        <?php endforeach; ?>
    </select>
</div>
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
fefe
  • 8,755
  • 27
  • 104
  • 180

1 Answers1

0

why don't you define those variables in fnServerParams?

"fnServerParams": function (aoData) {
    var brand = $("#filter_table :input[name='brand']").val();
    var shop = $("#filter_table :input[name='shop']").val();
    if(brand !=''){  
        aoData.push({ "name": "brand", "value": brand});              
    } else if(shop !=''){
        aoData.push({"name": "shop", "value": shop});          
    }
}

Also, what do you mean "acts as HTML object"? What happens if you do a

console.log($("#filter_table :input[name='brand']").val())

from inside fnServerParams?

Jakotheshadows
  • 1,485
  • 2
  • 13
  • 24