weixin_33709609 2014-07-17 15:49 采纳率: 0%
浏览 77

在$ .each()语句中循环

I'm having a result inside which there are 5 items. Now under certain conditions i have to push those items in an array and bind it in jqgrid as columns and rows. So i have to loop into the result(something like that). How can i do?

//Code:

var colname = [];
    var colHeader = [];

    $.ajax({
        url: '@(Url.Action("LoadColumns", "Home"))',
        datatype: 'json',
        mtype: 'GET',
        success: OnComplete,
        error: OnFail
    });
    function OnComplete(result) {
        var i = 0;
        $.each(result, function () {
            console.log(result.rows[i]);
           var currentRow = result.rows[i];
           colHeader.push(currentRow.Name);
          // alert(currentRow.Name);
           switch (currentRow.Datatype) {

                case 'int':
                    colname.push({ name: currentRow.Name, index: currentRow.Name, width: 200, align: 'left', sortable: true, editable: false, sorttype: 'int' });
                    break;
                case 'String':
                    colname.push({ name: currentRow.Name, index: currentRow.Name, width: 200, align: 'left', sortable: true, editable: true });
                    break;
                case 'DateTime':
                    colname.push({
                        name: currentRow.Name, index: currentRow.Name, width: 200, align: 'left', sortable: true, editable: true, sorttype: 'date',
                        formatter: 'date', formatoptions: { newformat: 'm-d-Y' }, datefmt: 'm-d-Y'
                    });
                    break;
                case 'dropdown':
                    if (currentRow.ValueList != null && currentRow.ValueList != '') {
                        var ValueListArray = currentRow.ValueList.split(" ");
                        var valueListItems = '';
                        $.each(ValueListArray, function (index, values) {
                            valueListItems = valueListItems + values + ":" + values + ";";
                        });
                    }
                    colname.push({
                        name: currentRow.Name, index: currentRow.Name, width: 200, edittype: "select", formatter: 'select',
                        editoptions: { value: valueListItems.slice(0, -1) }, stype: 'select'
                                , searchoptions: { value: ':All;' + valueListItems.slice(0, -1) }, align: 'left', sortable: true, editable: true
                    });
                    break;

            }

            i++;
        });
}

when i tried i'm getting only one value getting printed for 11 times.

Not sure where i'm wrong? Kindly help.

  • 写回答

2条回答 默认 最新

  • 妄徒之命 2014-07-17 16:32
    关注

    It seems that you are new in JavaScript language. The most people start with another language and have to use JavaScript language later. The problem is that JavaScript language is quite different as C++/C# or Java which is probably your current favorite language. I strictly recommend you to change the style of your JavaScript code. Don't use the same style which you use in your other favorite language.

    First of all you should follow the name conversion which are standard in JavaScript language. You can see already from the text of you question that OnComplete, OnFail or ValueListArray have another color as i variable for example. The reason: names of variables having the first capital letter are used as constructors of objects only. So one use

    var d = new Date();
    

    for example instead of var d = Date();.

    In the same way you should not always trying to use foreach construct which you probably like in C#. The current version of JavaScript don't have the construct. So the usage of

    var i, rows = result.rows, n = rows.length, item;
    for (i = 0; i < n; i++) {
        item = rows[i];
        // now you can work with item
    }
    

    is much more quickly as the usage of $.each. You can see that I saved the values of rows property (result.rows) in a variable and I saved rows.length in the next variable instead of usage rows.length directly in for loop. It improves the performance too because don't optimize the code. It can be that the size of rows array will be changes or even that length property will be overwritten by your custom implementation inside of for body. So JavaScript don't make any code optimization and saving unchanged property value in a variable will improve the performance.

    Even the usage of colHeader variable defined in outer scope of OnComplete function works slowly as working with local variable colHeader. Probably you can do all the work inside of OnComplete (after the loop $.each)? The current code makes OnComplete closure. It works in other way like you good knows in C#. So I recommend you to invest your time in studding JavaScript language. The argument after which I decide to do this for me was the fact that JavaScript don't have block level variables and use function level variables instead. If one don't understand that then one can write and read the code which will be interpreted by JavaScript in absolutely another way as you think. See the answer for more details.

    One more remark about the code

    colname.push({ name: currentRow.Name, index: currentRow.Name, width: 200,
        align: 'left', sortable: true, editable: false, sorttype: 'int' });
    

    and other close lines. jqGrid don't have any data type. On the other side one can use column templates (see the answer) which can make your code shorter and more readable.

    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵