0

I have created a datatable, I want to add filter option to url, here is my code

jQuery(document).ready(function() {
 oTable = jQuery('#vV7SyzDr').dataTable({
    "sPaginationType": "full_numbers",
    "bProcessing": true,
    "sAjaxSource": "my_url",
    "bServerSide": true,
    "fnServerParams":function ( aoData ) {
        $("select[name=label]").change(function () {
           var labels = $("select[name=label]").val();
           aoData.push({"labels": labels } ); 
        });       
     },
  });

  $("select[name=label]").change(function () {
      oTable.fnDraw();
    });
  });

But the url is not triggered with parameter labels. I have a multiple select box to filter the table.

<select class="tagit-hiddenSelect" name="label" multiple="multiple">
    <option selected="selected" value="Important">Important</option>
    <option selected="selected" value="Bug">Bug</option>
</select> 

I am trying to add the selected labels to my_url in sAjaxSource, and backed I should get labels and return json response, That will render in my datatable with new data from backend.

Now My url is

http://my_domain/filter?sEcho=1&iColumns=7&sColumns=%2C%2C%2C%2C%2C%2C&iDisplayStart=0&iDisplayLength=10&mDataProp_0=0&sSearch_0=&bRegex_0=false&bSearchable_0=true&bSortable_0=true&mDataProp_1=1

I want to add parameter label to my url. Ideally it should be

http://my_domain/filter?sEcho=1&iColumns=7&sColumns=%2C%2C%2C%2C%2C%2C&iDisplayStart=0&iDisplayLength=10&mDataProp_0=0&sSearch_0=&bRegex_0=false&bSearchable_0=true&bSortable_0=true&mDataProp_1=1&label=['important','bug'] (['important','bug'] means value in array type).

Please help me, I am a beginner in front end

5
  • 3
    Please, by respect for people who will try to help you, can you format properly your code ? Commented Oct 12, 2016 at 7:06
  • 2
    Can you better explain what you are trying to do? Its not clear what your doing or what your issue is Commented Oct 12, 2016 at 7:12
  • This still doesnt make a lot of sense. What do you mean by add filter option to url ? What url? What is a "filter option"? What do all of the calls to $("td", nRow).css() have to do with your question? Commented Oct 12, 2016 at 8:07
  • You might be looking for columns.render() which will let you read the incomming data from the server and render it however you like Commented Oct 12, 2016 at 8:10
  • chi stamu passannu.. Commented Oct 12, 2016 at 10:14

1 Answer 1

2

I think I understand what you're trying to do, and you've almost got it right - it looks like you've just got the change event in the wrong place. Your datatables initialisation code should look like this:

oTable = jQuery('#vV7SyzDr').dataTable({
    "sPaginationType": "full_numbers",
    "bProcessing": true,
    "sAjaxSource": "my_url",
    "bServerSide": true,
    "fnServerParams": function(aoData) {
        aoData.push({"labels": $("select[name=label]").val() });
      });
    }
  });

Note: All I have done is remove the change event binding.

Now you need to bind the datatables fnDraw to the selectlist change event. This is not part of the datatables init code, so put it elsewhere in the js :

$("select[name=label]").change(function() {
    oTable.fnDraw();
});

fnDraw will redraw the datatable by calling your ajax service, passing the additional parameter specified in fnServerParams.

Sign up to request clarification or add additional context in comments.

5 Comments

I have tried your answer, but not working(even didn't draw the table). I have edited the question. Please have a look
Yes I am getting an error: Cannot read property 'match' of undefined
Is there some code that you're not showing us? how are you binding the columns?
Here I am using a package (chumber datatable) for laravel (php framework) . I have update my code and the error has gone.
But still parameter and value is not added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.