【问题标题】:Unable to display data using jQuery datatables, AJAX and JSON无法使用 jQuery 数据表、AJAX 和 JSON 显示数据
【发布时间】:2015-11-30 23:07:59
【问题描述】:

我在使用 AJAX 在我的 jQuery DataTable 上显示我的数据时遇到问题。我正在使用来自 datatables.net 的库。我尝试将 JSON 打包成许多不同的格式,但没有任何效果。我还弄乱了“列”部分,交换了“标题”和“数据”。我现在只有一个要显示的事件,但表格底部显示了一些疯狂的东西,比如 4,000 个条目。这是我的代码:

<script src="//cdn.datatables.net/1.10.10/js/jquery.dataTables.js"></script>
<script type="text/javascript">   
    $(document).ready(function () {
                $('#myTable').DataTable({
                    "processing": true,
                    "ajax": {
                        "url": "/api/EventsApi/GetAll",
                        "dataSrc": ""
                    },
                    columns: [
                        { title: "Title" },
                        { title: "Template" },
                        { title: "Capacity" },
                        { title: "Boarding Location" },
                        { title: "Status" },
                        { title: "Edit / Delete" }
                        //{ "data": "title" },
                        //{ "data": "eventTemplateID" },
                        //{ "data": "locomotive.capacity" },
                        //{ "data": "boardingLocationStart.city" },
                        //{ "data": "status" },
                        //{ "data": "status" }
                    ]
                });
    });

    <div class="title-content">@ViewBag.Title</div>
        <div id="dataTable">
            <table id="myTable" class="table table-hover" style="text-align: center;">
                <tbody id="tBody">
                    <!-- Table body data goes here -->
                </tbody>
            </table>
        </div>

这是从 AJAX 调用返回的 JSON:

{"data":[{"tripEventID":1,"extraDetails":"this train has special details","eventName":"ZombieTrainEventName ","departureDate":"\/Date(1443715200000)\/","returnDate":"\/Date(1443718800000)\/","eventCapacityOveride":100,"eventTemplateID":3,"title":"The Zombie Train ","companyID":1,"description":"description of zombie train ride ","boardingClosed":30,"status":1,"boardingLocationStart":{"boardingLocationID":3,"companyID":1,"name":"Skunk Train Willits","streetAddress":"Willits somewhere","city":"Some city","state":"CA","zip":"95713","description":"Desc field1"},"boardingLocationStartTo":{"boardingLocationID":3,"companyID":1,"name":"Skunk Train Willits","streetAddress":"Willits somewhere","city":"Some city","state":"CA","zip":"95713","description":"Desc field1"},"boardingLocationReturnFrom":{"boardingLocationID":3,"companyID":1,"name":"Skunk Train Willits","streetAddress":"Willits somewhere","city":"Some city","state":"CA","zip":"95713","description":"Desc field1"},"boardingLocationReturnTo":{"boardingLocationID":3,"companyID":1,"name":"Skunk Train Willits","streetAddress":"Willits somewhere","city":"Some city","state":"CA","zip":"95713","description":"Desc field1"},"allowFlexableReturnDate":false,"product":[],"productBundle":[{"bundleID":10,"companyID":1,"displayName":" Pumkin Bundle copy Test","price":0.0100,"tax":0.0200,"productList":[]}],"locomotive":{"trainID":1,"companyID":1,"title":"Skunk_Steam ","type":1,"description":"Steam locomotive ","capacity":998,"status":0},"media":{"mediaID":1,"companyID":1,"hero":[],"gallery":[{"mediaDetailID":6,"formatTypeID":2,"fileName":"testimage6.txt","order":1,"path":null,"url":null},{"mediaDetailID":7,"formatTypeID":2,"fileName":"testimage6.txt","order":1,"path":"path6","url":"url6"},{"mediaDetailID":8,"formatTypeID":2,"fileName":"testimage7.txt","order":1,"path":"path7","url":"url7"}],"inside":[{"mediaDetailID":1,"formatTypeID":1,"fileName":"testimage.txt","order":1,"path":null,"url":null},{"mediaDetailID":2,"formatTypeID":1,"fileName":"testimage2.txt","order":1,"path":null,"url":null},{"mediaDetailID":3,"formatTypeID":1,"fileName":"testimage3.txt","order":1,"path":null,"url":null}]},"duration":15,"isExclusive":false,"timeAtDestination":45,"isOneWay":false}]}

就像我说的,我尝试将 JSON 重新打包为嵌套对象和数组,但没有任何效果。我错过了一些明显的东西吗?感谢您的帮助,谢谢!

【问题讨论】:

    标签: javascript jquery json ajax datatables


    【解决方案1】:

    您必须像这样的 json 索引键命名 js 中的列:

    $(document).ready(function() {
        $('#myTable').DataTable( {
            "ajax":  "path/to/your/file.json",
            "columns": [
                { "data": "title" },
                { "data": "eventTemplateID" },
                { "data": "eventCapacityOveride" },
                { "data": "boardingLocationStart.streetAddress" },
                { "data": "status" },
                { "data": null }
            ],
            "columnDefs": [ {
                "targets": -1,
                "data": null,
                "defaultContent": "<button>Click!</button>"
            } ]
        } );
    } );
    

    请注意,您可以使用columnDefs 选项在表格中定义自定义数据。

    然后用这样的东西编辑你的 HTML:

     <table id="myTable" class="table table-hover" style="text-align: center;">
        <thead>
            <tr>
                <th>Title</th>
                <th>Template</th>
                <th>Capacity</th>
                <th>Boarding location</th>
                <th>Status</th>
                <th>Edit / Delete</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
               <th>Title</th>
                <th>Template</th>
                <th>Capacity</th>
                <th>Boarding location</th>
                <th>Status</th>
                <th>Edit / Delete</th>
            </tr>
        </tfoot>
    </table>
    

    在这里你可以找到一个工作的fiddle

    【讨论】:

    • 您好,感谢您快速详细的回复。我进行了更改,我看到它正在使用小提琴,但由于某种原因,我仍然无法让我的代码正常工作。唯一填充的是所有“点击!”的最后一列。纽扣。页面加载大约需要 10 秒,然后它会发出警报“DataTables 警告:表 id=mytable - 请求第 0 行第 0 列的未知参数 'title'。有关此错误的更多信息,请参阅datatables.net/tn/4” -谢谢
    • 这意味着脚本在json中找不到标题键来绑定到第0列。您确定您的代码正确加载了json吗?看看 chrome 控制台是否抛出了一些错误(或者如果你愿意,也可以使用 firebug)。你在网络服务器上吗?本地使用无法正常工作。您传递给 dtatables 的整个 json 是有效的 json?
    • 或者,如果您想从外部 url 加载 json,则必须配置托管 json 的服务器以允许跨域访问控制
    • 好的。我认为问题可能是我正在返回一个字符串,其中包含 JSON。也许如果我可以反向字符串化它并摆脱使它成为字符串的引号。稍后我将不得不玩它
    • “你必须给列命名”。这一点怎么强调都不过分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    相关资源
    最近更新 更多