How can I change the initial query, based on a query string/parameter?

How can I change the initial query, based on a query string/parameter?

ibdfariaibdfaria Posts: 15Questions: 4Answers: 0

I am using js to retrieve the query string. I want to modify the initial request before the initial data is loaded. I tried doing everything on initComplete, which is a little too late, but I cannot even get the table to redraw at this point.

I have a dropdown menu with authors, and I am try to select the author based on the query string passed:

i tried this:

initComplete: function () {

var author = "My Author Name";
$('#authors-filter').val(author);
table.column(2).search(author).draw();

}

Technically this should change the dropdown menu to the correct author, and the second line should redraw the table. However, it seems like "draw()" is not doing anything.

Any ideas on what the issue is?

Answers

  • ibdfariaibdfaria Posts: 15Questions: 4Answers: 0
    edited November 2015

    Here a couple of things I have also tried;

    Setting the values I want in the column definition:

    { "data": "author", "orderable": false,"search":{"value":"My Author Name"}}
    

    That doesn't work

    Setting the values I want by modifying the ajax data:

    data: {
        "action":"load_posts",
        "pt":post_type,
        "columns":{2:{"search":{"value":"My Author Name"}}}
    },
    

    This partially works. The filter works but it also overrides all other columns. So what I need is to modify an existing data value, instead of overwrite it.

    PS: I finally figured out how to use the code blocks.

  • ibdfariaibdfaria Posts: 15Questions: 4Answers: 0

    I don't know if this is the proper solution but I found that I can update the initial query by changing the ajax data using a function:

    "ajax":{
        data: function(d){
           d.columns[2].search.value = get_author;
        }
    }
    
  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin

    Hi,

    If you want to define an initial column filter for the table, the searchCols can be used for that.

    However, ajax.data can also be used - although you'll always be setting column index 2 search to get_author that way.

    Allan

  • ibdfariaibdfaria Posts: 15Questions: 4Answers: 0

    I don't mind setting the column index in this case. You don't set the index with searchCols but you have to declare null for the other cols. So either way if the column changes the js code would need to change too.

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin

    Yes - as I was typing my message above I was thinking "I really must change this for the next major version"....

    Allan

This discussion has been closed.