简体   繁体   English

表格列有两个选择

[英]Two selects with table columns

I have a table with multiple columns. 我有一个包含多列的表。 I have two select boxes: 我有两个选择框:

  • one to change the background color of the highlighted column 一种更改突出显示的列的背景颜色
  • one to select which column to highlight 一个选择要突出显示的列

This assumes that I click the #colorselect select box first. 假设我先单击#colorselect选择框。 Do I need to create another function with the #columnselect select box first, then #colorselect function built inside? 我是否需要先使用#columnselect选择框创建另一个函数,然后再创建内部的#colorselect函数?

Here's what I have so far: 这是我到目前为止的内容:

$(function(){
    $("#colorselect").change(function() {
        $("#colorselect option:selected").each(function() {
            if($(this).attr("value")=="red") {
                clr="red"
            }

            if($(this).attr("value")=="green") {
                clr="green"
            }

            if($(this).attr("value")=="") {
                clr="yellow"
            }
        };

        $("#columnselect").change(function() {
            $("#columnselect option:selected").each(function() {
                if($(this).attr("value")=="column1") {
                    columnhighlightsel="Column1"
                }

                if($(this).attr("value")=="column2") {
                    columnhighlightsel="Column2"
                }

                if($(this).attr("value")=="") {
                    columnhighlightsel="Column2"
                }
            }

            for (var i = 0; i<= $("#table th").length; i++) {
                if($.trim($("#dailytable th:nth-child("+i+")").text()) === columnhighlightsel) {
                    varcolumnhighlight=i
                }
            };

            $("#table tbody tr td:nth-child("+columnhighlight+")").each(function() {
                $(this).css('background-color', clr);
            };

I would just write a single function that handles a change in either column. 我只想编写一个函数来处理任一列中的更改。 Check if either is blank before executing the code. 执行代码之前,请检查其中一个是否为空。 Then the order doesn't matter. 然后顺序就没关系了。

Then just make the values in your color select the colors you want, and the column the number of the column: 然后只需使颜色中的值选择所需的颜色,然后将该列作为该列的编号即可:

$('#colorselect, #columnselect').change(function() {
  var $color = $('#colorselect').val(),
      $column = $('#columnselect').val();
  if ($color && $column) {
    $("#table tbody tr td:nth-child("+$column+")").each(function(){
      $(this).css('background-color', $color);
    };
  }
});

Depending on your values, you may have to adjust the if condition to be more specific. 根据您的值,您可能必须将if条件调整为更具体。

 $("#colorselect").change(function(){
           clr = $("#colorselect option:selected").attr("value");
        clr = clr == '' ? 'yellow' : clr;
        color();
    }

   $("#columnselect").change(function(){

         columnhighlightsel= $(this);
        color();
    }

function color(){
   if(!columnhighlightsel || !clr){
      return;
   }
   columnhighlightsel.css('background-color', clr);
}

You now have to get the columnhighlightsel and clr- variables into the color-function context. 现在,您必须将columnhighlightsel和clr-变量放入颜色函数上下文中。

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

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