如何将阵列添加到JavaScript变量 [英] How to add a Array to a Javascript Variable

查看:163
本文介绍了如何将阵列添加到JavaScript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题,对于大多数的你我想;-)我有一个数组(从SharePoint列表)这个数组这样表示,如果通过我写的.html成一个DIV:

\r
\r

{NAME =Drittes Kapitel文本=uiuiuiVALUE =45.0000000000000 }\r
{NAME =艾因anderer影片名称文本=艾因zweiter文本值=123.000000000000}\r
{NAME =艾因影片名称文本=达斯IST DER文本值=256.000000000000}

\r

\r
\r

我的code把这个成一个DIV如下:

\r
\r

函数AddContent(名称,文字,价值)\r
{$(#meineListe)追加('{名称=+名字+'文本=+文字+VALUE =+价值+'}< BR />');\r
                                \r
}

\r

\r
\r

所以它显示了我在这个数组中的所有3个元素
但我怎么可以把数组变量?
如果我尝试

\r
\r

函数AddElements(名称,文字,价值)\r
{MyElements =('{名称=+名字+'文本=+文字+VALUE =+价值+'}< BR />');\r
的console.log(MyElements);\r
$(#meineListe)HTML(MyElements);\r
                                \r
}

\r

\r
\r

这表明我只是其中的第一项......我不明白究竟这里发生了什么。

全code(从sharepointhillbilly):

\r
\r

//这是在脚本启动后的页面加载\r
$(文件)。就绪(函数(){\r
\r
    GetMyListData();\r
\r
});\r
\r
功能GetMyListData()\r
{\r
        // Web服务方法,我们呼吁,阅读列表项,我们使用'GetListItems\r
        VaR方法=GetListItems;\r
        \r
        //我们从读取数据的列表的显示名称\r
        VAR列表=MYLIST;\r
\r
        //我们需要确定我们要返回的字段。在这种情况下,我们想要的姓名(名称),\r
        //从发言者名单博客,图片和领域。你可以在这里看到,我们正在使用的内部字段名称。\r
        //为演讲嘉宾的名字显示名称字段是名和内部名称为标题。你可以看到它可以\r
        如果你的显示名称是从你的内部名称完全不同的充//很快变得混乱。\r
        //对于任何列表中,您希望从阅读,一定要指定您希望返回的字段。\r
        变种fieldsToRead =下; ViewFields>中+\r
                                < FieldRef名称=标题/>中+\r
                                < FieldRef名称='的Treo'/>中+\r
                                < FieldRef名称='iz1y/>中+\r
                            &所述; / ViewFields>中;\r
                            \r
        //这是美好的CAML查询我早先提到。这个简单的查询返回\r
        //所有行说给我所在的ID字段不等于0的所有行。然后我\r
        //告诉查询由标题字段对行进行排序。 FYI:一个空白查询也返回\r
        //所有的行,但我喜欢用下面的查询,因为它可以帮助我知道,我的意思是\r
        //返回所有的行,不只是忘了写一个查询:)\r
        变种查询=下;查询>中+\r
                        <其中>中+\r
                            <&NEQ GT; +\r
                                < FieldRef名称='ID'/><值类型='数'> 0℃; /价值与GT; +\r
                            &所述; / NEQ>中+\r
                        < /在哪里>中+\r
                        &所述;排序依据>中+\r
                            < FieldRef名称=标题/>中+\r
                        &所述; /排序依据>中+\r
                    < /查询>中;\r
\r
        //这里是我们SPServices打电话,我们在传递,我们在上面设置变量\r
        $()。SPServices({\r
                操作:方法,\r
                异步:假的,//如果设置为true,你可能会得到更快的性能,但您的订单可能不准确。\r
                LISTNAME:列表中,\r
                CAMLViewFields:fieldsToRead,\r
                  CAMLQuery:查询,\r
                      //这基本上意味着做以下code调用完成时\r
                    completefunc:功能(XDATA,状态){\r
                        //通过数据中的每一行此code遍历从Web服务调用返回\r
                        $(xData.responseXML).SPFilterNode(Z:排)。每个(函数(){\r
                            //这里是我们正在阅读的字段值,并把它们在JavaScript中的变量\r
                            //注意,当我们读一个字段值有一个ows_内部字段名的前面。\r
                            //这是SharePoint Web服务怪癖,你需要牢记。\r
                            //所以要读出的一场它总是$(本).attr(ows_<内部字段名称>);\r
                            \r
                            //获取标题字段(元素标题)\r
                            变量名称=($(本).attr(ows_Title));\r
VAR文字=($(本).attr(ows_treo));\r
VAR值=($(本).attr(ows_iz1y));\r
                            \r
                            //获取博客网址时,SharePoint存储在&LT格式的URL,URL><&逗号GT;<描述>\r
                            //我们只想在< URL取代。要做到这一点,我们使用JavaScript分割功能\r
                            //它会变成< URL><&逗号GT;<描述>成阵列,其中的第一个元素[0]\r
                            //是URL。捕捉所有的问题?如果你没有,这是你应该另有原因\r
                            //开发者,如果你正在编写JavaScript和jQuery :)\r
                            // VAR博客=($(本).attr(ows_Blog))分裂()[0]。\r
                            \r
                            //事情是一样的博客,图片存储为< URL><&逗号GT;< ALT文字>\r
                            // VAR pictureUrl =($(本).attr(ows_Picture))分裂()[0]。\r
                            \r
                            //调用一个函数从该行的数据添加到一个表在屏幕上\r
                            AddElements(名称,文字,价值);\r
                            \r
                        });\r
                    }\r
        });\r
\r
}\r
\r
//很简单的功能,增加了一个行到表与speakerTable的id\r
//从我们SPServices调用返回的数据中的每一行。\r
//表中的每一行显示扬声器的图象和\r
//下面的演讲者的照片将成为他们的名字是一个超链接\r
//演讲者的博客。\r
功能AddRowToTable(名称,文字,价值)\r
{MyElements =('{名称=+名字+'文本=+文字+VALUE =+价值+'}< BR />');\r
的console.log(MyElements);\r
$(#meineListe)HTML(MyElements);\r
                                \r
}

\r

<! - 表,其中我们的listContent行会 - >\r
< D​​IV ID =meineListe>< / DIV>

\r

\r
\r

有什么建议?

我需要这个阵列中的JS变量。
有另一种解决方案,在.append增加一个阵列成varaible?


解决方案

$('#meineList')追加(项目); 将追加项目 meineList 你的元素,而 $('#meineList')。HTML(项目) ; 将取代任何HTML的元素中以 meineList 的id与项目您在传递。

我假设你叫 AddContent() AddElements()对于每个项目,这是原因为什么当你调用 AddElements(),它会显示你的第一个元素,然后用第二个替换它,然后用第三替换它,从而只显示最后一个要素,你传递给它。所以,你一定要使用 AddContent()功能,让您的所有项目都将显示。

要添加每次在传递给数组的项目,你需要的功能之外创建一个数组,然后添加一个新的元素,它在每次调用你的函数的时间。

编辑:

看着你满code,你的 AddRowToTable()函数应该被称为为每个要添加的元素,因此,如果调用的.html()在函数内部,你只会得到最后一个元素的输出。

相反,只是元素添加到您的 AddRowToTable()函数数组,然后打印出数组的内容你的 GetMyListData后( )功能齐全。

试着改变你的code到这样的事情(我删除了更好的可读性评论)

  VAR ARR = [];$(文件)。就绪(函数(){
  GetMyListData();
  $('#meineList')的HTML(arr.toString());
});功能GetMyListData(){
  VaR方法=GetListItems;
  VAR列表=MYLIST;  变种fieldsToRead =下; ViewFields>中+
    < FieldRef名称=标题/>中+
    < FieldRef名称='的Treo'/>中+
    < FieldRef名称='iz1y/>中+
    &所述; / ViewFields>中;  变种查询=下;查询>中+
    <其中>中+
    <&NEQ GT; +
    < FieldRef名称='ID'/><值类型='数'> 0℃; /价值与GT; +
    &所述; / NEQ>中+
    < /在哪里>中+
    &所述;排序依据>中+
    < FieldRef名称=标题/>中+
    &所述; /排序依据>中+
    < /查询>中;  $()。SPServices({
    操作:方法,
    异步:假的,
    LISTNAME:列表中,
    CAMLViewFields:fieldsToRead,
    CAMLQuery:查询,
    completefunc:功能(XDATA,状态){
      $(xData.responseXML).SPFilterNode(Z:排)。每个(函数(){        变量名称=($(本).attr(ows_Title));
        VAR文字=($(本).attr(ows_treo));
        VAR值=($(本).attr(ows_iz1y));        AddRowToTable(名称,文字,价值);
      });
    }
  });
}功能AddRowToTable(名称,文字,价值){
  arr.push({'名':姓名,文本:文本,值:值});
}

simple question for most of you i think;-) i have an array (from a Sharepoint List) this Array shows like this if i write it via .html into a DIV:

{name="Drittes Kapitel"text="uiuiui"value="45.0000000000000"}
{name="Ein anderer Titel"text="Ein zweiter Text"value="123.000000000000"}
{name="Ein Titel"text="Das ist der Text"value="256.000000000000"}

my Code to bring this up into a DIV is the following:

function AddContent(name,text,value)
{    $("#meineListe").append('{name="'+ name + '"text="' + text + '"value="'+ value +'"}<br />');
                                
}

so it shows me up all 3 Elements in this Array But how can i put the array into a Variable? if i try to

function AddElements(name,text,value)
{   MyElements = ('{name="'+ name + '"text="' + text + '"value="'+ value +'"}<br />');
	console.log(MyElements);
	$("#meineListe").html(MyElements);
                                
}

it shows me just the first entry of them... i did not understand what happend here exactly.

Whole Code (from sharepointhillbilly):

//this is where the script starts after the page is loaded
$(document).ready(function() { 

    GetMyListData();

});

function GetMyListData()
{
        //The Web Service method we are calling, to read list items we use 'GetListItems'
        var method = "GetListItems";
        
        //The display name of the list we are reading data from
        var list = "MyList";

        //We need to identify the fields we want to return. In this instance, we want the Name (Title),
        //Blog, and Picture fields from the Speakers list. You can see here that we are using the internal field names.
        //The display name field for the Speaker's name is "Name" and the internal name is "Title". You can see it can 
        //quickly become confusing if your Display Names are completely differnt from your internal names. 
        //For whatever list you want to read from, be sure to specify the fields you want returned. 
        var fieldsToRead =     "<ViewFields>" +
                                "<FieldRef Name='Title' />" +
                                "<FieldRef Name='treo' />" +
                                "<FieldRef Name='iz1y' />" +
                            "</ViewFields>";
                            
        //this is that wonderful CAML query I was talking about earlier. This simple query returns
        //ALL rows by saying "give me all the rows where the ID field is not equal to 0". I then 
        //tell the query to sort the rows by the Title field. FYI: a blank query ALSO returns
        //all rows, but I like to use the below query because it helps me know that I MEANT to 
        //return all the rows and didn't just forget to write a query :)
        var query = "<Query>" +
                        "<Where>" +
                            "<Neq>" +
                                "<FieldRef Name='ID'/><Value Type='Number'>0</Value>" + 
                            "</Neq>" +
                        "</Where>" +
                        "<OrderBy>" + 
                            "<FieldRef Name='Title'/>" +
                        "</OrderBy>" +
                    "</Query>";

        //Here is our SPServices Call where we pass in the variables that we set above
        $().SPServices({
                operation: method,
                async: false,  //if you set this to true, you may get faster performance, but your order may not be accurate.
                listName: list,
                CAMLViewFields: fieldsToRead,
                  CAMLQuery: query,
                      //this basically means "do the following code when the call is complete"
                    completefunc: function (xData, Status) { 
                        //this code iterates through every row of data returned from the web service call
                        $(xData.responseXML).SPFilterNode("z:row").each(function() { 
                            //here is where we are reading the field values and putting them in JavaScript variables
                            //notice that when we read a field value there is an "ows_" in front of the internal field name.
                            //this is a SharePoint Web Service quirk that you need to keep in mind. 
                            //so to read a field it is ALWAYS $(this).attr("ows_<internal field name>");
                            
                            //get the title field (Element Title)
                            var name = ($(this).attr("ows_Title"));
							var text = ($(this).attr("ows_treo"));
							var value = ($(this).attr("ows_iz1y"));
                            
                            //get the blog url, SharePoint stores a url in the form of <url><comma><description>
                            //We only want the <url>. To accomplish this we use the javascript "split" function
                            //which will turn <url><comma><description> into an array where the first element [0]
                            //is the url.   Catch all that? if you didn't this is another reason you should be
                            //a developer if you are writing JavaScript and jQuery :)
                            //var blog = ($(this).attr("ows_Blog")).split(",")[0];
                            
                            //same thing as the blog, a picture is stored as <url><comma><alt text>
                            //var pictureUrl = ($(this).attr("ows_Picture")).split(",")[0];
                            
                            //call a function to add the data from the row to a table on the screen
                            AddElements(name,text,value);
                            
                        });                
                    }
        });

}

// very simple function that adds a row to a table with the id of "speakerTable" 
// for every row of data returned from our SPServices call. 
// Each row of the table will display the picture of the speaker and
// below the speaker's picture will be their name that is a hyperlink
// to the speaker's blog.
function AddRowToTable(name,text,value)
{   MyElements = ('{name="'+ name + '"text="' + text + '"value="'+ value +'"}<br />');
	console.log(MyElements);
	$("#meineListe").html(MyElements);
                                
}

<!-- table where our listContent rows will go -->
<div id="meineListe"></div>

any suggestions?

i need this array in a JS Variable. is there another solution as in .append to add an array into a varaible?

解决方案

$('#meineList').append(item); will append item to your element with the id of meineList, while $('#meineList').html(item); will replace any html inside of your element with the id of meineList with the item that you pass in.

I assume you call AddContent() or AddElements() for each of the items, which is the reason why when you call AddElements(), it will display your first element, then replace it with the second, then replace it with the third, thus showing only the final element that you pass to it. So, you should definitely use the AddContent() function, so that all of your items will be displayed.

To add each item you pass in to an array, you will need to create an array outside of the function, then add a new element to it each time you call your function.

EDIT:

Looking at your full code, your AddRowToTable() function should be called for each of the elements you want to add, so if you call .html() inside the function, you will only get the last element output.

Instead, just add the element to the array in your AddRowToTable() function, then print out the contents of the array after your GetMyListData() function is complete.

Try changing your code to something like this (I removed the comments for better readability):

var arr = [];

$(document).ready(function() {
  GetMyListData();
  $('#meineList').html(arr.toString());
});

function GetMyListData() {
  var method = "GetListItems";
  var list = "MyList";

  var fieldsToRead = "<ViewFields>" +
    "<FieldRef Name='Title' />" +
    "<FieldRef Name='treo' />" +
    "<FieldRef Name='iz1y' />" +
    "</ViewFields>";

  var query = "<Query>" +
    "<Where>" +
    "<Neq>" +
    "<FieldRef Name='ID'/><Value Type='Number'>0</Value>" +
    "</Neq>" +
    "</Where>" +
    "<OrderBy>" +
    "<FieldRef Name='Title'/>" +
    "</OrderBy>" +
    "</Query>";

  $().SPServices({
    operation: method,
    async: false,
    listName: list,
    CAMLViewFields: fieldsToRead,
    CAMLQuery: query,
    completefunc: function(xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {

        var name = ($(this).attr("ows_Title"));
        var text = ($(this).attr("ows_treo"));
        var value = ($(this).attr("ows_iz1y"));

        AddRowToTable(name, text, value);
      });
    }
  });
}

function AddRowToTable(name, text, value) {
  arr.push({'name': name, 'text': text, 'value': value});
}

这篇关于如何将阵列添加到JavaScript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆