bind data client side before render (using data passed from server)

bind data client side before render (using data passed from server)

markzzzmarkzzz Posts: 49Questions: 8Answers: 1

Hi,

i've lots of records returned from server, and some data is replicate for each row.
So, I'd like to create a dictionary server side, pass with the table "data" JSON, and bind each ID (for some field) with this dictionary.

What's the best way to do it? createdRow?
Not sure which is the value to pass to it:

dtGrid = $("#grid").DataTable({
    ajax: {
        url: "@(Url.Action("GetWorktypes", "Worktypes"))"
    },
    columns: [
        { data: "ID", title: "", visible: false, searchable: false },
        { data: "ClinicID", title: "", visible: false, searchable: false },
        { data: "MedicalID", title: "", visible: false, searchable: false },
        { data: "PatientID", title: "", visible: false, searchable: false }
    ],
    createdRow: function (row, data, dataIndex) {
        // here?
    }
});

data contains the row, and I'll return a proper patientsDictionary dictionary so I can bind PatientID with Name/Surname with dictionary...

This way doesn't works:

createdRow: function (row, data, dataIndex, patientsDictionary) {
    // dunno is correct
}

Any clues? Thanks

Replies

  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin

    Hi,

    Thanks for your question, but I'm not entirely sure I understand. If you want to create a server-side dictionary to do row replication, I'm not clear on why you would use anything on the client-side for it?

    Allan

  • markzzzmarkzzz Posts: 49Questions: 8Answers: 1

    Because I want to build the value of Dictionary on Client side.
    Example:

    1 = Bill Gates
    2 = Steve Jobs
    ...

    I will pass for each row just the ID of the person, plus a single dictionary.
    Than, client side, I want to associate the id to the dictionary.

    In this way, for each person (let say 17k records) I just use an integer, saving data from the json returned from server.

    Is it more clear?

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    It's best to use colums.render for that - you can change what goes into the cell. It might be worth looking at serverSide - see examples here - as only a single page of data is sent to the client.

    Cheers,

    Colin

  • markzzzmarkzzz Posts: 49Questions: 8Answers: 1

    @colin: but how can I access custom data (from json) from that function?
    Any example?

    Thanks

  • kthorngrenkthorngren Posts: 20,150Questions: 26Answers: 4,736

    One option is to use ajax.dataSrc as a function. In the function place the custom data into a global variable. In columns.render use that varaible how you like.

    Kevin

  • colincolin Posts: 15,118Questions: 1Answers: 2,583

    Another option is to call ajax.json() - this is the last data returned.

    Colin

  • markzzzmarkzzz Posts: 49Questions: 8Answers: 1

    ajax.dataSrc seems to works perfect! thanks dudes

This discussion has been closed.