简体   繁体   中英

Edit an entire column in an HTML/PHP table through jquery/javascript/ajax?

I need help!

I have a php page that generates a html table from an array(). This array contains all the lines of a csv (no problem with this):

load_csv.php

<?php
function print_array_of_arrays_to_html_table(&$array_by_reference)
{
    echo "<br><table border=1 align=center id=tableID>";
    for ($i = 0; $i < count($array_by_reference); $i++)
    {           
        echo "<tr>";
        for ($j = 0; $j < count($array_by_reference[$i]); $j++)
        {
            echo "<td align=right title='f:".$i."\nc:".$j."\nv:".($array_by_reference[$i][$j])."'>".($array_by_reference[$i][$j])."</td>";
        }
        echo "</tr>";
    }
    echo "</table>";        
}
?>

My intention is to indicate a column number in a text field, change all values ​​of all cells in that column. The value would be the same for all cells in that column. For example, change all to '0' o whatever.

For now, with 'jquery' I can individually change any cell:

same load_csv.php:

<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script language="javascript" type="text/javascript">
$('#tableID').click(function(event){
    var original = $(event.target).text().toString();
    var original_copy = original;
    var retValue = prompt("\nChange value? ", original);
    //if not empty and OK is pressed
    if (retValue !== '' && retValue !== null)
    {
         //if not the same that 'original'
         if (retValue !== original)
         {
            $(event.target).text(retValue);                     
         }
         else
         {
            alert("has not been modified" + original);
         }
    }
    else
    {
         //if click Cancel
         alert("has not been modified: " + original_copy);
    }               
})
</script>

But also want to change a whole column at a time. For example, calling the function onclick() of a button:

same load_csv.php:

<form>
    <input type='button' value="Go back" onClick="history.go(-1);return true;"/>
    <!-- if onclick button all values ​​of all cells in the column are changed -->
    <br>Column: 
    <input type='text'/>
    <input type='button' value="Change all column?" onClick="myfunction()"/>
</form>

output example: enter link description here

Any tips (yes, all code in same php.page. Sorry for my bad english)?

Thanks in advance.

EDIT: Solution: I created two selectbox, one showing only the columns I intend to change, and one that allows me to choose to add two values ​​(0 or 1). Then with the button I call the function. This includes the selectbox selected values. Next ​​I check single cells (td) having 0 or 1 and "Pum"! success.

<select name="cols_bool" id="cols_bool">
    <option value="not_selected" selected="selected">Choose...</option>
<?php 
    if (count($col_with_bools) > 0)
    {
        for ($i = 0; $i < count($col_with_bools); $i++)
        {
            echo "<option value=".$i.">".$col_with_bools[$i]."</option>";
        }           
    }       
?>
</select>
<br>New bool values to insert:
<select name="value_to_replace" id="value_to_replace">
    <option value="1" selected="selected">1 (TRUE)</option>
    <option value="2">0 (FALSE)</option>
</select>
<input type='button' class="change" value="Change all cells?" onClick="replace()"/>

<script language="javascript" type="text/javascript">
function replace()
{
    var column_select = $('#cols_bool option:selected').text();
    var value_to_insert = $('#value_to_replace option:selected').text();
    $("#tableID").find('tr').each(function(){
       var $td = $(this).find('td');
       var $td_text = $td.eq(column_select).text();
       if ($td_text == '1' || $td_text == '0')
       {
           $td.eq(column_select).text(value_to_insert.substring(0,1));
       }                                
    });
}
</script>

you can simply do like this

function replace(value_to_replace)
{
$("#tableID").find('tr').each(function () {
        var $td = $(this).find('td');
            $td.eq(3).text(value_to_replace);        
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM