如何使用 Ajax Laravel 中的 add/include 观看次数

How to add/include views in Laravel using Ajax

我有点卡在一个地方了。我有一些小 HTML sections 的视图,合并后会给出完整的 HTML 页面。我正在尝试用 Jquery 构建一个 website builder,其中我有一个 drop event,它添加了那些特定的 views:

例如我有 HTML for slideshow:

<div id="slideshow" data-nitsid="2">
    <div class="revolution-slider">
        <ul>
            <!-- SLIDE  -->
            @foreach($contents->slider as $sliders)
                <li data-transition="{{ $sliders->transition }}" data-slotamount="{{ $sliders->slotamount }}" data-masterspeed="{{ $sliders->masterspeed }}">
                    <!-- MAIN IMAGE -->
                    <img src="{{ URL::asset($sliders->url) }}" alt="">
                </li>
            @endforeach
        </ul>
    </div>
</div>

在我的 JQuery 代码中:

nitsbuilder.dropeventhandler = function ($item, $position) {
    var nits_id = $item.data('nitsid');
    $.ajax({
        method: 'POST',
        url: dropurl,
        data: { nits_id: nits_id, _token: token},
        dataType: 'json',
        success: function (data) {
            nitsbuilder.adder($item, $position, data);
        }
    });

}

之前我在 database 中有 html codes,所以更容易拉出 html 并添加到 HTML 页面,现在我有html in views,我如何 push/include 这个 HTML codeviewajax request 以便我的 nitsbuilder.adder 函数通过我的控制器执行放置视图。

我现在的Controller是:

class DropeventController extends Controller
{
    public function htmlcode(Request $request)
    {
        $pluginid = $request['nits_id'];
        $code = Plugins::findOrFail($pluginid);
        $htmlcode = $code->code;
        return response()->json(['htmlcode' => $htmlcode]);
    }

}

请指导我。谢谢

假设您的 html 在名为 abc.blade.php 的视图文件中,您可以 return 在 json.

中从您的控制器呈现的视图
return response()->json([
'htmlcode' => View::make('abc')->render();
]);

您可以使用 \View::make

从 blade 视图轻松创建 html 字符串

例如假设您有以下文件夹结构

  • 项目
    • ...
    • 资源
    • 观看次数
      • 片段
      • 片段A
      • 片段B

您现在可以创建一个接受 "name" 参数的路由/控制器,然后执行以下操作

$name = "snippetA"; // get this from parameters

$html = \View::make("snippets.$name")->render();

您可能还需要根据您的观点添加变量

$name = "snippetA"; // get this from parameters
$errors = []; // error variable might be needed

$html = \View::make("snippets.$name", compact('errors'))->render();

然后您可以 return 这个 html 字符串

return ['html' => $html];

并从您的 ajax 完成功能访问它。

希望对您有所帮助