Jquery DataTable重绘表并将其他参数传递给ajax

时间:2014-02-04 15:39:22

标签: jquery ajax jquery-datatables redraw

我尝试为jquery Datatable构建自定义过滤器。我有一个表格有3个选择组合,并在表单提交我想传递给DatTable对象额外的过滤器参数并重绘我的表,但因为它似乎不是最好的方法我试试。我的代码

//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();
});

但变量值充当HTML对象

我使用php动态生成标记,例如

<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>

1 个答案:

答案 0 :(得分:0)

为什么不在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});          
    }
}

另外,你的意思是“充当HTML对象”?如果你做了

会发生什么
console.log($("#filter_table :input[name='brand']").val())

来自fnServerParams?