I have some problem with javascript limit. I am using Razor and filling javascript array with razor like this:`
var KriterAlanlariHtml = new Array();
#{
int sayac = 0;
string html = "";
foreach (var group in Model.GrupDTO)
{
if (group != null && group.ListAreaDTO != null)
{
foreach (var area in group.ListAreaDTO)
{
html = BuildHtmlTag(area);
<text>
KriterAlanlariHtml[#sayac] = "#MvcHtmlString.Create(html)";
</text>
sayac++;
}
}
}
}
`
After this codes View.cshtml filling Javascript array with html codes. But I couldnt this array in JavaScript. Because of javascript limitations. If I check the elements on Chrome, I am seeing like screenshot-1.
Screenshoot-1
Screenshoot-2
I coulndt use like this code: KriterAlanlariHtml[25] .Chrome giving this error:
KriterAlanlariHtml is not defined
How can i solve this problem ? (HTML is not wrong.)
You need to add javascript code inside script tag, the updated code is below
<script>
var KriterAlanlariHtml = new Array();
</script>
#{
int sayac = 0;
string html = "";
foreach (var group in Model.GrupDTO)
{
if (group != null && group.ListAreaDTO != null)
{
foreach (var area in group.ListAreaDTO)
{
html = BuildHtmlTag(area);
<script>
KriterAlanlariHtml[#sayac] = "#MvcHtmlString.Create(html)";
</script>
sayac++;
}
}
}
}
Related
I am trying to take the values from the array in PHP, and pass them onto the JavaScript function. The code below functions as intended. The values are taken from a MySQL database and entered into their respective arrays. [Disclaimer: New to coding]
echo "<div id='demo'></div>";
$sql = mysqli_query($conn, "SELECT DISTINCT Safepoint_name,Latitude,Longitude FROM safepoints");
while($row = mysqli_fetch_array($sql)) {
$latl[] = $row['Latitude'];
$long[] = $row['Longitude'];}
foreach($latl as $name) {
echo "$name";
}
foreach($long as $name) {
echo "$name";
}
The problem I am having is with code as shown below. It does not process and show the output as desired. I want the output to be shown one after the other after subtracting the two arrays. I would prefer if the output could be designed via HTML. What am I supposed to do?
[Note: The function is just a trial function just for checking whether it works]
<p id="demo"></p>
<button onclick = "myFunction()">Submit</button>
<script type="text/JavaScript">
let text = "";
function myFunction() {
var latitute = <?php echo json_encode($latl); ?>;
var loni = <?php echo json_encode($long); ?>;
for (let i = 0; i < latitute.length; i++)
{
var x[i] = latitute[i]-loni[i];
text += x[i]+"<br>";
}
document.getElementById("demo").innerHTML = text;
}
</script>
I think your problem here is that you are defining variable x in a cycle (first warning something is fishy) and you are assigning a value into it's array index at the same time (which is technically possible, but not recommendable, in PHP, but definitely not in JS where is causes syntax error).
You can skip the variable completely and just generate the text.
for (let i = 0; i < latitute.length; i++) {
text += (latitute[i]-loni[i]) +"<br>";
}
Or if you need to keep the result array for later, you should define it as an array before the cycle:
var x = [];
for (let i = 0; i < latitute.length; i++) {
x[i] = latitute[i]-loni[i];
text += x[i]+"<br>";
}
Update:
<p id="demo"></p>
<button onclick = "myFunction()">Submit</button>
<script type="text/JavaScript">
console.log('Preparing JS...');
let text = "";
function myFunction() {
console.log('Calculating demo...');
var latitute = [9,8,7];
var loni = [1,2,3];
for (let i = 0; i < latitute.length; i++) {
text += (latitute[i] - loni[i]) + "<br>";
}
document.getElementById("demo").innerHTML = text;
alert('Demo OK');
}
</script>
JS runs OK after submit.
Right-Click your page and use "View page source file" to check what PHP generates into the JS - if it's really valid array as in the example above.
Then open Debugger (press F12 in browser) and see Console if there are any Syntax or other errors.
If you are in doubt if JS is running, you can add console.log() and/or alert() into your code (see the example).
I have a SharePoint 2010 list of around 198 items. For the first 30 items Text to Html Javascript function successfully converts text code to Html but when I am trying to select next 31 items and go ahead using the pagination the function does not able to convert Html and display only text codes. Does anyone please who have the code handy to make this work? Below is the code used in SharePoint 2010. Thank you.
<script type="text/javascript">
function TextToHTML(NodeSet, HTMLregexp) {
var CellContent = "";
var i=0;
while (i < NodeSet.length){
try {
CellContent = NodeSet[i].innerText || NodeSet[i].textContent;
if (HTMLregexp.test(CellContent)) {NodeSet[i].innerHTML = CellContent;}
}
catch(err){}
i=i+1;
}
}
// Calendar views
var regexpA = new RegExp("\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*");
TextToHTML(document.getElementsByTagName("a"),regexpA);
// List views
var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$");
TextToHTML(document.getElementsByTagName("TD"),regexpTD);
// This function is call continuesly every 100ms until the length of the main field changes
// after which the convert text to HTML is executed.
//
var postElemLength = 0;
function PostConvertToHtml()
{
if (postElemLength == document.getElementsByTagName("TD").length)
{
setTimeout(PostConvertToHtml,100);
}
else
{
var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$");
TextToHTML(document.getElementsByTagName("TD"),regexpTD);
}
}
// Grouped list views
ExpGroupRenderData = (function (old) {
return function (htmlToRender, groupName, isLoaded) {
var result = old(htmlToRender, groupName, isLoaded);
var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$");
TextToHTML(document.getElementsByTagName("TD"),regexpTD);
// start the periodic callback to check when the element has been changed
if(isLoaded == 'false')
{
postElemLength = document.getElementsByTagName("TD").length;
setTimeout(PostConvertToHtml,100);
}
};
})(ExpGroupRenderData);
// Preview pane views
if (typeof(showpreview1)=="function") {
showpreview1 = (function (old) {
return function (o) {
var result = old(o);
var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$");
TextToHTML(document.getElementsByTagName("TD"),regexpTD);
};
})(showpreview1);
}</script>
Below is the generated text code which needs to be converted to Html. Thanks.
="<div style='position:relative;display:inline-block;width:100%;'>
<div style='width:100%;display:inline-block;text-align:center;border:1px solid "&Project_Status_clr&";position:absolute;color:"&Project_Status_clr&";'> "&Project_Status&"
</div>
<div style='display:inline-block;width: 100%;background-color:"&Project_Status_clr&";text-align:center;border:1px solid;z-index:-1;filter:alpha(opacity=20);opacity:0.2;'>"&Project_Status&"
</div>
</div>"
When generating a string of HTML in a calculated column in SharePoint 2010, you can change the calculated column's value type to "Number" to get the HTML to render in the list view.
I am trying to assign a value from a Model into a JavaScript variable. But I have to check a condition before doing so.
Normally, I can assign values using this statement in script : var quantity = '#Model.Quantity'. It works, but I don't know how to do this inside an if loop, like-
<script>
#if (Model.Count == 0)
{
var quantity = 0;
}
else
{
var quantity = '#Model.Quantity';
}
</script>
But the statement inside the loop will be rendered as C#. So how do I achieve this ? I have to store the quantity into a JavaScript variable inside if loop.
Thanks :)
Try this approach:
<script>
#if (Model.Count == 0)
{
#: var quantity = 0;
}
else
{
#: var quantity = '#Model.Quantity';
}
</script>
Can you try as follow:-
<script>
var quantity = 0;
#if(Model.Count > 0)
{
quantity = 'Model.Quantity';
}
</script>
So I have set up two javascript arrays to pull information from some php. One array gets the name of the category to be clicked on, while the other array stores the class and id tag for the category. The class and id tags are the same other than there css type, but the array needs to output them into document elements and then, when clicked, affect the relevant areas of the document. I also need to remove duplicates from the arrays, which doesn't seem to work under my current code:
<script type="text/javascript">
var BookSeries = [];
var BookClass = [];
var i=0;
</script>
then variables for the array are pulled from php and output this way:
<script type="text/javascript">
var uniqueSeries = BookSeries.filter(function(elem, pos) {
return BookSeries.indexOf(elem) == pos;
});
var uniqueClass = BookClass.filter(function(elem, pos) {
return BookClass.indexOf(elem) == pos;
});
while (uniqueSeries[i]) {
document.write( "<span id='"+uniqueClass[i]+"'>"+uniqueSeries[i]+"</span>" );
i++;
}
for(var i = 0; i < uniqueClass.length; i++) {
$np("#"+uniqueClass[i]).click(function(){
$np(".postitem").fadeOut(200);
$np("."+uniqueClass[i]).fadeIn(200);
});
}
</script>
You are using jquery so you can do the following for appending the elements to the DOM:
var htmlString = "";
for (var i = 0; i < uniqueSeries.length; i++) {
htmlString += "<span id='"+uniqueClass[i]+"'>"+uniqueSeries[i]+"</span>";
}
$("#myContainer").html(htmlString);
Not sure what is $np so I'll assume you meant jquery's $.
for(var i = 0; i < uniqueClass.length; i++) {
var uClass = uniqueClass[i];
$("#" + uClass).click(function(){
$(".postitem").fadeOut(200);
$("." + uClass).fadeIn(200);
});
}
Edit:
"#myContainer" refers to the id of the dom element you want to append the html to. if you just want to append it to document you can do:
$(document).appendTo(htmlString);
Also see I updated the code above to reflect your comments about the uniqueClass array.
Not able to display div with style.display='block';
i have a jsp with style as display : none
i want to set display as block in javascript
i have written below code but its not working
also i have tried to set display in document.ready
strangely it was working but after refreshing page 2 3 times.
result here is loaded by making ajax call.
jsp code:
<c:set var="projVar" value="ssc-portal-2.0.3.x"/>
<div style="display: none;" id="<c:out value="${projVar}"/>">
</div>
javascript:
// result is : ssc-portal-2.0.3.x#/P_ssc##SSC00004951#/P_ssc(WI- 1409);,
function populateActivityInfo(result) {
var resultProjArr = result.split(',');
var tempProjName = '';
var activityList = '';
var tempProj = '';
for (var i = 0; i < resultProjArr.length; i++) {
if (trim(resultProjArr[i]) != '') {;
tempProjName = resultProjArr[i].split('##')[0];
activityList = resultProjArr[i].split('##')[1];
tempProj = trim(tempProjName.split('#')[0]);
//here tempProj is ssc-portal-2.0.3.x
document.getElementById(tempProj).style.display = 'block';
}
}
}
You say the id is ssc-portal-2-0-3-x
//here tempProj is ssc-portal-2-0-3-x
document.getElementById(tempProj).style.display = 'block';
While the id should be ssc-portal-2.0.3.x.
<c:set var="projVar" value="ssc-portal-2.0.3.x"/>