Image insertion on fnCreatedRow

Image insertion on fnCreatedRow

maksbolmaksbol Posts: 9Questions: 0Answers: 0
edited September 2012 in General
This is my fnCreatedRow:

[code]
"fnCreatedRow": function (nRow, aData, iDataIndex) {
$.each(aData, function (i, o) {

if (o.match(/^ADVERT_TYPE_.*/) !== null) {
var advert_type = o.replace(/ADVERT_TYPE_/g, ''),
img = $('')
.attr('src','img/icons/objecttypes/advert_type_' + advert_type + '.png'),
cell = $('td:eq(' + i + ')', nRow);

cell.empty().append(img);
}
});
}
[/code]

As you can see, the purpose of this code is to replace a string (returned på the Ajax call) with an appropriate image signifying the advert type of the row. Although the img element is correctly inserted into the DOM, however, the image itself never gets loaded. I get the same result from putting the equivalent code in fnDrawCallBack. The simple/ugly answer would've been to return the HTML-string for the image to be loaded directly from the server, but I'm not a big fan of that kind of "solutions".

Am I overlooking something stupid/obvious? Please help!

Replies

  • maksbolmaksbol Posts: 9Questions: 0Answers: 0
    This is the resulting HTML according to Google Chrome:

    [code][/code]

    From what I gather, the "display: none; visibility: hidden" part is put there by Chrome because it's missing the referenced image resource. Looking at the image resource list of the network tab in Chrome debugbar, it's obvious the images are never loaded at all... however, right clicking the src path and choosing to open it in a new tab correctly links to the desired image.
  • maksbolmaksbol Posts: 9Questions: 0Answers: 0
    Update: The exact same problem occours when loading the HTML string directly from the server. :| This is probablt not datatables-related.
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    I don't know what would be the problem there, off the top of my head. If you 'brute force it' like this, does it work?:

    [code]
    $('td:eq(' + i + ')', nRow).html(
    ''
    );
    [/code]

    Allan
  • maksbolmaksbol Posts: 9Questions: 0Answers: 0
    Nope. :/

    As I said, the result is exactly the same when loading the HTML directly from the server so there's really something weird going on here. The result is the same in Firefox 15.0 and Chrome 21.0.1180.89, except that in Firefox the "display: none; visibility: none" part isn't "added"...

    This is my complete dataTables option list:

    [code]
    {
    "oLanguage": oLanguage,
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "ajax/contract.php",
    "sPaginationType": "bootstrap",
    "sDom": "<'row'<'span12'<'#object_controls'>><'span4'f>r>t<'row'<'span4'i><'span8'p>>",
    "sSortAsc": "header headerSortDown",
    "sSortDesc": "header headerSortUp",
    "sSortable": "header",
    "sScrollY": calcDataTableHeight(),
    "fnServerParams": function (aoData) {
    var sShowAllObjects = $('#objects_agent_filter').val(),
    sAdvertType = $('#objects_advert_type').val(),
    oShowAllObjects = {
    "name":"sShowAllObjects",
    "value":sShowAllObjects
    },
    oAdvertType = {
    "name":"sAdvertType",
    "value":sAdvertType
    };

    aoData.push(oShowAllObjects);
    aoData.push(oAdvertType);
    },
    "fnCreatedRow": function (nRow, aData, iDataIndex) {
    $.each(aData, function (i, o) {

    if (o.match(/^ADVERT_TYPE_.*/) !== null) {
    var advert_type = o.replace(/ADVERT_TYPE_/g, ''),
    img = $('')
    .attr('src','img/icons/objecttypes/advert_type_' + advert_type + '.png'),
    cell = $('td:eq(' + i + ')', nRow);

    cell.empty().append(img);
    }

    });
    }
    }
    [/code]
  • maksbolmaksbol Posts: 9Questions: 0Answers: 0
    Just added this craziness to debug:

    [code]
    var debug_images = setInterval(function () {
    $('#datatable_test_wrapper td:nth-child(3)').html(
    ''
    }, 600);
    [/code]

    Still no images. So not even forcing this from outside of dataTables makes any difference... Which would probably mean it's something else, right?
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    I would think so yes - I'd be really surprised if DataTables was interrupting image loading like that.. Are you using a lazy loader any where or something like that?

    Allan
  • maksbolmaksbol Posts: 9Questions: 0Answers: 0
    Not that I'm aware of... I'm using standard twitter bootstrap with some custom plugins and scripts, vanilla dataTables, jeditable (for separate use w/o dataTable), and some other stuff that shouldn't interfere like jqplot, google prettify and ThreeDots.

    Added a new DIV to the base HTML of the page and added it to the debug selector above. Just like dataTables it isn't being manipulated in any way. Also, it's not subject to any extra CSS rules (just the basic CSS standardization rules), so that rules out dataTables and CSS as the source of the problem. The search goes on...
  • maksbolmaksbol Posts: 9Questions: 0Answers: 0
    Also tried adding a callback to the image load event to log if the image ever got loaded... it wasn't. :S
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    What happens if you write an image into the HTML outside of DataTables? Does that work?

    Allan
  • maksbolmaksbol Posts: 9Questions: 0Answers: 0
    Well, I don't know what the hell is going on here but changing the name of the file from "advert_type_6.png" to "adverttype6.png" seems to have solved the problem. Seems my app just refused to load any image file containing underscores in that particular folder (?!), while other paths like "img/icons/16/arrow_bottom_icon.png" loads just fine. I'm terribly sorry for wasting your time with this but it was (and still is) pretty confusing...
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Heh - very odd!

    If you've got wireshark handy it might be interesting to view the HTTP communication that is going over the network, checking that the browser is actually making a request for the file. I guess the other thing might be to clear your cache?

    Allan
  • maksbolmaksbol Posts: 9Questions: 0Answers: 0
    It's really the weirdest thing... if I put "adverttype_6_.png" wireshark registers it and Chrome shows a broken image, but if I put "advert_type_6.png" nothing happens. Cleaning the cache and cookies does nothing, but running the app in an Incognito window i Chrome "advert_type_6.png" seems to be OK. Well, I'm tired of this now and the problem is technically solved so I'll just have to live with this.
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Very odd indeed.

    Yup - moving on... :-)

    Allan
This discussion has been closed.