brian4342
brian4342

Reputation: 1253

How to get information from datatable - javascript - MVC

I have created an ASP.net MVC app and I have created a DataTable [DataTable.net] as follows:

<table id="invoiceTable">
    <thead>
        <tr>
            <th>Invoice ID</th>
            <th>Date</th>
            <th>Reciept Date</th>
            <th>Category</th>
            <th>Total Value</th>
            <th>Invoice Ref</th>
            <th>Client</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>
        @{
            foreach (FreeAgentApp.Models.CustomInvoice _invoice in ViewBag.Invoices)
            {
                <tr>
                    <td>@_invoice.InvoiceId</td>
                    <td>@_invoice.Date</td>
                    <td>@_invoice.RecpDate</td>
                    <td>@_invoice.Category</td>
                    <td>@_invoice.TotalValue</td>
                    <td>@_invoice.InvoiceRef</td>
                    <td>@_invoice.Client</td>
                    <td>@_invoice.Status</td>
                </tr>
            }
        }
    </tbody>
</table>

And i can get the information from a row when its selected using javascript as follows:

// Row data
    $(document).ready(function () {
        oTable = $('#invoiceTable').dataTable();

        oTable.$('tr').click(function () {
            var data = oTable.fnGetData(this);
            alert(data);
            // ... do something with the array / object of data for the row
        });
    });

The variable data will provide a string of every value in the row separated by a comma as follows:

"000,26-01-14,27-01-14,001,1000,inv,something ltd,paid"

I want to have all these values separated. Note this could be done by splitting on the comma however a value in the table could contain commas.

How can I separate this string?

Upvotes: 0

Views: 92

Answers (1)

Jon Koops
Jon Koops

Reputation: 9261

According to the DataTables documentation oTable.fnGetData(this); return an array filled with the data of the definitions in the row so you should be able to acces the data directly from data.

var invoiceId = data[0];
var date = data[1];
var recpDate = data[2];

// etc. etc.

Upvotes: 1

Related Questions