OVerriding A Function

OVerriding A Function

cyber0necyber0ne Posts: 1Questions: 0Answers: 0
edited January 2010 in General
This is probably a very simple question and will show my lack of JavaScript experience...

I need to override some of the logic in the _fnFilterColumn function in DataTables (basically just adding some logic to the line that creates the regular expression). Naturally, I don't want to do this in the provided .js file since future upgrades would clobber my changes. How do I override an internal function like this from my page's JavaScript code?

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Hi cyber0ne,

    I'm sorry to say that due to how DataTables is constructed at the moment, it's not possible to override an internal function using Javascript outside of DataTables scope. This is something that will be addressed whenever I get around to doing the 2.x series (which might be a while off!) - but at present you would need to alter the core.

    Another option, however, might be to use custom filtering: http://datatables.net/development/filtering . This allows you to do whatever filtering you need, through the filtering API, in addition to the normal filtering DataTables provides (obviously that can be hidden if needed).

    Regards,
    Allan
  • stonefieldstonefield Posts: 2Questions: 0Answers: 0
    I'm writing a wrapper (plugin without changed main code) based on afnFiltering. For each sType you can write you own filter function. If you want i can share my work for public use.
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Sounds very interesting indeed! I'd be most interested in seeing what you've done (and I'm sure others would as well, given how important a topic filtering is) :-)

    Regards,
    Allan
  • KarinKarin Posts: 5Questions: 0Answers: 0
    Is there any news on this subject?

    I need a custom column filtering function based on the sType which replaces the default "string matching column filter" in _fnFilterColumn.

    I have a column in my table which requires a custom column filter which is not based on string matching or regular expression matching but has more complex operations.

    When I use the filter it will call _fnFilterComplete:
    - It first applies the global filter (_fnFilter), there are no issues here because my column is set to "bSearchable": false.

    - After that it applies the column filter (_fnFilterColumn), this is basically a string matching no matter the sType.
    (You could define a $.fn.dataTableExt.ofnSearch["myType"], which will return a string which is used to string match with the value in oSettings.aoPreSearchCols[i].sSearch)
    Because I have to save my filter for the custom filter in the oSettings.aoPreSearchCols[i].sSearch) the default filter will always fail.

    - After that it applies the custom filters (which is what I want to do on my column), however the result will never make it here since it is filtered out previously by the jQuery column filter.

    I was wondering what the possibilities are, is there something developed as described above, or is there a different solution to skip the default column filter and just apply my own column filter?


    Some notes:
    - I cannot alter the JQuery.datatable.js, (and as seen above and in other threads it is not possible to overwrite the _fnFilterColumn, this would have been a good solution to me.)
    - I have to save the custom column filter in oSettings.aoPreSearchCols[i].sSearch because the generic code I use notes that this is indeed a column filter.
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Why can't you just modify the source? That's the whole point of open source :-)

    I do plan to have a complete filtering plug-in solution at some point, but that's probably a couple of versions down the line. Until then, you can define your own, completely custom filter using the $.fn.dataTableExt.afnFiltering option: http://datatables.net/development/filtering (Custom row filters).

    Allan
  • KarinKarin Posts: 5Questions: 0Answers: 0
    Thanks for your quick reply, however I have to find a different solution :( .

    To modify the source would have been very nice indeed, however it is a common library used in many places which should be upgradable any time without extra changes (which will be forgotten and not appreciated).

    I did write my own row filter $.fn.dataTableExt.afnFiltering, however the results never arrive here since they are already removed in the default column filter using oSettings.aoPreSearchCols[i].sSearch.

    ------
    First the data is retrieved on the server-side, processed by Java code and put in the page, the code also decides if the datatable should be client- or server-sided.
    After that generic js/jQuery code in the page generates a datatable and initializes the settings and functions (slightly different depending on client- or server-side processing).

    So when the user performs an advanced search on columns, the generic search functions sees one of the fields as a column search (which it because it should only filter on that specific column) and puts the filter in aoPreSearchCols[i] here the code has no clue of server or client side processing.

    After all filter data is gathered the code checks if the datatable is processed server or client side.
    In case of server-side it provides the data in aoPreSearchCols[i] among other data to the server, there my custom filter is recognized and appied instead of a string match.
    In case of client-side it starts filtering (with _fnFilterComplete). Because my custom filter is defined in oSettings.aoPreSearchCols[i].sSearch and the default filter will use string matching instead of my own filter function this will always fail and remove my result.
    ------

    I will keep searching for a nice solution. I am not allowed to change the jQuery.datatable.js file, however I am the one to write the generic datatable (server-/client-side) code so I'll keep trying to find some other possibilities to misuse.

    Thanks, Karin
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    > I did write my own row filter $.fn.dataTableExt.afnFiltering, however the results never arrive here since they are already removed in the default column filter

    Why apply the default column filter if that isn't what you want? I don't quite understand that :-).

    When you redraw the table (fnDraw) the it will be passed through the custom filer, and you can have it use any data source you want to filter each row. You don't need to use the built in filtering.

    Allan
This discussion has been closed.