views:

37

answers:

2

Hi,

I am using flexigrid for one of my projects and I need to come up with a way to change the image source depending on the value of one of the cells. For people who are used to flexigrid, I have the following code:

$json .= ",'".addslashes("<span><img src='' id='flag' />" . $row['availability']. "</span>")."'"; 

and my javascript that I've come up with , looks like this:

<script type="text/javascript"> 
var available = "<?php echo '$row[availability]' ?>"; 

if (available == 0) { 
document.getElementById('flag').src="images/flag_red.png"; 
} 
elseif (available == 1) { 
document.getElementById('flag').src="images/flag_green.png"; 
} 
else { 
document.getElementById('flag').src="images/flag_orange.png"; 
}

I am not sure where I need to insert this function and how to trigger it. Any help will be greatly appreciated.

Regards, Cristian.

LE: The code where the problem is being reported:

url: 'post2.php',
            dataType: 'json',
            colModel : [
                {display: 'ID', name : 'id', width : 40, sortable : true, align: 'center', hide: true},
                {display: 'URL', name : 'url', width : 450, sortable : false, align: 'left'},
                {display: 'File Name', name : 'filename', width : 270, sortable : true, align: 'left'},
                {display: 'Availability', name : 'availability', width : 50, sortable : true, align: 'center'},
                {display: 'State', name : 'state', width : 40, sortable : true, align: 'center'},
                {display: 'Total Size', name : 'totalsize', width : 90, sortable : false, align: 'center'},
                {display: 'Current Size', name : 'currentsize', width : 90, sortable : false, align: 'center'},
                {display: 'Procent', name : 'procent', width : 40, sortable : true, align: 'center'},
                {display: 'Log',  width : 20, sortable : false, align: 'center'},
                ],
            buttons : [
                {name: 'Add', bclass: 'add', onpress : test},
                {separator: true},
                {name: 'Delete', bclass: 'delete', onpress : test},
                {separator: true},
                {name: 'Select All', bclass : 'selectall', onpress : test},
                {name: 'DeSelect All', bclass : 'deselectall', onpress : test},
                {separator: true}
                ],
            searchitems : [
                {display: 'URL', name : 'url'},
                {display: 'Filename', name : 'filename', isdefault: true}
                ],
            sortname: "state",
            sortorder: "asc",
            usepager: true,
            title: '',
            useRp: false,
            rp: 5,
            showTableToggleBtn: true,
            } ----- **IE says there is a problem here**         );   
});
+1  A: 

You shouldn't use javascript for this, you can do it directly in your existing PHP line.

$json .= ",'" . 
         addslashes("<span><img src='" . 
         ($row['availability'] == 0 ? "images/flag_red.png" :
            ($row['availability'] == 1 ? "images/flag_green.png" : 
               "images/flag_orange.png")
         ) . 
         "' id='flag' />" . $row['availability'] . "</span>") . "'";
Fosco
Simple and effective. Thanks, Fosco ... greatly appreciated. Everyday I learn a new thing, I am loving it. Cheers, C.
Chris19
Tested the code in an older browser IE 7.0.53 and is not working, is causing an error in the javascript, Expected identifier, string or number flexigrid, line 63 character 4, the location pointed out is where the configuration of the table is closing. If I remove the code, no problems.
Chris19
@Chris19 I guess that's a separate issue.. is flexigrid compatible with that version of IE?. As you know this solution didn't involve javascript.
Fosco
From what I can tell, the entire table content is generated by javascript, so I am guessing the code disrupts the order in that table. I will update the question in one sec.
Chris19
and the code provided by you gets inserted in the post2.php file.
Chris19
@Chris19 There's a comma before that `}` which needs to be removed.
Fosco
You were right. Thanks, that was the problem. I wonder why was it causing problems only after I added the new code. ??!?!
Chris19
A: 

It depends on whether cell values change after the page has loaded or not. If not, then place that Javascript snippet just before the </body> tag.

However, if your page uses AJAX to update cell values, you'd need put that code into a callback function that gets invoked after a successful AJAX request.

Saul
Thanks for your answer. Looks like there was a simpler way to resolve my problem. Fosco's code is the way to go, worked right out of the box :).
Chris19
@Chris: Yup, Fosco's approach is good enough if the content of a page doesn't change after the document loads.
Saul
Yes, that value doesn't change after that.
Chris19