[英]How to apply highlight function on each table row?
I developed a web project where I can upload two files and django server execute a function to return the lines that don't match with other in a pandas dataframe which is renderized in a html template. I developed a web project where I can upload two files and django server execute a function to return the lines that don't match with other in a pandas dataframe which is renderized in a html template. Is returned a two columns table, file1 and file2.返回一个两列的表,file1 和 file2。 I would like to have another column with the text and text difference 'highlighted'.我想要另一列“突出显示”文本和文本差异。 I have no knowledge about javascript programing, but I don't know how to apply the function on each table row.我对 javascript 编程一无所知,但我不知道如何在每个表行上应用 function。 My views.py我的观点.py
def index(request):
form = UploadFileForm(request.POST, request.FILES)
if request.method == 'POST':
if form.is_valid():
check = CheckFiles(form.cleaned_data["arquivo1"], form.cleaned_data["arquivo2"]).verifica_tamanho()
if type(check) == str:
return HttpResponse('The file are the same!')
#return HttpResponse(check.to_html())
return render(request, 'index2.html', {'check': check})
else:
print('Invalid Form!')
return render(request, 'index.html', {'form': form})
and in my index2.html:在我的 index2.html 中:
<table class="table" border="1px">
<thead class="thead-dark">
<tr>
<th scope="col">Original File</th>
<th scope="col">Secondary File</th>
</tr>
</thead>
<tr>
{% for _, record in check.iterrows %}
<tr>
{% for value in record %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
</tr>
</table>
and the highlight javascript function is:和亮点 javascript function 是:
highlight($("#new"), $("#old"));
function highlight(newElem, oldElem){
var oldText = oldElem.text(),
text = '';
newElem.text().split('').forEach(function(val, i){
if (val != oldText.charAt(i))
text += "<span class='highlight'>"+val+"</span>";
else
text += val;
});
newElem.html(text);
}
.highlight {background-color: #B4D5FF}
So, I need some help to understand how to apply this javascript function on each table row.所以,我需要一些帮助来了解如何在每个表格行上应用这个 javascript function。 Thank you, have a nice day.感谢您有一个愉快的一天。
I can see you also use jquery so:我可以看到你也使用 jquery 所以:
$(.table tr).each(function(index){
$(this).addClass("highlight");
});
that way you can apply highlight to every row.这样您就可以将突出显示应用于每一行。 It seems like your current highlight function "highlights" chars that have changed in the string but I have no idea what it has to do with rows... What is $("#new")... what is $("#old")...似乎您当前的突出显示 function “突出显示”字符串中已更改的字符,但我不知道它与行有什么关系...什么是 $("#new")...什么是 $("#老的”)...
**UPDATE ** **更新 **
$("table tr").each(function(index){
a = $(this).find("td");
first_td = a.eq(0);
second_td = a.eq(1);
highlight(second_td, first_td);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.