繁体   English   中英

如何使用 php ajax javascript 根据选定的下拉项更改表格的颜色

[英]how can I change the color of a table based on selected drop down item using php ajax javascript

我对 php 还很陌生,对 PDO 甚至更新! 我想要做的是根据表单中下拉菜单的选择更改行的颜色,但我不知道如何执行此操作!

我只是在谷歌上搜索了一下,没有找到任何与我想要做的事情接近的东西

这是主表

 <div class="row">
    <div class="large-4 columns">
      <label>Current Status
        <select name="current_status">
            <option>Select Status</option>
            <?php foreach($statuses as $key => $value) : ?>
                <option value="<?php echo $key; ?>"><?php echo $value; ?></option> 
            <?php endforeach; ?>
        </select>
      </label>

这是“关键”代码

'InProgress'=>'In Progress',
'Completed'=>'Completed',
'Unstatused'=>'Unstatused'

这是控制器页面代码

  <div class="large-4 columns">
      <label>Select Current Status
          <select name="current_status">
            <?php foreach($statuses as $key => $value) : ?>
                <?php 
                    if($key == $task->current_status) {
                        $selected ='selected';
                    }else{
                        $selected='';
                    }
                ?>
                <option value="<?php echo $key; ?>" <?php echo $selected; ?>><?php echo $value; ?></option> 
            <?php endforeach; ?>
        </select>
      </label>
    </div>

     <input type="hidden" name="id" value="<?php echo $task->id; ?>">
     <input name="submit" type="submit" class="add_btn button right small" value="submit"> 
     <a class="close-reveal-modal" aria-label="Close">&#215;</a>
</form>
</div>

所以我希望可以在这里完成的是; 如果在表单下拉列表中选择了“已完成” - 那么我想将保持该任务的行颜色更改为红色,如果他们选择“未状态”,我希望将该表行更改为蓝色......但我没有关于如何做到这一点的线索!

    <table><!--start table-->
            <thead><!--create the headers-->
                <tr><!--the table row for the table headers-->
                    <th width="100">task date</th>
                    <th width="450">task title</th>
                    <th width="150">assigned to</th>
                    <th width="100">current status</th>
                    <th width="200">action</th>
                </tr><!--close the table header table row-->
             </thead><!--close the headers-->
                <tbody><!--begin the table body-->
                <?php foreach($tasks as $task) : ?>
                    <tr>
                        <td><?php echo $task->task_date; ?></td>
                        <td><?php echo $task->task_title; ?></td>
                        <td><?php echo $task->assigned_to; ?></td>
                        <td><?php echo $task->current_status; ?></td>

                       <td>
                        <ul class="button-group radius">
<li>
<a href="#" class="button tiny" data-reveal-id="editModal<?php echo $task->id; ?>" data-task-id="<?php echo $task->id; ?>">Edit</a>
<div id= "editModal<?php echo $task->id; ?>" data-cid= "<?php echo $task->id; ?>"  class="reveal-modal editModal" data-reveal>

我想要更改的行是“current_status”行 - 基于他们是否在用于填充表格的表单中选择了“已完成”或“进行中”。

JEFF-这是完整的代码页(控制器页)

<?php
//create database object
$db = new Database;
//bring in the contacts
$db->query("SELECT * FROM multytasklist");
//assign the resultset
$tasks = $db->resultset();
?>

 <div class="row"><!--begin the row for table data-->
    <div class="large-12 columns"><!--begin columns-->
        <table><!--start table-->
            <thead><!--create the headers-->
                <tr><!--the table row for the table headers-->
                    <th width="100">task date</th>
                    <th width="450">task title</th>
                    <th width="150">assigned to</th>
                    <th width="100">current status</th>
                    <th width="200">action</th>
                </tr><!--close the table header table row-->
             </thead><!--close the headers-->
                <tbody><!--begin the table body-->
                <?php foreach($tasks as $task) : ?>
                    <tr>
                        <td><?php echo $task->task_date; ?></td>
                        <td><?php echo $task->task_title; ?></td>
                        <td><?php echo $task->assigned_to; ?></td>
                        <td><?php echo $task->current_status; ?></td>

                       <td>
                        <ul class="button-group radius">
<li>
<a href="#" class="button tiny" data-reveal-id="editModal<?php echo $task->id; ?>" data-task-id="<?php echo $task->id; ?>">Edit</a>
<div id= "editModal<?php echo $task->id; ?>" data-cid= "<?php echo $task->id; ?>"  class="reveal-modal editModal" data-reveal>

    <h2>EDIT TASK</h2>

   <form id="edit_multytask" action="#" method="POST">

  <div class="row">
    <div class="large-6 columns">
      <label>Task Date
        <input name="task_date" type="text" placeholder="task date.." value="<?php echo $task->task_date; ?>">
      </label>
    </div>

     <div class="large-6 columns">
      <label>Task Title
        <input name="task_title" type="text" placeholder="task title.." value="<?php echo $task->task_title; ?>" >
      </label>
    </div>
  </div>

  <div class="row">
    <div class="large-6 columns">
      <label>Task Description
        <input name="task_description" type="text" placeholder="task description.." value="<?php echo $task->task_description; ?>" >
      </label>
    </div>
    </div>


     <div class="row">
     <div class="large-12 columns">
      <label>assigned to
        <input name="assigned_to" type="text" placeholder="assigned to.." value="<?php echo $task->assigned_to; ?>" >
      </label>
     </div>
     </div> 


   <div class="large-4 columns">
      <label>Select Current Status
          <select name="current_status">
            <?php foreach($statuses as $key => $value) : ?>
                <?php 
                    if($key == $task->current_status) {
                        $selected ='selected';
                    }else{
                        $selected='';
                    }
                ?>
                <option value="<?php echo $key; ?>" <?php echo $selected; ?>><?php echo $value; ?></option> 
            <?php endforeach; ?>
        </select>
      </label>
    </div>

     <input type="hidden" name="id" value="<?php echo $task->id; ?>">
     <input name="submit" type="submit" class="add_btn button right small" value="submit"> 
     <a class="close-reveal-modal" aria-label="Close">&#215;</a>
</form>
</div>
</li>

<li>
    <form id="delete_multytask" action="#" method="post">
        <input type="hidden" name="id" value="<?php echo $task->id; ?>">
        <input type="submit" class="delete_btn button tiny secondary alert" value="delete">
    </form>
</li>
                        </ul><!--close unordered list-->

                       </td><!--close table data-->
                    </tr><!--close table row-->
                   <?php endforeach; ?>
                 </tbody><!--close tbody-->
             </table><!--close table-->
         </div><!--close 12 column class-->
        </div><!--close row-->

这是动态发生的,因此 php 和 pdo 在这种情况下是不合适的。 你的 css 需要是:

<style>
.InProgress { background-color: red; }
.Completed { background-color: orange; }
.Unstatused { background-color: blue; }
</style>

您需要将<select>项更改为:

<select name="current_status" onchange='changeColors(this.value)'>

添加一些 JavaScript:

<script>
function changeColors( value ) {
  var element = document.getElementById('myElementToChange');
  element.className = value;
}

请注意,这是一个粗略的指南,因为您没有提供完整的 html,所以我只能猜测您想要更改的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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