简体   繁体   English

我尝试在共享点上突出显示我的表行,如果它与数组中的任何内容匹配,为什么不起作用?

[英]I am trying to highlight my table row on sharepoint if it matches anything in an array, why doesn't it work?

I have a table on Sharepoint and an array, I tried to compare them and highlight the ones that match but it doesn't work. 我在Sharepoint上有一张桌子和一个数组,我试图将它们进行比较并突出显示匹配的那些,但这是行不通的。 Please help! 请帮忙! The following code is coded in Javascript. 以下代码使用Javascript编码。 I have tried various ways and tweaked accordingly according to my findings all around the web but nothing seems to be able to resolve this issue. 我尝试了各种方法,并根据我在网络上的发现进行了相应的调整,但是似乎没有任何方法可以解决此问题。

    //gets table
var oTable = document.getElementById('tablename');

//gets rows of table
var rowLength = oTable.rows.length;

//loops through rows    
for (i = 0; i < rowLength; i++){

  //gets cells of current row  
   var oCells = oTable.rows.item(i).cells;

   //gets amount of cells of current row
   var cellLength = oCells.length;

   //loops through each cell in current row
   for(var j = 0; j < cellLength; j++){

        // get your cell info here

        var cellVal = oCells.item(j).innerHTML;
        console.log(cellVal);

        var serverRelativeUrlOfMyFile = "https://Sharepointsite/Blacklist.txt";
        $.ajax({
            url: serverRelativeUrlOfMyFile,
            type: "GET"
        }).done(handler);
        function handler(data){
            //console.log(data);
            var lines = data.split('\n');
            for(var line = 0; line < lines.length; line++)
            {
                if (cellVal.includes(lines[line])){
                    oCells.item[j].style.background = "red";
                }
            }
        }
    }
}

Below's another way that I tried to match my table on Sharepoint with my array. 下面是我尝试将Sharepoint上的表与数组匹配的另一种方法。 It works partially - Only the last item in the array gets matched when I run through the array properly. 它部分起作用-当我正确运行数组时,只有数组中的最后一项才匹配。 I don't really understand so appreciate the people out there who can help! 我不太了解,所以感谢能帮助我的人! Thanks a lot in advance. 非常感谢。

(Additional Information!) I create my table on Sharepoint by generating a table using Javascript with the executeQueryAsync function to retrieve data from a Sharepoint list and then producing it out as a viewable HTML table. (其他信息!)我通过在Java上使用表和executeQueryAsync函数生成一个表以从Sharepoint列表中检索数据,然后将其生成为可查看的HTML表,从而在Sharepoint上创建表。

        var serverRelativeUrlOfMyFile = "https://Sharepointsite/SoftwaresBlacklist.txt";
$.ajax({
url: serverRelativeUrlOfMyFile,
type: "GET"
}).done(handler);
function handler(data){
//console.log(data);
var lines = data.split('\n');
    for(var line = 0; line < lines.length; line++){
        //var templines = lines;
        //alert(lines[line]);

        $("#tablename tr:contains('" + lines[line] + "')").css("background-color", "red");
    }
}

I ran into something similar recently. 我最近遇到了类似的情况。 It came down to an issue of "scope" when referring to objects outside of the AJAX function. 当引用AJAX函数之外的对象时,它归结为“范围”问题。

Try something like this: (the new property named "theCells" and the line that starts with "this.theCells".) 尝试这样的事情:(名为“ theCells”的新属性以及以“ this.theCells”开头的行。)

    $.ajax({
        url: serverRelativeUrlOfMyFile,
        theCells: oCells,
        type: "GET"
    }).done(handler);
    function handler(data){
        //console.log(data);
        var lines = data.split('\n');
        for(var line = 0; line < lines.length; line++)
        {
            if (cellVal.includes(lines[line])){
                //oCells.item[j].style.background = "red";
                this.theCells.item(j).style.background = "red";
            }
        }
    }

Another note, "item" is a method, so should use (), not []. 另外请注意,“ item”是一种方法,因此应使用(),而不要使用[]。

this.theCells.item[j].style.background = "red";

this.theCells.item(j).style.background = "red";

暂无
暂无

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

相关问题 我正在编写一个Javascript函数来为表中的每一行着色不同的颜色。 为什么我的代码不起作用? - I am writing a Javascript function to to color every other row in a table a different color. Why doesn't my code work? 为什么当我尝试将函数的返回值与字符串连接时我的代码不起作用? - why my code doesn't work when I am trying to concatenate a function's return value with a string? 我正在尝试优化第一块代码。 为什么我的方法不起作用? 缩短代码的最佳方法是什么? - I am trying to optimize the first chunk of code. Why doesn't my approach work? What's the best approach to shorten the code? 我试图用jQuery创建一个按钮。 为什么不起作用? - I am trying to create a button with jQuery. Why doesn't it work? 为什么这个 JS 和 PHP 代码不起作用? (我正在尝试将值从 JS 发送到 PHP) - Why doesn't this JS & PHP code work? (I am trying to send a value from JS to PHP) 我正在尝试使用此 JavaScript 行滚动 div 容器,但它不起作用。 为什么? - I'am trying to scroll a div container with this JavaScript line, but it doesn't work. Why? 为什么不起作用? 尝试使用隐藏的提交按钮添加行 - Why doesn't it work? Trying to add row with hidden submit button 我正在尝试使用 fetch api 显示数据,但它不显示任何内容 - I am trying to display data using fetch api but It doesn't display anything 我的动态表格行创建代码无效 - My dynamic table row creation code doesn't work 我不明白为什么我的代码不起作用有人可以检查一下吗? 我很新 (JavaScript) - i can't understand why my code doesn't work can someone please check it out? I am very new (JavaScript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM