Skip Navigation

[Resolved] Use a View’s ShortCode in a JS Code

This thread is resolved. Here is a description of the problem and solution.

Problem

Can I use a View in my JS code, by adding the View's shortcode where the JS expects some results? The View returns the results the code expects.

Solution

Generally, ShortCodes can not be added to JS.

You could add the JS within script tags to your HTML section of a content Template for example.

Then you could apply this custom code here to retrieve a "raw" Views' loop result. This can then eventually be used to populate your Custom JS.

This is however not suggetsed, n or supported. It is a full custom solution - and not agreeing with Toolset Views' major purpose, which is to display - not deliver data.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 4 replies, has 2 voices.

Last updated by mohammadD 3 years, 6 months ago.

Assigned support staff: Beda.

Author
Posts
#623840

i am trying to load a view by shortcode parameter chosen in the page via js . the code looks like this

<script>
  var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: [[wpv-view name="ptpdates-raw" pol=$('#pol').val() pod=$('#pod').val() cnt=$('#cnt').val()]],
        datasets: [{
            label: 'Price in USD: ',
            data: [[wpv-view name="ptprates-raw" pol=$('#pod').val() pod=$('#pod').val() cnt=$('#cnt').val()]],
  			fill : 'false'
        }]
    },
    options: {
    }
});
</script>

but the shortcode returns the same things cause it should be rendered in php side.
is there anyrest api for the actual result of views , so i could get those contents with js ?

#623943

This is not the purpose of Views, and we do not support this.

The Toolset Views loop always comes "packed" in a standard HTML - which is what destroys your JS variable here.

The JS expects a clean value set, usually in some particular format, and cannot handle add-on HTML or text/characters that are not part of the planned syntax.

As said we do not officially support or suggest this for several reasons, mainly because Views is not a data Provider.

If you require raw data, it is better to query the Database and build the exact syntax directly using the WP_Query to get the data and PHP or other custom code to produce the output.

Views are just a "wrapper" for the WP_Query, and mainly it adds a GUI, where you control the WP_Query, and it's easy to produce a visually pleasing output.
But it's not built to replace the WP_Query or make custom data structure outputs.

If you still want to proceed with Views in this particular aspect of your project, you can apply the custom code described here:
https://toolset.com/forums/topic/how-to-display-view-results-inline-or-specify-div-class/#post-350852

Please acknowledge that we cannot support follow up issues of that code since it is warned that several things in Views may break if used.

The usage of ShortCodes the way you use it is as well not allowed by WordPress, and the only reason this works (partially) with Toolset is, that we added some code to make it possible.
But, this does not mean that you can add JS to Views as attributes, or use Views in JS variables.
It is only intended to allow the attributes usage with other ShortCodes, or HTML parts as separators, and similar things.

There is as well a feature request filed, where it's asked to allow Views "Raw" outputs, hence, make Views a data provider.
This is not yet in development and will depend on the Developers decisions.

#623947

according to what you said , i changed my idea and i am going to use ajax and custom search of views to do what i want, but the problem is when i try to do it , ajax doesn't load anything. you can check it in the link below :
hidden link
i have looked into it , and there was no issue with thirdparty plugins and theme ( deactivated them and tested it again.)
i am using the last beta version.
what should i do ?

#623951

Well, the AJAX works on that page.
It does load results but does not display them.

This may be due to several reasons, which are out of the topic of this ticket.

Please open a new ticket with the AJAX issue if after the below steps it still does not work:

- Create a View
- Add Custom Search Filters and set to filter by AJAX
- Complete the Loop with the Wizard to produce a simple numbered list
- Insert the View into a page and test it.

Please make sure to have no custom code, as well no Custom CSS or JS, applied to this View.

As well, I would not suggest using the Beta yet for active development but for tests only.
This is not yet a stable version, and we are eager to analyse each problem coming up (like this one, if it persists).
But we cannot grant that we can fix the issues just yet in the very beta state.

This is why I recommend the Beta only for test sites or test development just yet.

If the problem persists after above steps, please can you report this on a new ticket soi can help you there?

Thank you!

#623952

Thanks ok i will create a new ticket. i will change to product version as well too.