简体   繁体   English

如何创建动态对象HTML并分配给定的基于属性的数据

[英]How to Create Dynamic Object HTML and Assign Attribute Based Data Given

I have dynamic object (button and textbox) create by jquery where each time I press Add Transport. 我有一个动态对象(按钮和文本框),由jquery每次按添加传输时创建。 I took this code from this forum and modify little bit to suit my situtions (thanks to whom create this code). 我从这个论坛上获取了这段代码,并做了一些修改以适应我的观点(感谢谁创建了这段代码)。

Let me detail first regarding my dynamic items: 首先让我详细介绍我的动态项目:

My set of group data are Trip[], Bus No[] and Amount[] . 我的一组组数据是Trip[], Bus No[] and Amount[] This set can be multiple but consistent with 3 items only each group. 该集合可以是多个,但每个组只能包含3个项目。

trip[] = button object
busno[] = text object
amount = text object

Below are my HTML script: 以下是我的HTML脚本:

    <div class="purchase-items-fieldset" style="clear:both;">
        <div class="purchase-items-wrapper">
            <div class="purchase-items">                            
                        <ul>
                        <li>
                        <input type="button" name="trip[]" value="PB" class="field btn-field">
                        </li>
                        <li>
                        <input type="text" name="busno[]" class="field txt-field">
                        </li>
                        <li>
                        <input type="text" name="amount[]" class="field txt-field">
                        </li>
                        </ul>
                <input type="button" class="remove-line btn-remove" style="border:solid">
            </div>
        </div>
        <button type="button" id="btnAddTrans" class="add-field" style="display: none">Add field</button>
    </div>

Let says I have 2 set group data like below ***(mydata="PB,WBX001,1000,P,WBK001,500")*** 假设我有2组组数据,如下所示***(mydata="PB,WBX001,1000,P,WBK001,500")***

Then, my plan is:- 然后,我的计划是:

Group 1
trip[]=PB
busno[]=WBX001
amount[]=1000

Group 2 
trip[]=P
busno[]=WBK001
amount[]=500 

where I want Jquery/Javascript create 2 group dynamic object and assign value each object base on plan above :- 我想在其中jQuery / Javascript创建2组动态对象,并根据上述计划为每个对象分配值:-

function assigndatatrip (mydata) {
        //mydata="PB,WBX001,1000,P,WBK001,500"
        //each 3 item are 1 set group data etc: PB,WBX001,500

        //do split function and count how many set group
                 // create dynamic object
                 // assign data for dynamic object
        //loop
        //else no more data to assign then exit-return             
    }

I no idea where to start because I not so good on javascript/jquery function. 我不知道从哪里开始,因为我对javascript / jquery函数不太满意。

I hope anybody who face this problem can share and help me how to solve this problem. 我希望遇到这个问题的任何人都能分享并帮助我如何解决这个问题。 Thanks on advance who's reading and reply this question. 预先感谢谁正在阅读并回答此问题。

Thanks you. 谢谢。

If you will have that structure for your data : "each three items a group" . 如果您具有用于数据的结构: "each three items a group"

Then you can apply this logic: 然后,您可以应用以下逻辑:

 $(document).ready(function() { //Initialize vars var start, mydata = ["PB", "WBX001", "1000", "P", "WBK001", "500"] //Loop to create items based on the array amount/3 for (start = 0; start < (mydata.length / 3); start++) { //Target the elements of the array you need for each group var trip = start * 3, busno = trip + 1, amount = trip + 2; //Create the element to append var item = "<div class='item'><ul><li>" + mydata[trip] + "</li><li>" + mydata[busno] + "</li><li>" + mydata[amount] + "</li></ul></div>"; //Append the group element $('body').append(item); } }) 
 .item { color: white; background: purple; margin: 20px auto; padding: 25px; } .item li { list-style: none } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

I must thanks to DaniP who have spirit and kindness, helping without asking. 我必须感谢DaniP的精神和友善,不加要求地提供帮助。 Thanks again. 再次感谢。

I think and think many time regarding problem above, I can't use DaniP entire solution because I already have function AddTrans, SaveTrans and many more. 对于上述问题,我想了很多次,我无法使用DaniP整个解决方案,因为我已经有了AddTrans,SaveTrans等功能。 My critical part only how to display back all a data I already save. 我的关键部分只是如何显示我已经保存的所有数据。 Since object are dynamic my head getting sick to think and honestly I really bad on javascript/jquery coding. 由于对象是动态的,老实说,我的头不舒服,我真的对javascript / jquery编码不好。

Ok. 好。 If anybody using Dynamic Object inside Div and already run perfectly but do not know how to display back a data while create dynamic object then please consider my code below are one of solution on your reference. 如果有人在Div内使用Dynamic Object并且已经完美运行,但是不知道在创建动态对象时如何显示数据,那么请考虑以下我的代码是您参考中的解决方案之一。

 function assigndatatrans(mydata) {
    //var mydata = "PB,WBX001,500,P,WBK001,300"
    var gdata = mydata.split(',').length / 3;
    for (start = 0; start < gdata - 1; start++) {
        $('#btnremove').click();
        clickaddtrans();
    }
    var substr = mydata.split(',');
        $.each($('.field'), function (index,value) {
            $(this).val(substr[index]);
        });
}

Let me explain my code for myself (I still on learning process JQuery function):- 让我为我自己解释代码(我仍在学习JQuery函数的过程中):-

First thing first is data string get from database 首先是从数据库获取数据字符串

//var mydata = "PB,WBX001,500,P,WBK001,300"

then I need to count how many group data I have, each 3 item are 1 group then I need do small calculation 那么我需要计算一下我有多少组数据,每3个项目是1组,那么我需要做少量计算

var gdata = mydata.split(',').length / 3;

After that, I know I already have function AddTrans and RemoveTrans then I need to clear first my dynamic object and show it again based how many group should be appear:- 之后,我知道我已经具有AddTrans和RemoveTrans函数,然后我需要先清除我的动态对象,然后根据应显示的组数再次显示它:

for (start = 0; start < gdata - 1; start++) {
        $('#btnremove').click();
        clickaddtrans();
    }

Finally, I learn some function $.each and Index and Value on JQuery recently and I try apply to make sure my data string can be perfectly insert into each field on my dynamic object. 最后,我最近在JQuery上学习了一些函数$ .each和Index and Value,并尝试应用以确保我的数据字符串可以完美地插入到动态对象的每个字段中。 I think this part are really tricky for me and I try many time to make it working. 我认为这部分对我来说真的很棘手,我花了很多时间使它起作用。

var substr = mydata.split(',');
    $.each($('.field'), function (index,value) {
        $(this).val(substr[index]);
    });

That's all.... Now I can get my output run smoothly and workable. 仅此而已...现在,我可以使输出运行平稳且可行。 I can't say perfect! 我不能说完美! because I believe yours all who read this question can do better than this and far far more superior. 因为我相信所有阅读此问题的人都可以做得更好,而且要优越得多。

Anyway thanks so much and sleep well. 无论如何,非常感谢,并睡得很好。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM