AngularJS - 将数据从输入推送到特定对象内的数组

时间:2017-09-19 11:11:37

标签: angularjs arrays json multidimensional-array

我有一个包含不同员工对象的数组,每个对象都有自己的嵌套付款数组,如下所示:

var employees = [
{
    id: '1',
    icon: 'img/NC.png',
    iconAlt: 'N C Image',
    title: 'Mr',
    firstName: 'N',
    lastName: 'C',
    dateOfBirth: '01/01/1900',
    niNumber: 'JZ123456D',
    jobTitle: 'Web Developer',
    department: 'Development',
    joinDate: '18/04/2017',
    leaveDate: '18/04/2017',
    email: 'user@mail.com',
    phonePrimary: '0123 456789',
    phoneSecondary: '0123 456789',
    menu: 'nickCookMenu',
    payments: [
        {
            id: 1,
            code: 'TEST',
            paymentType: '',
            hours: '',
            zeroiseHours: false,
            partPay: false,
            rate: '',
            employerPercentage: '',
            pay: '',
            paidPer: '',
            fromDate: '',
            sequence: '',
            employerPension: '',
            csaReference: '',
            suspend: false,
            netToGross: false,
            requiredTotal: '',
            paidToDate: '',
            protectedNet: '',
            arrearsCarried: ''
        },
        {
            id: 2,
            code: 'TEST',
            paymentType: '',
            hours: '',
            zeroiseHours: false,
            partPay: false,
            rate: '',
            employerPercentage: '',
            pay: '',
            paidPer: '',
            fromDate: '',
            sequence: '',
            employerPension: '',
            csaReference: '',
            suspend: false,
            netToGross: false,
            requiredTotal: '',
            paidToDate: '',
            protectedNet: '',
            arrearsCarried: ''
        },
    ]
},
]

-

我已经以下面的形式获取了各种输入的值,并希望将此数据作为新的付款添加到特定员工的付款数组中。

    if(!error) {
        var employeePaymentFormData = [
            {
                code: angular.element('#employeePaymentForm input#paymentCodeInput').val(),
                paymentType: angular.element('#employeePaymentForm input#paymentTypeSelect').val(),
                hours: angular.element('#employeePaymentForm input#paymentHoursInput').val(),
                zeroiseHours: angular.element('#employeePaymentForm input#zeroiseHoursCheckbox').val(),
                partPay: angular.element('#employeePaymentForm input#paryPayCheckbox').val(),
                rate: angular.element('#employeePaymentForm input#paymentRateInput').val(),
                employerPercentage: angular.element('#employeePaymentForm input#employerPercentageInput').val(),
                pay: angular.element('#employeePaymentForm input#paymentPayInput').val(),
                paidPer: angular.element('#employeePaymentForm input#paidPerInput').val(),
                fromDate: angular.element('#employeePaymentForm input#fromDateInput').val(),
                sequenceInput: angular.element('#employeePaymentForm input#sequenceInput').val(),
                employerPension: angular.element('#employeePaymentForm input#employerPensionInput').val(),
                csaReference: angular.element('#employeePaymentForm input#csaReferenceInput').val(),
                suspendPayment: angular.element('#employeePaymentForm input#suspendPaymentCheckbox').val(),
                netToGross: angular.element('#employeePaymentForm input#netToGrossCheckbox').val(),
                requiredTotal: angular.element('#employeePaymentForm input#requiredTotalInput').val(),
                paidToDate: angular.element('#employeePaymentForm input#paidToDateInput').val(),
                protectedNet: angular.element('#employeePaymentForm input#protectedNetInput').val(),
                arrearsCarried: angular.element('#employeePaymentForm input#arrearsCarriedInput').val(),
            }
        ]
    }

这一切都是在员工详细信息状态下完成的,该状态仅为特定员工传递了数据,因此这有助于识别员工的ID以将数据推送到。

1 个答案:

答案 0 :(得分:0)

如果我了解您想要将表单数据添加到特定员工的付款数组中?

首先你需要获得employee_id。完成后,你可以这样做:

var someEmployeeId = 1; // get it from route, or w/e

employees.forEach(function(employee){
    if (employee.id === someEmployeeId){
        employee.payments.push(employeePaymentFormData);
    }
});