2017.12.7 AngularJS 一鍵復制、JavaScript數組、 動態添加、LINQ、通用的ajax請求工具

第一組:劉聰 AngularJS 一鍵復制功能

Html:


image.png

Js:


image.png

效果:

image.png

第二組:馮佳麗 JavaScript數組方法總結

中篇:
  1. findIndex

作用:findIndex()方法根據傳入的測試條件(函數)遍歷數組元素直到找到符合條件的元素。
返回值:Number,符合條件的第一個元素的索引位置,之后元素不會再執行,如果沒有符合條件的元素返回-1
原數組:不改變
注意: findIndex()對于空數組,函數不執行,返回-1
語法:array.findIndex(function(currentValue, index, arr), thisValue)
語法參數解析:同上

var arr1=[6,3,32,16,19];
console.log(arr1.findIndex(function(i){
    return i>10;
}))     //-1
console.log(arr1);      //[6,3,32,16,9]
console.log(arr1.findIndex(function(i){
    return i>40;
}))     //-1
console.log(arr1.findIndex(function(i,index){
    return i>10;
}))     //2
console.log(arr1.findIndex(function(i,index,arr1){
    return i>10;
}))     //2
console.log(arr1.findIndex(function(i){
    return i>10;
},30))     //2
console.log([].findIndex(function(i){
    return i>10;
}))     //-1
  1. forEach

作用:forEach()方法用于調用數組的每個元素,并將元素傳遞給回調函數。
返回值:數組元素調用后的結果
原數組:不改變
注意: forEach() 對于空數組是不執行回調函數的。
語法:array.forEach(function(currentValue, index, arr), thisValue)
語法參數解析:同上

var arr1=[6,3,32,16,19];
arr1.forEach(function(i){
    console.log(i+1);
})      //7 4 33 17 20
console.log(arr1);      //[6,3,32,16,9]
arr1.forEach(function(i,index){
    console.log(i+1);
})      //7 4 33 17 20
arr1.forEach(function(i,index,arr1){
    console.log(arr1[index]);
})      //6 3 32 16 19
arr1.forEach(function(i){
    console.log(i+1);
  1. map

作用:map() 方法通過指定函數處理數組的每個元素,并返回處理后的數組。
返回值:Array,新數組,數組元素為原始數組元素調用函數處理后的值
原數組:不改變
注意: map() 不會對空數組進行檢測
語法:array.map(function(currentValue,index,arr), thisValue)
語法參數解析:同上

var arr1=[6,3,32,16,19];
console.log(arr1.map(function(i){
    return i+1;
}))     //[7, 4, 33, 17, 20]
console.log(arr1);      //[6,3,32,16,9]
console.log(arr1.map(function(i){
    return i+1;
}))     //[7, 4, 33, 17, 20]
console.log(arr1.map(function(i,index){
    return i+1;
}))     //[7, 4, 33, 17, 20]
console.log(arr1.map(function(i,index,arr1){
    return i+1;
}))     //[7, 4, 33, 17, 20]
console.log(arr1.map(function(i){
    return i+1;
},30))     //[7, 4, 33, 17, 20]
console.log([].map(function(i){
    return i+1;
}))     //[]
  1. reduce

作用:reduce()方法根據接收的函數將數組元素計算為一個值(從左到右)。
返回值:返回計算后的值
原數組:不改變
注意:reduce()對于空數組是不會執行回調函數的。
語法:array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
語法參數解析:function(total,currentValue,currentIndex,arr)函數必需,數組中每個元素都會執行這個函數;函數中的參數total必需,表示初始值或者計算結束后的返回值;currentValue必須,表示當前元素的值;currentIndex可選,表示當前元素的索引值;arr可選表示該數組;initialValue可選,表示傳遞給函數的初始值

var arr1=[6,3,32,16,19];
console.log(arr1.reduce(function(sum,val){
    return sum+val;
}))     //76
console.log(arr1);      //[6,3,32,16,9]
console.log([].reduce(function(sum,val){
    return sum+val;
}))     //報錯
  1. reduceRight

作用:reduceRight()方法根據接收的函數將數組元素計算為一個值(從左到右)。
返回值:返回計算后的值
原數組:不改變
注意:reduce() 對于空數組是不會執行回調函數的。
語法:array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)
語法參數解析:同上

var arr1=[6,3,32,16,19];
console.log(arr1.reduceRight(function(sum,val){
    return sum+val;
}))     //76
console.log(arr1);      //[6,3,32,16,9]
console.log([].reduceRight(function(sum,val){
    return sum+val;
}))     //報錯
  1. fill

作用:fill()方法用于將一個固定值替換數組的元素。
返回值:Array,替換后的數組
是否改變原數組:改變
語法:array.fill(value, start, end)
語法參數解析:value為必需,表示填充的值;start可選,表示開始填充位置;end為可選,表示停止填充位置(默認為arr.length)

var arr1=[1,2,3,4,5,6];
console.log(arr1.fill('ab',2));     //[1, 2, "ab", "ab", "ab", "ab"]
console.log(arr1);     //[1, 2, "ab", "ab", "ab", "ab"]
var arr2=[1,2,3,4,5,6];
console.log(arr2.fill('fv',2,4));     //[1, 2, "fv", "fv", 5,6]
console.log(arr2);     //[1, 2, "fv", "fv", 5,6]
  1. indexOf

作用:indexOf()方法從頭到尾地檢索數組字符串stringObject,并返回某個指定的字符串值所在的位置。
返回值:Number,某個指定的字符串值在字符串中首次出現的位置,未找到則返回-1
原數組:不改變
語法:array.indexOf(item,start)
語法參數解析:item為必需,表示查找的元素;start可選的整數參數,表示開始查找的索引位置,默認為0

var arr1=['a','c','b','f','a','v','a'];
console.log(arr1.indexOf('a'));     //0
console.log(arr1);      //['a','c','b','f','a','v','a']
console.log(arr1.indexOf('a',3));   //4
console.log(arr1.indexOf('af'));    //-1
  1. lastIndexOf

作用:類似于indexOf()方法,lastIndexOf()方法從后往前檢索數組字符串stringObject,并返回某個指定的字符串值所在的位置。
返回值:Number,某個指定的字符串值在字符串中最后出現的位置,未找到則返回-1
原數組:不改變
語法:array.lastIndexOf(item,start)
語法參數解析:item為必需,表示查找的元素;start可選的整數參數,表示開始查找的索引位置,默認為array.length-1

var arr1=['a','c','b','f','a','v','a'];
console.log(arr1.lastIndexOf('a'));     //6
console.log(arr1);      //['a','c','b','f','a','v','a']
console.log(arr1.lastIndexOf('a',3));   //0
console.log(arr1.lastIndexOf('a',5));   //4
console.log(arr1.lastIndexOf('af'));    //-1
  1. join

作用:join()方法用于把數組中的所有元素轉換成一個字符串,元素是通過指定的分隔符進行分隔的。
返回值:String,轉換后的字符串
原數組:不改變
語法:array.join(separator)
語法參數解析:separator可選,表示要使用的分隔符。如果省略該參數,則使用逗號作為分隔符。

var arr1=[1,2,3,4,5];
console.log(arr1.join());       //'1,2,3,4,5'
console.log(arr1);      //[1,2,3,4,5]
console.log(arr1.join('and'));      //'1and2and3and4and5'
  1. toString

作用:toString()方法可把數組轉換為字符串,并返回結果,數組中的元素之間用逗號分隔
返回值:String,轉換后的字符串
原數組:不改變
語法:array.toString()

var arr1=[1,2,3,4,5];
console.log(arr1.toString());   //'1,2,3,4,5'
console.log(arr1);      //[1,2,3,4,5]

第三組: 動態添加

點圖中監督人員,然后彈出圖中對話框,根據部門顯示相關人員,并可以進行查詢。下面是核心代碼

image.png
///監督人員
$(function() {
    $(".DutyOfficerX").each(function() {
        $(this).attr("onClick", "selectPeople(this)");
        $(this).attr("readonly", "readonly");
    });
});
function selectPeople(a) {
    var controlId = $(a).attr("uid");
    var checkperson = a.id AlertPage('selectPeopleList.aspx?dutydept=質量管理分部' + '&idlist=' + controlId + '&tempid=' + checkperson, '選擇人員', 250, 500, this)
}
function insertPeopleTpara(id, name, tempid) {
    if (id != "") {
        $("#" + tempid).val(name);
        $("#" + tempid).attr("uid", id);
    }
}

$(function() {
    dataInit();
}) function dataInit() {
    $("#<%=PmAsProblemCorActionsIm.ClientID%>").attr("uid");
    $("#<%=PmAsProblemCorActionsPre.ClientID%>").attr("uid");
}

selectPeopleList.aspx

$(function() {
    checked();
}) function checked() {
    var str = document.getElementsByName('cb');
    var a = '<%=Request["idlist"]%>';
    for (var i = 0; i < str.length; i++) {
        if (a.indexOf(str[i].value) > -1) {
            str[i].checked = true;
        }
    }
}

function savePeople() {
    var a1 = GetSelectValue('cb');
    if ($("#tblist").find("tr").length == 0 || a1 == "") {
        alert("無人員數據。");
        return false;
    }

    a1 = GetSelectValue('cb');
    var uid = '',
    uname = '';
    $("[name='cb']").each(function() {
        if ($(this).attr("checked") == true || $(this).attr("checked") == "checked") {
            uid += $(this).attr("value") + ",";
            uname += $(this).attr("u_name") + ",";
        }
    }) if (uname.length > 0) {
        uname = uname.substring(0, uname.length - 1);
    }
    ShowMsg(uid, uname);
}

function ShowMsg(id, name) {

    parent.insertPeopleTpara(id, name, '<%=Request["tempid"]%>');
    parent.CloseAlertPage();
}


第四組:李俊 LINQ的select方法查詢from、where子句

上一次用到了LINQ的select方法查詢,其實,它還有from、where子句。通常情況下,用var關鍵字聲明結果變量,用from子句指定數據源,where子句指定條件,select子句選擇元素。代碼如下:

class IntroToLINQ
{        
    static void Main()
    {
        // The Three Parts of a LINQ Query:
        //  1. Data source.
        int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

        // 2. Query creation.
        // numQuery is an IEnumerable<int>
        var numQuery =
            from num in numbers
            where (num % 2) == 0
            select num;

        // 3. Query execution.
        foreach (int num in numQuery)
        {
            Console.Write("{0,1} ", num);
        }
    }
}

其中,where子句是可選的,甚至可以忽略,但大多數情況下,都要指定where條件,把結果限制為我們需要的數據,可使用&&或||來連接多個條件,本例限制條件為偶數;select子句是必須的,因為必須指定結果集中有哪些元素;通常得到的數據放在一個數組中,此時就可以通過foreach遍歷這些結果。


第五組:周倩宇 通用的ajax請求工具

直接調用 ajaxRequest 方法,傳遞需要的參數,即可ajax請求,節約時間

  • 通用的ajax請求函數
    • @param type 請求類型 get 或者 post
    • @param url 請求接口url
    • @param parmasMap Map類型的參數,key與value方式,其中,value統一為字符串
    • @param async 是否異步請求
    • @param successFun 請求成功回調函數的名稱
function ajaxRequest(type, url, parmasMap, async, successFun) {
    //拼接參數
var parmas = "{";

    //遍歷map,獲取參數
    if(parmasMap != null){
        parmasMap.each(function(key,value,index){
            parmas += "\"" + key + "\"" + ":" + "\"" + value + "\"";
            if(index < parmasMap.size() - 1){
                parmas += ",";
            }
        });
        parmas += "}";
    }
    
    $.ajax({
        type : type,
        url : url,
        data : parmasMap == null ? null : eval("(" + parmas + ")"),
        async : async,
        cache : false,
        dataType : "text",
        contentType : "application/json",
        success : function(data) {
            var funObj = new runFun(successFun, data);
            try {
                funObj.func();
            } catch (e) {
                console.log(e);
                console.log(successFun + "()方法不存在或方法內部代碼執行有錯誤");
            }
        }
    });
} 

//根據方法名稱,調用執行方法
//successFun :請求成功回調的方法名稱
//data : 后臺返回的數據

function runFun(successFun, data) {
    this.func = new Function(successFun + "('" + data + "');");
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,836評論 6 540
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,275評論 3 428
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,904評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,633評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,368評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,736評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,740評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,919評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,481評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,235評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,427評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,968評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,656評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,055評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,348評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,160評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,380評論 2 379

推薦閱讀更多精彩內容

  • 數組的方法 首先我們先打印一下Array.prototype看一下數組都有哪些方法 concat1.定義和用法:c...
    黃鶴你不是人閱讀 418評論 0 0
  • 86.復合 Cases 共享相同代碼塊的多個switch 分支 分支可以合并, 寫在分支后用逗號分開。如果任何模式...
    無灃閱讀 1,407評論 1 5
  • 一 2011年-2015年,我幾乎每年去一次上海。前三次是被動,因為不同人的邀請,推脫不掉。而最后一次是主動去的,...
    顧子梔閱讀 437評論 1 2
  • 下雨天最美好的事情之一就是:聽著指彈曲、喝著茶、畫著畫 有同好者可以互相學習交流
    林玉楓閱讀 220評論 5 8
  • 剛才在知乎看了一篇文章,里邊有一句話說的非常好你的衣服不僅僅是衣服,也代表了個人的形象,公司的形象。 經常看奇葩說...
    AronGao閱讀 814評論 0 0