Please help on java script error.

Please help on java script error.

gsurgegsurge Posts: 5Questions: 0Answers: 0
edited January 2012 in DataTables 1.8
I am getting the error "DataTables warning (table id = 'searchResults'): Requested unknown parameter '1' from the data source for row 0"
when the Data-table doesn't have any record to display.

here is part of the script:


$(document).ready(function() {
$('#searchResults').dataTable({
"aoColumnDefs": [
{ "sType": "date", "aTargets": [3] },
{ "sType": "html", "aTargets": [4] },
{ "sClass": "centered-alignment", "aTargets": ["_all"] }
],
"aaSorting": [[3, 'desc']],
"sPaginationType": "full_numbers"
});
});


....

@Html.Grid(Model.Transactions).Columns(column => {
column.For(t => t.BusinessEntityName);
column.For(t => t.SccId).Named("XX ID");
column.For(t => t.DisplayType).Named("Type");
column.For(t => t.Date);
column.For(x => Html.Partial("_TransactionLink", x)).Named("Confirmation Number / DCN");

}).Attributes(id => "searchResults")


help please.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Can you link to the page please? Also I presume that the generated table creates a TBODY and THEAD element as DataTables requires: http://datatables.net/usage

    Allan
  • gsurgegsurge Posts: 5Questions: 0Answers: 0
    Allan, how to add some javascript to make sure the table doesn't try to work on null data?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    DataTables will work okay with null data given as the data for a cell (using Ajax or JSON). So I'm afraid I would need more details to be able to offer any help, ideally a link :-)

    Allan
  • gsurgegsurge Posts: 5Questions: 0Answers: 0
    Allen, here are the pages:

    .chtml:

    @using MvcContrib.UI.Grid
    @using Web.UI.Areas.Accounts.Models
    @model UserTransactionsViewModel
    @{
    ViewBag.Title = "User Transactions History";
    }
    @section TitleContent {
    User Transaction History
    }
    @section titleTextContent{
    User Transaction History



    }
    @section ScreenIdContent{
    e1200
    }

    #searchResults
    {
    width: 100%;
    float: left;
    }
    #container
    {
    width: 600px;
    }
    #UserInfo
    {
    margin-top: 2em;
    margin-bottom: 2em;
    }
    .centered-alignment {
    text-align: center;
    }


    $(document).ready(function () {
    $('#searchResults').dataTable({
    "aoColumnDefs": [
    { "sType": "date", "aTargets": [3] },
    { "sType": "html", "aTargets": [4] },
    { "sClass": "centered-alignment", "aTargets": ["_all"]}
    ],
    "aaSorting": [[3, 'desc']],
    "sPaginationType": "full_numbers"
    });
    });





    @Html.Grid(Model.Transactions).Columns(column => {
    column.For(t => t.BusinessEntityName);
    column.For(t => t.Id).Named("ID");
    column.For(t => t.DisplayType).Named("Type");
    column.For(t => t.Date);
    column.For(x => Html.Partial("_TransactionLink", x)).Named("Confirmation Number / DCN");

    }).Attributes(id => "searchResults")

    ---------------------------------------------------------------------------
    model:

    using System.Collections.Generic;
    using Web.UI.Dtos;

    namespace Web.UI.Areas.Accounts.Models
    {
    public class UserTransactionsViewModel
    {
    public IList Transactions { get; set; }
    }
    }

    ---------------------------------------------------------------------------------
    controller:

    using System.Linq;
    using System.Web.Mvc;
    using Core;
    using Core.BusinessEntities;
    using NHibernate;
    using NHibernate.Linq;
    using Web.UI.Areas.Accounts.Models;
    using Web.UI.Dtos;
    using Web.UI.Helpers;

    namespace Web.UI.Areas.Accounts.Controllers
    {
    [Authorize]
    public class HistoryController : BaseController
    {
    private readonly ISession _session;
    private readonly IProvideUserContext _userContext;

    public HistoryController(ISession session, IProvideUserContext userContext)
    {
    _session = session;
    _userContext = userContext;
    }

    public ActionResult Transactions()
    {
    var transactions = (from t in _session.Query().Where(x => x.UserId == _userContext.CurrentUserId).ToList()
    select new TransactionHistoryDto
    {
    BusinessEntityName = t.EntityName,
    Id = t.Id,
    DisplayType = t.DisplayType,
    Type = t.Type,
    Date = t.TransactionDate,
    ConfirmationNumber = t.DCN == null ? t.ArNumber.ToString() : t.DCN.ToString()
    }).OrderByDescending(x => x.Date);
    var model = new UserTransactionsViewModel
    {
    Transactions = transactions.ToList()
    };
    return View(model);
    }
    }
    }
    -------------------------------
    when i click on transaction history link i found the error :
    "DataTables warning (table id = 'searchResults'): Requested unknown parameter '1' from the data source for row 0"
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    What does the rendered HTML on the page look like (i.e. view source)?

    Allan
  • gsurgegsurge Posts: 5Questions: 0Answers: 0
    edited January 2012
    ...


    $(document).ready(function () {

    $('#searchResults').dataTable({

    "aoColumnDefs": [

    { "sType": "date", "aTargets": [3] },

    { "sType": "html", "aTargets": [4] },

    { "sClass": "centered-alignment", "aTargets": ["_all"]}

    ],

    "aaSorting": [[3, 'desc']],

    "sPaginationType": "full_numbers"

    });

    });










    There is no data available.




    ...
  • gsurgegsurge Posts: 5Questions: 0Answers: 0
    Allen, congrats,
    this code has solved the problem:



    $(document).ready(function () {
    if (!$('#searchResults tr td:first').html().match('There is no data available')) {

    $('#searchResults').dataTable({
    "aoColumnDefs": [
    { "sType": "date", "aTargets": [3] },
    { "sType": "html", "aTargets": [4] },
    { "sClass": "centered-alignment", "aTargets": ["_all"] }
    ],
    "aaSorting": [[3, 'desc']],
    "sPaginationType": "full_numbers"
    });
    } else {
    $('#searchResults td').addClass('centered-alignment');
    }

    });


    Thank you so much for your time.
This discussion has been closed.