0

Using jQuery, how can I populate the value of table cells using an AJAX call, when the table cell itself is being programatically generated?

I am building a page that works as follows:

  1. Perform initial AJAX call, expecting a JSON array, e.g. a list of item names
  2. AJAX callback uses jQuery.each to iterate through the array
  3. The callback for each array item adds a row to the table, with the item name
  4. There are additional cells (let's say they represent item locations) but to get the value of this, another AJAX call needs to be made (marked as * in the sample) - how might I implement this functionality?

Sample table structure:

Name       Location1  Location2
================================
Item1          *          *
Item2          *          *

Other considerations:

  • Need to cater for a dynamic number of columns, i.e. locations. This is why I
  • I know there are a large number of calls, but not concerned about that for this question
  • I am using the DataTables plugin
  • I would like to understand whether this can indeed be done without simply changing the web services backend to generate the whole table, or at least a row at a time. By performing a query per-cell I can re-use existing calls.

Note: I am adding the table rows by using the following code, which I am open to altering. Reference

$("#tableID").find("tbody").append($('<tr>')
    .append($('<td>').text(...))
    .append($('<td>').text(...))
    .append($('<td>').text(...))
);

I'm thinking I could probably generate IDs for each cell and then fire off the AJAX calls based on that, but that seems a little clunky. Is there any more elegant way to do this?

Ideally any solution would also be able to be adjusted to handle the use case of adding an extra column to the table, e.g. Location3 in the sample table above.

Community
  • 1
  • 1
invincible
  • 11
  • 2
  • @RawN Updated the question - how might I be able to implement step 4? The rest is mainly for context – invincible Apr 06 '15 at 16:16
  • If you are using dataTables plugin you don't append your own html, plugin won't recognize it is there. Use the API to add data – charlietfl Apr 06 '15 at 16:22
  • @charlietfl Point taken - I hadn't gotten to the dataTables integration yet and the typical approach would have been to generate a full row then pass it to the API. In this question I am looking for ideas on how to manage all the per-cell callbacks – invincible Apr 06 '15 at 16:26
  • Why can't you add the data to plugin api in callback of second request? – charlietfl Apr 06 '15 at 16:29

1 Answers1

0
var nEditing = null;
 $('#query_for_search_result_table_new').click(function (e) {
            e.preventDefault();
            var aiNew = oTable.fnAddData(['', '', '', '<a class="edit" href="">Edit</a>', '<a class="cancel" data-mode="new" href="">Cancel</a>', '']);
            var nRow = oTable.fnGetNodes(aiNew[0]);
            editRow(oTable, nRow);
            nEditing = nRow;
        });

function editRow(oTable, nRow) {

            var aData = oTable.fnGetData(nRow);
            var jqTds = $('>td', nRow);
            jqTds[0].innerHTML = $("#dvFieldLabel").html();
            jqTds[1].innerHTML = $("#dvFieldOperator").html();
            jqTds[2].innerHTML = '<input type="text" class="m-wrap small" value="' + aData[2] + '">';
            jqTds[3].innerHTML = '<a class="edit" href="">Save</a> &nbsp; <a data-mode="new" class="cancel" href="">Cancel</a>';
            jqTds[4].innerHTML = '<label>' + aData[0] + ' ' + aData[1] + ' ' + aData[2] + '</label>';



        }
Nimesh Gami
  • 361
  • 1
  • 2
  • 18