Laravel-Backpack / community-forum

A workspace to discuss improvement and feature ideas, before they're actually implemented.
28 stars 0 forks source link

Blade file for only widget HTML #643

Closed sharat-rhinofish closed 1 year ago

sharat-rhinofish commented 1 year ago

Hello,

I want to output a line chart using Ajax. I know you can get the data of the chart using Ajax. I am using Laravel charts and Chartjs. However, the Ajax data does not allow the labels of the x-axis to be changed. The x-axis labels are fixed in the setup function (Strangely!)

As a workaround, I want to use Ajax and get only the chart contents as an html field in a JSON response. To do this, I am trying to only output the Widget HTML with the following in my blade.php file.


@php Widget::add([ 'type' => 'chart', 'controller' => \App\Http\Controllers\Admin\Charts\OrderVolumeChartController::class, 'class' => 'card', 'wrapper' => ['class'=> 'col-md-12', 'id' => 'order-volume-chart-id'], ]); @endphp

@include(backpack_view('inc.widgets'), [ 'widgets' => app('widgets')->where('section', 'before_content')->toArray() ])


This does not extend a Layout. This is however not working. How do I just get the chart HTML to be output without the left nav bar, so I can pick the chart contents using the ID (order-volume-chart-id) and then update my chart.

Thanks,

Sharat

tabacitu commented 1 year ago

Hi @sharat-rhinofish ,

You can create a route and controller to reply whatever content you want. Including a Backpack widget. But that will not work. The widget doesn't only consist of HTML. It also consists of JS. And you can't load that using Ajax and expect it to work, unfortunately.

I don't see how you can achieve what you are trying, unfortunately. Instead of using a Backpack widget, try to code a custom solution, that'll be much simpler. Or use a different library like this one that provides charts, that might be more suitable for your use case.

Sorry I couldn't be of more help. Feel free to reply if this isn't helpful. Or if you find a solution you like and you'd like to share it with others who encounter the same problem.

Cheers!