Forum Discussion

jfarmer's avatar
jfarmer
Cloud Apps
04-14-2022

How to apply javascript to all pages of a table

I have the following script that modifies the values within a couple columns in this table widget and it works just fine. The issue I am having is it only applies to the first page of the table, and as soon as I change the page the changes are no longer applied. Is there an event associated with changing pages of a table I can set up in the script to make sure it runs when pages are changed? 

Thanks in advance. 

 

 

widget.on('domready', function(se, ev) {
		
	var tableObject = document.getElementById("grid");
	for (var r = 1, n = tableObject.rows.length; r < n; r++) {
		if (tableObject.rows[r].cells[5].innerHTML == 1){
			tableObject.rows[r].cells[5].innerHTML = "True";
		}
		else{
			tableObject.rows[r].cells[5].innerHTML = "False";
		};
		if (tableObject.rows[r].cells[6].innerHTML == 1){
			tableObject.rows[r].cells[6].innerHTML = "True";
		}
		else{
			tableObject.rows[r].cells[6].innerHTML = "False";
		}
	};
});

 

 

 

3 Replies

Replies have been turned off for this discussion
    • jfarmer's avatar
      jfarmer
      Cloud Apps

      It is running on Linux, Sisense hosted. Also this is a table widget type object, not a pivot2 object if that helps. 

      • KatieG's avatar
        KatieG
        Sisense Employee

        Hi jfarmer , spent some time testing and I did not find a script solution that worked super well. 

        Some of the limitations to a script solution I found:

        • Going from a numeric data type to a string data type in a table widget (value 1 to "True") did not seem to work - only worked if the data type was the same (numeric to numeric)
        • The native widget event to put the logic behind ("processcell") does not fire for every page of results - just the first 10 or so pages

        The script that somewhat worked (but again has the limits above, I don't recommend using it as is)

        // list of column names to apply logic to
        let columnTitleList = ['Column5', 'Column6']

        widget.on('processcell', function(ev, widget){
        //console.log(widget)
        if(columnTitleList.includes(widget.item.jaql.title)){
        if(widget.cell.data === 1){
        widget.cell.data = 50000
        //console.log(widget.cell.text)
        }
        else {
        widget.cell.data = 60000
        }
        }
        })

        I think we can get the result you are looking for via a different method:

        ______________________________________________________________

        If this is a regular table:

        I recommend you add a custom column in the Elasticube model that contains the True or False logic and use those columns in the table widget as is - 

        Before model changes:

        After model changes:

        If this is a table with aggregation:

        Unless there is a specific reason to use the table w/agg, switch the type to pivot table. Then you can use this script below (uses newer Pivot 2 API):

        // List Value names
        let colList = ["Value4", "Value5"]

        widget.transformPivot(
        {
        type: ['value']
        },
        function (metadata, cell) {
        if(colList.includes(metadata.measure.title)){
        if(cell.value === "1"){
        cell.content= "True"
        }
        else {
        cell.content= "False"
        }
        }
        }
        );

        Before script:

        After script:

        Hope that helps!