拉拉维尔冲突脚本,引导



在我目前的Laravel项目中,我正在使用(尝试(Bootstrap和MDBoostrap。目前,我收到错误TypeError: $(...).DataTable is not a function并且通过在线阅读此内容,这通常是由于jquery被多次调用。我相信错误与 app 有关.js但是如果我只包含 Bootstraps 对数据表所需的脚本,我会收到 $ 未定义的错误。注意:index.blade.html是从app.blade.html扩展而来的。对于 Laravel,我只是尝试使用 MDB 来创建数据表。这可能是这里的重复,但这个问题从未得到解答。我已经在这个问题上砰了一整天了,任何意见都是感激的。

应用刀片.html

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="{{asset('css/app.css')}}">
<title>Laravel Project</title>
</head>
<body>
<div class="container">
@yield('content')
</div>
<script src="{{asset('js/app.js')}}"></script>
<script>
// Material Design example
$(document).ready(function () {
$('#dtMaterialDesignExample').DataTable();
$('#dtMaterialDesignExample_wrapper').find('label').each(function () {
$(this).parent().append($(this).children());
});
$('#dtMaterialDesignExample_wrapper .dataTables_filter').find('input').each(function () {
const $this = $(this);
$this.attr("placeholder", "Search");
$this.removeClass('form-control-sm');
});
$('#dtMaterialDesignExample_wrapper .dataTables_length').addClass('d-flex flex-row');
$('#dtMaterialDesignExample_wrapper .dataTables_filter').addClass('md-form');
$('#dtMaterialDesignExample_wrapper select').removeClass('custom-select custom-select-sm form-control form-control-sm');
$('#dtMaterialDesignExample_wrapper select').addClass('mdb-select');
$('#dtMaterialDesignExample_wrapper .mdb-select').materialSelect();
$('#dtMaterialDesignExample_wrapper .dataTables_filter').find('label').remove();
});
</script>
</body>
</html>

索引刀片.html

@extends('layouts.app')
@section('content')
<h1>User Table</h1>
@if(count($users) > 0)
<table id="dtMaterialDesignExample" class="table" cellspacing="0" width="100%">
<thead>
<tr>
<th class="th-sm">ID
</th>
<th class="th-sm">Name
</th>
<th class="th-sm">Occupation
</th>
<th class="th-sm">Location
</th>
<th class="th-sm">Salary
</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->occupation}}</td>
<td>{{$user->location}}</td>
<td>{{$user->salary}}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
@endsection

用户控制器.php

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppUser;
class UsersController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
$users = User::orderBy('id', 'asc')->paginate(10);
return view('users.index',['users'=>$users]);
}

引导.js

try {
window.Popper = require('popper.js').default;
window.$ = window.jQuery = require('jquery');
require('bootstrap');
require('mdbootstrap');
} catch (e) {}

应用.js

require('./bootstrap');
var moment = require('mdbootstrap');

app.scss

// Fonts
@import url('https://fonts.googleapis.com/css?family=Nunito');
// Variables
@import 'variables';
// Bootstrap
@import '~bootstrap/scss/bootstrap';
// MDBootstrap
@import '~mdbootstrap/css/mdb.min.css';

如果这是 DataTables.net 包,我相信你必须包含JS和可能的CSS才能$('#dtMaterialDesignExample').DataTable();成功。我知道这些不是开箱即用的引导程序。我有一个使用原版Bootstrap的Laravel包,我必须包含JS和CSS来渲染数据表。

我对MDB不是很熟悉。他们可能提供CSS,但我不知道他们是否提供JS。您似乎不应该有一个重复的定义,因此您需要确保您的应用程序正在向浏览器呈现以下内容:

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css"/>
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js" type="text/javascript"></script>

这将包括 DataTable(( 函数的 JS 定义。

最新更新