0

I am new to JSF and currently trying to implement some custom functionality in a DataTables widget -- this includes custom filter functions, as well as handlers for events such as clicking on cells (say, paste their content into the global filter box), shift-clicking on column headers (copy column data to paste buffer), and a custom export function bound to a button.

I had previously added this functionality to a prototype DataTables instance by writing JS to run client-side. However, when I try to include these directly in my JSF project, I find they conflict with the JS as used by JSF.

I have put my custom javascript in files in the /resources/js subdirectory of my JSF project.

const enableTableColsCopy = (tableName) => {

    $(`#${tableName} thead`).on('click', 'th', function (e) {

        if (e.shiftKey) {
            let thisTable = $(`#${tableName}`).DataTable();
            let selColIndex = thisTable.column($(this)).index();
            let selColName = $(thisTable.column(selColIndex).header()).html();
            let tabData = tableToJSON($(`#${tableName}`));
            let selColCells = [];

            for (let idx in tabData) {
                selColCells.push(String(tabData[ idx ][ selColName ]).trim());
            }

            selColCells = `(${selColCells.join('|')})`;
            copyToClipboard(selColCells);

        }

    });

};

And within my xhtml files I include the JS like so:

<h:outputScript library="js" name="customiseTable.js"/>
<!-- ... at some point later, for the sake of example ... -->
<p:commandButton id="exampleButton" type="button" onclick="enableTableColsCopy('someTableName')" value="Enable custom copy" style="float:left" icon="pi pi-align-justify" />

Including custom JS directly leads to the application failing to load, or the appearance of error messages in the JS or GlassFish server consoles (say, jQuery is not available, then the application stops working when I manually include jquery-latest.min.js). I have validated all my JS in JSFiddle and my test environment – I am sure that the problem is not with the code itself.

My question is then, how best to customise DataTables within my application. Would I need to extend PrimeFaces, or is there a (robust) workaround to get my custom JS to the browser, as before?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Gavin Kirby
  • 43
  • 10
  • Your question is confusing. When you say "conflict" did you mean that you're having exactly the same problem as https://stackoverflow.com/q/11112058? Then just solve it as answered there. – BalusC Apr 04 '19 at 10:40
  • It is similar, but not restricted to jquery.js -- I included that for the sake of example. If I want to edit the DataTable functionality as I had done before, I would need to include some form of datatables.js, and related libraries, and it seems this is not possible using the primefaces library (since within the jar, the component has the form of java code containing precompiled blobs) -- I cannot seem to access the underlying JS directly, and I suppose that I am not meant to. This jsfiddle shows some of the things I have been testing: https://jsfiddle.net/archon88/92t7g1vc/ – Gavin Kirby Apr 04 '19 at 11:40
  • OK. Let's phrase it differently: look at generated HTML output, are the (auto)included scripts all right and in the correct order? Your problem clearly indicates it isn't. Just rewrite JSF code in such way that the (auto)included scripts in the generated HTML output are all right and in the correct order. Clues can be found in the aforelinked related question. – BalusC Apr 04 '19 at 12:13

0 Answers0